通过PHP发送邮件(带附件、HTML等)

用PHP发邮件,可以走sendmail,但是MIME方面,如果用内置的,非常麻烦。

我使用了SwiftMail

有兴趣的可以看看他们的文档,支持SMTP、Sendmail,对MIME的支持也很简单很强大,支持附件、html等。

一个例子:

<?php
require_once './lib/swift_required.php';

//E-Mail
$mail_title = "I'm Subject";
$mail_content = "I'm Content";
$mail_sender = "sender@qq.com";
$mail_recver = "recever@vip.qq.com";
$mail_body = "I'm Body";
$mail_file = "./tb.php";
$sendmail_cmd = "/usr/sbin/sendmail -bs";

//Create E-Mail
$message = Swift_Message::newInstance();
$message->setSubject($mail_title);
$message->setFrom(array($mail_sender));
$message->setTo(array($mail_recver));
$message->setBody($mail_body);
$message->attach(Swift_Attachment::fromPath($mail_file));
//echo $message->toString();

//Send E-Mail
//echo system("which sendmail")
$transport = Swift_SendmailTransport::newInstance($sendmail_cmd);
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);

?>

Leave a Reply

Your email address will not be published.