[英]PHP PEAR Mail and Mail_mime adding attachments to email send with SMTP
我在發送帶有PHP PEAR Mail和Mail_mime及其附件的電子郵件時遇到問題。
問題不在於發送電子郵件本身,就是發送的電子郵件恰好就是所產生的電子郵件是亂碼,如字符混合。
這是我的test.php的內容(我刪除了該問題不想要的內容):
<?php
//All the custom classes needed for the whole code but I dont use them all for this exemple.
use Framework\cUsager;
use Framework\cConnexion;
use Framework\cPHPMailer;
use Framework\cLog;
use Framework\cFileStream;
use Framework\gConfig as g;
require_once "Config.php";
$HTML = "<div>This is a test</div>";
$Text = "Ceci est un grand grand test";
$mail = new cPHPMailer
(
'To@exemple.com',
'From@exemple.com',
'Test',
'test.txt',//this is a text file a want to send as attachment, its save in the same directory as test.php
'1.0',
'text/html; charset=UTF-8',
$HTML,
$Text
);
$mail->Mail();
?>
這是cPHPMailer的代碼:
<?php
namespace Framework;
header('Content-Type: text/html; charset=UTF-8');
require_once "Mail.php";
require_once "Mail/mime.php";
class cPHPMailer
{
//I skipped all the getters and setters as well as the __construct and the attributs you guys can guess that $this->getTo() return to the To attributes in my class.
public function Mail()
{
$header = array
(
"To" => $this->getTo(),
"From" => $this->getFrom(),
"Subject" => $this->getSubject(),
"MIME-Version" => $this->getMimeVersion(),
"Content-Type" => $this->getContentType()
);
$mime = new \Mail_mime();
$mime->setTXTBody($this->getText());
$mime->setHTMLBody($this->getHTML());
foreach($this->getAttachments() as $att)
{
if($mime->addAttachment($att))
{
echo "Att: ".$att."<br>";
}
else
{
echo "TEST FAILED!!!!!";//THIS IS EDIT #2
}
}
$smtp = \Mail::factory
(
'smtp',
array
(
'host' => gConfig::$SMTPMailHost,//those are store in the Config.php file in test.php (see code above)
'port' => gConfig::$SMTPMailPort,
'auth' => gConfig::$SMTPMailAuth,
'username' => gConfig::$SMTPMailUser,
'password' => gConfig::$SMTPMailPass
)
);
$envoyer = $smtp->send($this->getTo(),$mime->headers($header),$mime->get());
echo $envoyer;//this return true/1 meaning that no errors occured
}
}
?>
這是運行腳本時收到的電子郵件的內容:
--=_88e78c51e29d6b210a754d69484afcbc Content-Type: multipart/alternative; boundary="=_2ceac717c96dcbe1a49c37c533389352" --=_2ceac717c96dcbe1a49c37c533389352 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=ISO-8859-1 Ceci est un grand grand test --=_2ceac717c96dcbe1a49c37c533389352 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1 This is a test --=_2ceac717c96dcbe1a49c37c533389352-- --=_88e78c51e29d6b210a754d69484afcbc Content-Transfer-Encoding: base64 Content-Type: application/octet-stream; name=test.txt Content-Disposition: attachment; filename=test.txt Y2VjaSBlc3QgdW4gdGVzdA== --=_88e78c51e29d6b210a754d69484afcbc--
我希望收到一封帶有<div>this is a test</div>
或“ Ceci est un grand grand test”的正文的電子郵件,其中帶有名為test.txt的文本文件。
編輯 :刪除錯誤抑制運算符后,我沒有收到任何警告或錯誤。
編輯 :我有一個快閃,我更改了foreach循環,添加了一個If語句,以查看附件是否正確連接,並且它不會拋出似乎正確連接的任何錯誤。
回聲的結果:
Att: test.txt
1
這是發生了什么:
我搜索Mail_mime文檔並發現了這一點:
請勿嘗試以相反順序致電這些行!
我在這里找到它(注:保羅)
http://pear.php.net/manual/en/package.mail.mail-mime.example.php
問題是$mime->get();
和$mime->headers($header);
必須先按此順序調用get()
然后再調用Headers()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.