我一直在使用PHP中添加的PEAR来发送邮件,因为我可以更好地控制它的工作。它需要包含以下两个部分:
require "Mail.php";
require "Mail/mime.php";
我通常在这两条注释之后从表单中执行所有处理,然后使用以下代码集将它们组合在一起:
$server = "mail.myserver.com";
$username = "myusername@myserver.com";
$password = "thisIsAPassword";
$port = "587";
// Set Up The Mail Headers
$hdrsUs = array(
"From" => $sender,
"To" => $receiverUs,
"Subject" => $subjectUs,
"Return-Path" => $sender
);
// Configure the Mailer Mechanism
$smtp = Mail::factory("smtp",
array(
"host" => $server,
"username" => $username,
"password" => $password,
"auth" => true,
"port" => $port
)
);
$textUs =
"This is a text version of the e-mail.";
$htmlUs =
"
This is the HTML version of the e-mail.
";
$mimeUs = new Mail_mime($crlf);
$mimeUs->setTXTBody($textUs);
$mimeUs->setHTMLBody($htmlUs);
// Do not EVER Try to Call These Lines in Reverse Order
$bodyUs = $mimeUs->get();
$hdrsUs = $mimeUs->headers($hdrsUs, true);
// Send the Message
$mailUs = $smtp->send($receiverUs, $hdrsUs, $bodyUs);
if (PEAR::isError($mailUs))
{
echo ($mailThem->getMessage());
}
else
{
echo "
}