我無法設置與使用PHPMailer類發送的郵件一起發送的名稱。PHPMailer更改發件人名稱
我寫了下面的函數,以便它可以以類似的方式在mail()
函數中使用php的bulit。
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 = "[email protected]";
$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("[email protected]", "test email", "test message here");
一切工作正常時,從地址顯示爲在預期的頭[email protected]
,但是我在收件人的收件箱中看到名字不是Web Mailer
其與用戶衛生組織關聯的默認帳戶憑證用於發送電子郵件。 在標題中,從名稱確實顯示爲網絡郵件程序,但它的收件箱中我想要看到它
我們無法在我們的系統上設置更多用戶帳戶,以允許我們只用一個新帳戶所需的姓名和電子郵件,因此我們必須通過現有的用戶帳戶發送。 在這種情況下,我的電子郵件和我的名字一起發送,但我們希望名稱顯示爲Web Mailer。
這甚至可能嗎?
哈哈我沒有這個,同樣發生在我身上。當使用外部電子郵件地址時,它正確地來了。 –
@MaulikVora煩人的呃?我浪費了這麼多時間。 – Novocaine
是啊@Novocaine,但你救了我在這裏的時間。 ;-) –