我在使用PHPMailer类发送邮件时,设置邮件名称遇到了一些问题。
我编写了以下函数,以便可以像php内置的
我编写了以下函数,以便可以像php内置的
mail()
函数一样使用。function pmail($to, $subject, $message, $headers = "", $attachments = "")
{
date_default_timezone_set('Europe/London');
require_once($_SERVER['DOCUMENT_ROOT']."/lib/inc/class.phpmailer.php");
//include($_SERVER['DOCUMENT_ROOT']."/lib/inc/class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$defaultEmail = "reply@example.com";
$defaultEmailName = "Web Mailer";
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.example.com"; // SMTP server
$mail->SMTPDebug = false; // enables SMTP debug information (for testing, 1 = errors and messages, 2 = messages only, false = off)
$mail->SMTPAuth = true; // enable SMTP authentication
//$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "mail.example.com"; // sets the SMTP server
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "###"; // SMTP account username
$mail->Password = "###"; // SMTP account password
$mail->SetFrom( ($headers['fromEmail'] != "" ? $headers['fromEmail'] : $defaultEmail), ($headers['fromName'] != "" ? $headers['fromName'] : $defaultEmailName) );
$mail->AddReplyTo( ($headers['replyToEmail'] != "" ? $headers['replyToEmail'] : $defaultEmail), ($headers['replyToName'] != "" ? $headers['replyToName'] : $defaultEmailName) );
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($message);
foreach($attachments as $attachment) {
//$mail->AddAttachment("images/phpmailer.gif"); // attachment example
$mail->AddAttachment($attachment);
}
if(!$mail->Send()) {
//echo "Mailer Error: ".$mail->ErrorInfo;
return false;
} else {
//echo "Message sent!";
return true;
}
}
当使用以下方式进行测试时;
pmail("test@test.com", "test email", "test message here");
一切正常,发件人地址如预期一样显示为reply@example.com
,然而,收件人收到邮件后在收件箱中看到的名称不是Web Mailer
,而是发送邮件所使用用户账户的默认名称。
在邮件头中,发件人名称确实显示为Web Mailer,但我希望在收件箱中看到这个名称。
我们无法在系统上设置更多的用户账户以便创建一个具有所需名称和电子邮件的新用户账户,因此我们必须通过现有用户账户发送邮件。 在这种情况下,邮件会带上我的名称,但我们希望将其显示为Web Mailer。
这是否可能?