Repository: codeigniter4/CodeIgniter4 (PHP)

Issue:
Bug: Email.php inline images fail.
### PHP Version

7.4, 8.0, 8.1, 8.2, 8.3

### CodeIgniter4 Version

4.4.1+

### CodeIgniter4 Installation Method

Composer (using `codeigniter4/appstarter`)

### Which operating systems have you tested for this bug?

macOS, Linux

### Which server did you use?

apache

### Database

_No response_

### What happened?

Inline images for emails show up as attachments, Cid reference is empty.

### Steps to Reproduce

Make a new email with `\Config\Services::email();`
Attach an image: `$email->attach('image data', 'inline', 'image001.png', 'image/png');`
Get the cid for the image; `$email->setAttachmentCID('image001.png');`
Add the image to the body: `<img src="cid:'. $cid1 .'">`
Send the email.

### Expected Output

Email should contain the image inline with no attachment shown.

### Anything else?

The problem lies in vendor/codeigniter4/framework/system/Email/Email.php
Method setAttachmentCID references [0] for name of the attachment while it should be [1].
Both
```php
if ($attachment['name'][0] === $filename) {
```
and
```php
$this->attachments[$i]['cid'] = uniqid(basename($attachment['name'][0]) . '@', true);
```
have the wrong index.

The array comes from the attach method line 681: `$namesAttached = [$file, $newname];`



Discussion: The first parameter is a real filename., not new filename.
See https://codeigniter4.github.io/CodeIgniter4/libraries/email.html#CodeIgniter\Email\Email::setAttachmentCID

```php
    public function index(): string
    {
        $email = \Config\Services::email();

        $filename = ROOTPATH . 'tests/_support/Images/EXIFsamples/portrait_4.jpg';
        $email->attach($filename);

        $email->setFrom('your@example.com', 'Your Name');
        $email->setTo('foo@example.jp');
        $cid = $email->setAttachmentCID($filename);
        $email->setMessage('<img src="cid:' . $cid . '" alt="photo1">');
        $email->send(false);

        return $email->printDebugger();
    }
```
```
Your message has been successfully sent using the following protocol: mail<br><pre>Date: Tue, 16 Jan 2024 20:10:10 +0000
From: "Your Name" &lt;your@example.com&gt;
Return-Path: &lt;your@example.com&gt;
Reply-To: &lt;your@example.com&gt;
User-Agent: CodeIgniter
X-Sender: your@example.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: &lt;65a6e2a2eedb71.41636424@example.com&gt;
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="B_ATC_65a6e2a2ef0852.10779133"

This is a multi-part message in MIME format.
Your email application may not support this format.

--B_ATC_65a6e2a2ef0852.10779133
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

&lt;img src="cid:portrait_4.jpg@65a6e2a2eeb8e0.31559128" alt="photo1"&gt;


--B_ATC_65a6e2a2ef0852.10779133
Content-Type: image/jpeg; name="portrait_4.jpg"
Content-Disposition: attachment;
Content-Transfer-Encoding: base64
Content-ID: &lt;portrait_4.jpg@65a6e2a2eeb8e0.31559128&gt;

/9j/4AAQSkZJRgABAQEASABIAAD/7QAsUGhvdG9zaG9wIDMuMAA4QklNBCUAAAAAABDUHYzZjwCy
...
```
From the documentation: "If you need to use a buffer string instead of a real - physical - file you can use the first parameter as buffer, the third parameter as file name and the fourth parameter as mime-type"

Setting the indexes in setAttachmentCID to 1 will fix it but it's not the proper solution. Still working on that.

So, try this to see it fail:
```php
    // Change to your email to test.
    $to = 'demo@example.com';


    $email = \Config\Services::email();

    $email->attach(base64_decode('iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAACEFBMVEVHcEz/AAAAAAD/AAD/AACqAAD/AAC/AAD/MwDMMwD/KwDVKwDbJAD/IADjHADmGgDqFQDtJADuIgDvIADwHgDhHgDyGwDkGwDmGgDzGADoIwD0IADrHQDtGwDtGgDuGgDmGQDnIADwHwDoHwDpHgDqHADrIQDsHwDtHgDuHADoHADqIADqHwDwHgDrHgDrHQDsHADsHADqHQDrHADsHwDtHgDpHgDtHQDpHQDpHQDqHADuHgDrHgDrHQDsHADsHADtHwDqHgDqHgDuHQDrHQDrHADsHgDsHQDrHADrHgDrHgDuHQDsHQDrHADuHgDsHgDsHgDsHgDsHQDsHQDtHQDtHgDtHADtHgDrHgDsHQDsHgDsHgDsHgDtHgDtHQDtHQDrHQDtHQDrHQDsHQDrHQDrHQDtHgDrHgDtHQDsHQDsHQDsHQDqHQDrH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92LITQWyhwJm/gWvSw3X40mJYEr/70AfeHnqZi2GbFv+jfBmi9wL6GuwnD99sH34XgBsDzzx/b0w4IXdceWHl4iW6ftTuTxu1Lr4B+zgC/ANUzlwVxpAXgAAAAAElFTkSuQmCC'),
      'inline', 'image001.png', 'image/png');

    $cid1 = $email->setAttachmentCID('image001.png');

    $subject = 'CodeIgniter 4 Email inline image bug';
    $message = 'CI Logo image > <img src="cid:'. $cid1 .'"> < image.';

    $email->setTo($to);
    $email->setFrom('demo@example.com');
    $email->setSubject($subject);
    $email->setMessage($message);
    $email->send();
```
> If you need to use a buffer string instead of a real - physical - file you can use the first parameter as buffer, the third parameter as file name and the fourth parameter as mime-type:
```php
$email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');
```
https://codeigniter4.github.io/CodeIgniter4/libraries/email.html#CodeIgniter\Email\Email::attach

@CleverInsert Please try #8446 



Fix (patch):
diff --git a/system/Email/Email.php b/system/Email/Email.php
index d18b40ff7cef..42835c3d404e 100644
--- a/system/Email/Email.php
+++ b/system/Email/Email.php
@@ -705,6 +705,7 @@ public function attach($file, $disposition = '', $newname = null, $mime = '')
     public function setAttachmentCID($filename)
     {
         foreach ($this->attachments as $i => $attachment) {
+            // For file path.
             if ($attachment['name'][0] === $filename) {
                 $this->attachments[$i]['multipart'] = 'related';
 
@@ -712,6 +713,15 @@ public function setAttachmentCID($filename)
 
                 return $this->attachments[$i]['cid'];
             }
+
+            // For buffer string.
+            if ($attachment['name'][1] === $filename) {
+                $this->attachments[$i]['multipart'] = 'related';
+
+                $this->attachments[$i]['cid'] = uniqid(basename($attachment['name'][1]) . '@', true);
+
+                return $this->attachments[$i]['cid'];
+            }
         }
 
         return false;