Email tool on IOS (IPad or Iphone) does not open attachment

I’m having a problem opening attachments sent on IOS (Ipad and iPhone) by CakePHP 4, the attachment icon appears, but the file does not show.
Email resent using another tool, the attachment appears to open or view.

If the same E-Mail attachment opens up in e.g. thunderbird or outlook but doesn’t open in Apple Mail (iOS, MacOS etc.) then it’s not a problem of the server framework (CakePHP) to fix this issue.

Besides the fact, that CakePHP’s mail logic is pretty minimal and you should be able to reproduce your problem with a pure PHP mail as well (unless you of course send via SMTP)

Thank you for answering me, but the problem is in the cake sending, something must be missing, because sent with pure PHP code doesn’t give me any problems:

in my source with CAKEPHP:

$email = new Mailer('default');
        $email->setFrom(['' => 'test']);
        $email->setSubject('Test PDF');
        $r = [
            'test.pdf' => [
                'file' => 'test.pdf',
                'mimetype' => 'application/pdf',
                'contentId' => md5((string) time())
        $email->deliver('Test pdf');

email source response:

Content-Disposition: inline; filename="test.pdf"
Content-Type: application/pdf
Content-Transfer-Encoding: base64


in my source mail with pure PHP:

email source response:

Content-Type: application/pdf;
Content-Disposition: attachment;
Content-Transfer-Encoding: base64


code send:

$name = "Test PDF";
        $email = "";
        $to = "$name <$email>";
        $from = "Test-PDF ";
        $subject = "Here is your attachment";
        $mainMessage = "Hi, here's the file.";
        $fileatt = "test.pdf"; //file location
        $fileatttype = "application/pdf";
        $fileattname = "test-file.pdf"; //name that you want to use to send or you can use the same name
        $headers = "From: $from";
        // File
        $file = fopen($fileatt, 'rb');
        $data = fread($file, filesize($fileatt));
        // This attaches the file
        $semi_rand = md5((string) time());
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
        $headers .= "\nMIME-Version: 1.0\n" .
                "Content-Type: multipart/mixed;\n" .
                " boundary=\"{$mime_boundary}\"";
        $message = "This is a multi-part message in MIME format.\n\n" .
                "--{$mime_boundary}\n" .
                "Content-Type: text/plain; charset=\"iso-8859-1\n" .
                "Content-Transfer-Encoding: 7bit\n\n" .
                $mainMessage . "\n\n";

        $data = chunk_split(base64_encode($data));
        $message .= "--{$mime_boundary}\n" .
                "Content-Type: {$fileatttype};\n" .
                " name=\"{$fileattname}\"\n" .
                "Content-Disposition: attachment;\n" .
                " filename=\"{$fileattname}\"\n" .
                "Content-Transfer-Encoding: base64\n\n" .
                $data . "\n\n" .
        if (mail($to, $subject, $message, $headers)) {
            echo "The email was sent.";
        } else {
            echo "There was an error sending the mail.";

Check the headers and the content of each version carefully, to see where they differ.

Above there are two tests:

pure php, the attachment appears on the iPhone
cakephp 4.1, attachment does not appear on iPhone

Could any of the DEV help me with version 4.1?

I see your code, but it’s hard to parse through all of that to see what might actually differ in what’s going to the SMTP server. You are the only one that has the ability to look at the end result of what’s being generated, by examining the headers and “view source” of the emails that are actually sent. By comparing the email that works to the email that doesn’t, we can work backwards to determine what aspect of that output is causing the issue, and hence to determine the issue in the code that is responsible for that.

is the filepath correct? file.pdf seem like an odd filepath.

yes, the filepath is correct.
It’s a small PDF file, written test on it, could someone who has an iPhone do this test and give me feedback if they can send it?
Or as a solution, you shouldn’t use cakephp to send the file by email and use pure php?

Lots of people are using Cake for sending email with attachments. If you don’t want to do the debugging work that I’ve already suggested (twice), I can’t help you fix your implementation.

cakephp doesn’t work on iphone:

Subject: Test CakePHP - subject
From: Test CakePHP - name
Date: Fri, 16 Feb 2024 08:46:58 -0300
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=“c5ae5d55466fa4bef10247f656ce1a0d”
Content-Transfer-Encoding: 8bit

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

Test CakePHP - message

Content-Disposition: attachment; filename=“test.pdf”
Content-Type: application/pdf
Content-Transfer-Encoding: base64


pure working on iphone:

Subject: Test PHP-Pure - subject
From: Test PHP-Pure - name
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=“rel_31f6dd577335e45ab91de9b89b4fc2f2x”
Content-Transfer-Encoding: 8bit
Date: Fri, 16 Feb 2024 09:13:44 -0300 (-03)

Content-Type: multipart/alternative; boundary=“rel_31f6dd577335e45ab91de9b89b4fc2f2z”

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

Test PHP-Pure - message

Content-Disposition: attachment; filename=“test.pdf”;
Content-Type: application/pdf; name=“test.pdf”;
Content-Transfer-Encoding: base64


The “pure” output has a Content-Type: multipart/alternative line in it, which I don’t see anything about in your code. Is that happening from PHP mail function, or do you have a different version of the code generating that?

Anyway, that seems to be the main difference here. Looking at Cake’s code, it seems that this happens only when you send the message in both HTML and text formats. Maybe give that a try and see if it fixes it?