PHP PEAR Mail和Mail_mime通过SMTP向电子邮件发送添加附件
我在用PHP PEAR Mail和Mail_mime发送电子邮件时遇到问题 问题不在于发送电子邮件本身,而是电子邮件发送得很好,只是生成的电子邮件是一堆乱七八糟的字符 下面是我的test.php的内容(我删除了问题中未指定的内容): 以下是发生的情况: 我搜索了Mail_mime文档,发现: 永远不要尝试以相反的顺序调用这些行强> 我在这里找到的:(注:保罗)PHP PEAR Mail和Mail_mime通过SMTP向电子邮件发送添加附件,php,email,Php,Email,我在用PHP PEAR Mail和Mail_mime发送电子邮件时遇到问题 问题不在于发送电子邮件本身,而是电子邮件发送得很好,只是生成的电子邮件是一堆乱七八糟的字符 下面是我的test.php的内容(我删除了问题中未指定的内容): 以下是发生的情况: 我搜索了Mail_mime文档,发现: 永远不要尝试以相反的顺序调用这些行 我在这里找到的:(注:保罗) 问题是$mime->get()和$mime->headers($header)第一步是get()第二步是Headers()第二步。第一步是
问题是
$mime->get()代码>和$mime->headers($header)必须按照以下顺序调用代码>第一步是get()
第二步是Headers()
第二步。第一步是删除@
。使用错误抑制操作符相当于将手指插入耳朵,然后说“拉拉听不见”。但是给定示例文本,标题的构造不正确,它们都位于同一行文本上,这意味着它们已损坏,邮件客户端根本不会对其进行解释。要删除错误抑制运算符,并在有错误消息的情况下返回给您。完成后,我删除了@
,并将结果发布在编辑中该行没有问题,因此,在粘贴的代码之前,可能有一个悬空的“
。刚才看到这是一个我已经解决的旧错误。。。
<?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();
?>
<?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
}
}
?>
Att: test.txt
1