簡體   English   中英

在PHP中通過郵件表發送附件和消息

[英]Send attachment and message using table by mail in php

我想使用HTML表格在郵件正文中附加一個生成的pdf文件和一封謝謝消息,其中包含pdf生成的內容。 附加和郵寄文件可以正常工作。 但是,感謝消息和詳細信息不會顯示在郵件正文中。 這是代碼。

$pdf->Output('abc-REG.pdf', 'I');
$to = 'abc@gmail.com, ' . $email;
$subject = 'ABC :: ACTIVATION';
$repEmail = 'abc@gmail.com';
$fileName = 'abc-REG.pdf';
$fileatt = $pdf->Output($fileName, 'E');
$attachment = chunk_split($fileatt);
$eol = PHP_EOL;
$separator = md5(time());
$headers = 'From: ABC <'.$repEmail.'>'.$eol;
$headers .= 'MIME-Version: 1.0' .$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$message = "--".$separator.$eol;
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$message .= '<html><body>';
$message .= '<img src="http://google.com/images/logo.png"  /><br/>';
$message .= "Thanks for Activation. Your online registration number is A0". mysql_insert_id() ". "
    $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Mobile:</strong> </td><td>" . strip_tags($_POST['mobile']) . "</td></tr>";
$message .= "<tr><td><strong>Organisation Name:</strong> </td><td>" . strip_tags($_POST['cname']) . "</td></tr>";
$message .= "<tr><td><strong>Address:</strong> </td><td>" .  strip_tags($_POST['Address']) . "</td></tr>"; 
$message .= "</table>";
$message .= "</body></html>".$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; 
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment".$eol.$eol;
$message .= $attachment.$eol;
$message .= "--".$separator."--";
if (mail($to, $subject, $message, $headers)){
    echo "Email sent";
}
else {
    echo "Email failed";
}

好像由於缺少時間段而缺少第一個分隔符:

$message = "--".$separator.$eol; <--- This separator 

$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;  <--- and this encoding is dropped

$message = '<html><body>';  <--- due to missing period here (= not .=)

$message .= '<img src="http://google.com/images/logo.png"  /><br/>';

編輯

好的,似乎在內容類型為HTML的適當位置也缺少了HTML:

...
$headers .= 'MIME-Version: 1.0' .$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$message = "--".$separator.$eol;
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$message .= '<html><body>';
$message .= '<img src="http://google.com/images/logo.png"  /><br/>';
$message .= "Thanks for Activation. Your online registration number is A0". mysql_insert_id() ". "
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Mobile:</strong> </td><td>" . strip_tags($_POST['mobile']) . "</td></tr>";
$message .= "<tr><td><strong>Organisation Name:</strong> </td><td>" . strip_tags($_POST['cname']) . "</td></tr>";
$message .= "<tr><td><strong>Address:</strong> </td><td>" .  strip_tags($_POST['Address']) . "</td></tr>"; 
$message .= "</table>";
$message .= "</body></html>".$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;

//missing content here

$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; 
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment".$eol.$eol;
...

像這樣:

...
$headers .= 'MIME-Version: 1.0' .$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$message = "--".$separator.$eol;
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= '<html><body>';
$message .= '<img src="http://google.com/images/logo.png"  /><br/>';
$message .= "Thanks for Activation. Your online registration number is A0". mysql_insert_id() ". "
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Mobile:</strong> </td><td>" . strip_tags($_POST['mobile']) . "</td></tr>";
$message .= "<tr><td><strong>Organisation Name:</strong> </td><td>" . strip_tags($_POST['cname']) . "</td></tr>";
$message .= "<tr><td><strong>Address:</strong> </td><td>" .  strip_tags($_POST['Address']) . "</td></tr>"; 
$message .= "</table>";
$message .= "</body></html>".$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; 
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment".$eol.$eol;
...

順便說說? 您是否需要7位編碼部分? 也許將其標記為內容類型文本/純文本,並提供郵件的無標記純文本版本? 您會驚訝地發現人們可能不支持HTML電子郵件,或者不支持HTML電子郵件。

編輯2

只是注意到您正在使用PHP_EOL作為行尾。 如果這是從非Windows系統發送的,則可能會得到錯誤的行尾。 "\\r\\n"分隔行。

還可以嘗試將此行設置為正文的第一行,而不是標題行:

$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

更改為:

$message = "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
...

您可能還需要混合使用內容類型和替代內容類型。 看到這里: http : //webcheatsheet.com/php/send_email_text_html_attachment.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM