我无法设置使用 PHPMailer 类发送邮件的名称。
我编写了以下函数,以便可以以与 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。
这甚至可能吗?