我无法使用带有附件的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。
修改: 删除错误抑制操作符后,我没有收到任何警告或错误。
修改: 我有一个flash,我更改了foreach循环添加一个If语句,看看附件是否正确连接,并且它没有抛出任何错误,似乎正确连接。
回声的结果:
Att: test.txt
1
答案 0 :(得分:1)
以下是发生的事情:
我搜索Mail_mime文档并找到了:
请勿尝试以反向顺序致电这些线路!
我在这里找到了:(注意:保罗)
http://pear.php.net/manual/en/package.mail.mail-mime.example.php
问题在于$mime->get();
和$mime->headers($header);
必须先按此顺序get()
和Headers()
秒进行调用。