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

使用php mail()函数发送电子邮件附件-适用于gmail,但对于其他邮件提供商,附件的大小为空(0字节),php,email,tcpdf,Php,Email,Tcpdf,我正在使用php mail()函数发送带有附件的电子邮件 它适用于gmail,但雅虎的附件大小为空(0字节) 以下是我使用的附件代码: $attachment = $pdf->Output(" ", "S"); $attachment_final = chunk_split(base64_encode($attachment)); $email_to = $_SESSION['flyer_data']['download_email']; $email_from = $_SESSI

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

它适用于gmail,但雅虎的附件大小为空(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);
$attachment=$pdf->Output(“,”S”);
$attachment_final=块分割(base64_编码($attachment));
$email\u to=$\u会话['flyer\u数据]['download\u email'];
$email\u from=$\u会话['flyer\u data']['sender\u email'];
$email_subject='subject';
$email_txt=“检查传单的附件。

”; $fileatt_name=$output_file_name; $file=fopen($fileatt_name,'rb'); $data=fread($file,filesize($fileatt_name)); fclose($文件); $separator=md5(time()); $mime_boundary=“==Multipart_boundary_x{$separator}x”; $headers=“From:$email\u From”//电子邮件的发件人(示例) $headers.=“\n时间版本:1.0\n”。 “内容类型:多部分/混合;\n”。 “边界=\”{$mime\u boundary}\”; $email_message.=“这是一封MIME编码的邮件。\n\n”。 “{$mime\U边界}\n”。 “内容类型:text/html;字符集=\”iso-8859-1\“\n”。 “内容传输编码:8位\n\n”$电子邮件。“\n\n”; $data=$attachment\u final; $email\u message.=“--{$mime\u boundary}\n”。 “内容类型:application/pdf;name=\”{$fileatt\u name}\“\n\n”。 “内容传输编码:base64\n\n”。 “内容处置:附件\“{$data}\”\n\n”。 “{$mime\u boundary}--\n”; //发送消息 邮件($email\u to、$email\u subject、$email\u message、$headers);
我更喜欢使用HTML来实现这些目的。您可以使用电子邮件中的锚标记提供服务器上文件的链接

要在电子邮件中写入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);
?>

我更喜欢使用HTML来实现这些目的。您可以使用电子邮件中的锚标记提供服务器上文件的链接

要在电子邮件中写入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);
?>