evinci Posted October 3, 2019 Share Posted October 3, 2019 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.: Quote --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? Quote Link to comment https://forums.phpfreaks.com/topic/309322-phpmail-with-attachment-doesnt-reach-gmail-formatting-errors/ Share on other sites More sharing options...
ginerjm Posted October 3, 2019 Share Posted October 3, 2019 Why don't you simply download the PHPMailer app and use that? SOOOOO much easier to send mail with attachments. Quote Link to comment https://forums.phpfreaks.com/topic/309322-phpmail-with-attachment-doesnt-reach-gmail-formatting-errors/#findComment-1570253 Share on other sites More sharing options...
evinci Posted October 3, 2019 Author Share Posted October 3, 2019 I need to use it in Wordpress. I don't need to implement the entire system to send a single attachment. I managed to slightly modify the above and the attachment is already visible. The problem that when you try to preview or save pdf file is corrupt - does not work. What could be the reason? Quote Link to comment https://forums.phpfreaks.com/topic/309322-phpmail-with-attachment-doesnt-reach-gmail-formatting-errors/#findComment-1570258 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.