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

时间:2015-06-04 05:28:26

标签: 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 = $_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);

1 个答案:

答案 0 :(得分:0)

我更喜欢将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);
?>