当使用 PHP 发送电子邮件时,我一直使用一个使用 STMP 的自定义函数,并且在该函数中,它使用以下标头来设置我相信您尝试设置的名称:
"To: $nameto <$emailto>"
其中 $emailto 是您要发送到的实际电子邮件,而 $nameto 是您希望它显示给谁。
这是我使用的函数,如果您需要更改任何其他标题,您可以修改它们,它们在函数的末尾:
/*
Desc: Used to send emails from stmp server. Make sure to have the config variables set (at the top of function). Stores logs in array, you'll need to modify the function to actually use the array though.
Params:
String $from - Email address that the email is from. Ex. "john.smith@example.com"
String $namefrom - Name to go along with the email address. Ex. "John Smith"
String $to - Email address of the recipient of the email. Ex, "jane.doe@example.com"
String $nameto - Name to go along with the email address. Ex. "Jane Doe"
String $subject - Email subject.
String $message - Main contents of email.
Return: true on success false on failure.
*/
function amail($from, $namefrom, $to, $nameto, $subject, $message)
{
$smtpServer = "";
$port = 0;
$username = "";
$password = "";
$timeout = 30;
$localhost = "127.0.0.1";
$newLine = "\r\n";
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout) or die('Could not connect.');
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect)){
return false;
} else {
$logArray['connection'] = "Connected: $smtpResponse";
}
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailfromresponse'] = "$smtpResponse";
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/plain; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;
$headers .= "Subject: $subject" . $newLine;
fputs($smtpConnect, "$headers\n\n$message\n".$newLine.".".$newLine);
$smtpResponse = fgets($smtpConnect, 1024);
$logArray['data2response'] = "$smtpResponse";
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
return true;
}