【问题标题】:PHP PEAR Mail and Mail_mime adding attachments to email send with SMTPPHP PEAR Mail 和 Mail_mime 添加附件到使用 SMTP 发送的电子邮件
【发布时间】:2014-09-04 14:34:14
【问题描述】:

我在使用 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--

我希望收到一封电子邮件,正文为 &lt;div&gt;this is a test&lt;/div&gt; 或“Ceci est un grand grand test”,并附有名为 test.txt 的文本文件。

编辑: 删除错误抑制运算符后,我没有收到任何警告或错误。

编辑: 我有一个闪光,我更改了 foreach 循环添加一个 If 语句,以查看附件是否正确附加,并且它没有抛出任何错误,似乎正确附加。

回声结果:

Att: test.txt
1

【问题讨论】:

  • 第一步是在任何地方删除@。使用错误抑制算子相当于把你的手指伸进你的耳朵,然后说“lalalalala 听不到你的声音”。但是鉴于您的示例文本,您的标头没有正确构建,并且它们都在同一行文本上,这意味着它们已损坏并且根本不会被邮件客户端解释。
  • 将删除错误抑制运算符,如果我有错误消息,请回复您。
  • 完成我删除了@ 并将结果发布在编辑中
  • 该行没有任何问题,因此您可能在粘贴代码之前的某处有一个悬空的'
  • 刚刚看到这是一个我已经解决的老错误...

标签: php email


【解决方案1】:

这是发生了什么:

我搜索了 Mail_mime 文档,发现了这个:

切勿尝试以相反的顺序调用这些线路!

我在这里找到的:(注:Paul)

http://pear.php.net/manual/en/package.mail.mail-mime.example.php

问题是 $mime-&gt;get();$mime-&gt;headers($header); 必须按此顺序调用 get() 首先和 Headers() 其次。

【讨论】:

    猜你喜欢
    • 2010-12-02
    • 2015-07-08
    • 1970-01-01
    • 2018-05-13
    • 2012-08-01
    • 2012-05-29
    • 2018-06-10
    • 2015-09-30
    • 2012-02-28
    相关资源
    最近更新 更多