1

I use phpmail script to send attachments from site to e-mail. The script itself works. I noticed that e.g. in the case of Gmail messages are coming but without attachments. Instead, in addition to the content, there is a very long message containing only the hash string.

In addition, not all mailboxes display content correctly. In some cases, the content also adds, for ex.:

--PHP-alt-41babb7e7ab4c64e3af6ea533924bebdContent-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit

and the html coding in plain text.

Can it be standardized so as not to display unnecessary information?

Here's my code:

<?php
$to = $_POST['email'];
$subject = 'Attachment from site domain.com';
$subject = sprintf("=?utf-8?B?%s?=", base64_encode($subject));
$random_hash = md5(uniqid(time()));

$headers = "From: MySite <site@domain.com>\r\nReply-To: site@domain.com";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

$filename = $_POST['file_id'] . ".pdf";
$path = "/home/ftp/wp-content/uploads";
$file = $path . "/" . $filename;

// read file into $data var
$f = fopen($file, "rb");
$data = fread($f,  filesize( $file ) );
fclose($f);

$attachment = chunk_split(base64_encode($data));

ob_start();
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit

Hello,
Here the mail text.

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 8bit

<h2>Hello,</h2>
<p>Here's the mail text</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?>
Content-Transfer-Encoding: base64
Content-Type: application/pdf; name="<?php echo $filename; ?>"
Content-Disposition: attachment

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
$message = ob_get_clean();
if (@mail($to, $subject, $message, $headers )) {
    echo "<p><b>File send!</b></p> Check your e-mail.";
} else {
    echo "<p><b>Error!</b></p> File not send.";
}
?>

What can I change to make delivery with an attachment work for all mailboxes?

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
Pat
  • 697
  • 3
  • 11
  • 30
  • Getting this to work for all mail clients can be difficult. It could, for instance, be that you have a "line-return" to many, or to few, somewhere. Most people therefore use an existing PHP library to send attachment. Like [PHPMailer](https://github.com/PHPMailer/PHPMailer). Mind you, getting email accepted that was sent directly from your own server is also getting more and more difficult. You have to set up stuff like SPF, DKIM, DMARC, and more. Nowadays I use a service to sent emails. There are many, but I like [Postmark](https://postmarkapp.com). – KIKO Software Sep 30 '19 at 08:15
  • This script belongs to the PHPmailer libraries. It is only used to send the PDF attachment on request to the email address. I think that using an external solution here is disproportionate to the needs. Does anyone have a different solution? – Pat Sep 30 '19 at 21:03
  • Have you considered using Gmail API for sending emails with attachments? https://developers.google.com/gmail/api/quickstart/php – ziganotschka Oct 02 '19 at 14:43
  • @ziganotschka Yes, I considered it so. Unfortunately, in this particular case it is not possible to implement. – Pat Oct 03 '19 at 08:52

0 Answers0