【问题标题】:how to send attachment with pear smtp如何使用梨 smtp 发送附件
【发布时间】:2016-06-12 04:54:51
【问题描述】:

我想使用 pear smtp 发送带有电子邮件的文件。电子邮件已成功发送至电子邮件。还有一个附件与电子邮件一起发送,但其中没有名称且没有内容。当我下载附件时,它只有一行 “这是一个 MIME 格式的多部分消息...”。

我不明白它有什么问题。我已经附上了这个问题的代码,我用它来通过电子邮件发送附件。请大家检查一次,让我知道这段代码有什么问题。

提前致谢。

require_once "Mail.php";   
include('Mail/mime.php');

$from = "Dispatcher <server@mymailserver.com>";
$host = "smtp.mymailserver.com";
$username = "mailuser";
$password = "password";
$to = "email@example.com";
$subject = "Subject of the email";

$hdrs = array ('From' => $from,
            'To' => $to,
            'Subject' => $subject,
            );


$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = './test.txt';

$crlf = "rn";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file);

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$smtp = Mail::factory('smtp',
  array ('host' => $host,
        'auth' => true,
        'username' => $username,
        'password' => $password));

$smtp->send($to, $headers, $body);

        if (PEAR::isError($mail)) {
            echo("<p>" . $mail->getMessage() . "</p>");
        } else {
            return 1;
        }

【问题讨论】:

  • 我已经使用 PHP 邮件程序发送邮件和附件.. 一次尝试.. 我可以工作..
  • 您的$crlf 可能不适合作为Mail_mime 构造函数的参数。根据the official document,应该是关联数组。
  • @Passerby 你太棒了,现在我在 $crlf 中使用了“\n”而不是“rn”,它运行良好。谢谢

标签: php email smtp pear


【解决方案1】:

$crlf 表示“回车,换行”,应该是这样。

使用"\n" 使其适用于电子邮件。

【讨论】:

    猜你喜欢
    • 2010-12-30
    • 1970-01-01
    • 2019-05-29
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 2017-06-16
    相关资源
    最近更新 更多