【发布时间】: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”,它运行良好。谢谢