Skip to content

Bug: Email.php inline images fail. #8419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
CleverInsert opened this issue Jan 16, 2024 · 4 comments · Fixed by #8446
Closed

Bug: Email.php inline images fail. #8419

CleverInsert opened this issue Jan 16, 2024 · 4 comments · Fixed by #8446
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@CleverInsert
Copy link

CleverInsert commented Jan 16, 2024

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

if ($attachment['name'][0] === $filename) {

and

$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];

@CleverInsert CleverInsert added the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 16, 2024
@kenjis kenjis removed the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 16, 2024
@kenjis
Copy link
Member

kenjis commented Jan 16, 2024

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

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

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

        $email->setFrom('[email protected]', 'Your Name');
        $email->setTo('[email protected]');
        $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;[email protected]&gt;
Return-Path: &lt;[email protected]&gt;
Reply-To: &lt;[email protected]&gt;
User-Agent: CodeIgniter
X-Sender: [email protected]
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: &lt;[email protected]&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:[email protected]" 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;[email protected]&gt;

/9j/4AAQSkZJRgABAQEASABIAAD/7QAsUGhvdG9zaG9wIDMuMAA4QklNBCUAAAAAABDUHYzZjwCy
...

@kenjis kenjis closed this as completed Jan 16, 2024
@CleverInsert
Copy link
Author

CleverInsert commented Jan 17, 2024

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:

    // Change to your email to test.
    $to = '[email protected]';


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

    $email->attach(base64_decode('iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAACEFBMVEVHcEz/AAAAAAD/AAD/AACqAAD/AAC/AAD/MwDMMwD/KwDVKwDbJAD/IADjHADmGgDqFQDtJADuIgDvIADwHgDhHgDyGwDkGwDmGgD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IqRC+R/XEFErGRN3goABxeNyBHABZeJlJsnZeudRGzOyd0xnOHFntxiyXb7+XvMVP35bS5V1h/BMKS+KyWW1WrZioxGwy6uYCoor9k9jHCSXGKj+csSbPrapgyHmfOavi+FRtSrCiKWtfYaIPFyVYNNzGx3nhWasDy9TDJTfucNH4ANH5pZMhst7yM0YYt0JUtV4TYeB3wIu1j557s2c44mHFKKrAr6lNWQuw4IAKfZdajI2qgJR5WS3LX/hMNdRwHhB64WK1lsr9aEXezm99zBguBAxqp/C78wWGE2/IjThLXZqjp/LTB363y9sjfLje6HAU2I/6janqfAB728XpyyK+p3ABqnqe92LITQWyhwJm/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('[email protected]');
    $email->setSubject($subject);
    $email->setMessage($message);
    $email->send();

@kenjis kenjis reopened this Jan 20, 2024
@kenjis
Copy link
Member

kenjis commented Jan 20, 2024

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:

$email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');

https://codeigniter4.github.io/CodeIgniter4/libraries/email.html#CodeIgniter\Email\Email::attach

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 20, 2024
@kenjis
Copy link
Member

kenjis commented Jan 23, 2024

@CleverInsert Please try #8446

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants