使用 php mail() 函数的电子邮件附件 - 适用于 gmail,但其他邮件提供商的附件大小为空(0 字节)

Email Attachments using php mail() function - Working for gmail but attachments are of null size (0 bytes) for other mailing provider

我正在使用 php mail() 函数发送带附件的电子邮件。

它适用于 gmail,但附件对于 yahoo 来说是空大小(0 字节)。

这是我用过的附件代码:

$attachment = $pdf->Output(" ", "S");
 $attachment_final = chunk_split(base64_encode($attachment));
 $email_to = $_SESSION['flyer_data']['download_email']; 
 $email_from = $_SESSION['flyer_data']['sender_email']; 
 $email_subject = 'Subject'; 
 $email_txt = "<p>Check attachment for flyer.</p>"; 

 $fileatt_name = $output_file_name; 
 $file = fopen($fileatt_name,'rb');
 $data = fread($file,filesize($fileatt_name));
 fclose($file);
 $separator = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$separator}x";
    $headers="From: $email_from"; // Who the email is from (example)
    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";
    $email_message .= "This is a MIME encoded message.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 8bit\n\n" . $email_txt . "\n\n";
    $data = $attachment_final;
    $email_message .= "--{$mime_boundary}\n" .
    "Content-Type: application/pdf; name=\"{$fileatt_name}\"\n\n".  
    "Content-Transfer-Encoding: base64\n\n" .
    "Content-Disposition: attachment\"{$data}\"\n\n" .
    "--{$mime_boundary}--\n";
        // send message

        mail($email_to,$email_subject,$email_message,$headers);

出于此类目的,我更喜欢使用 HTML。您可以使用电子邮件中的锚标记将 link 提供给您服务器上的文件。

要在电子邮件中写入 HTML:

<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<a href='./unknown.pdf'>Download</a>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
 </body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

mail($to,$subject,$message,$headers);
?>