我在发送带有附件的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
结果如下:
我搜索了Mail_mime文档,发现了这个:
永远不要试图以相反的顺序调用这些行!
我发现它在这里:(注:Paul)
http://pear.php.net/manual/en/package.mail.mail-mime.example.php问题是$mime->get();
和$mime->headers($header);
必须按这个顺序调用:get()
先,Headers()
后。