问题
I am sure most of you have already done with kind of thing but i am unable to use correct charset for subject with html format on this code for phpmailer somehow eventhough i've tried hundereds of way. Could you guys catch what could be wrong with the code below?
If i replace "Content-Type: text/html" to "Content-Type: text/plain" then utf8 works fine for only message body but not for subject still gets bad header warning besides mail does not seem as html format either. And what should i write to use an external mail server into this code ?
$message = "Hi $uname,\r\n\r\n\r\n";
$message .= "$passwordLink\r\n\r\n\r\n";
$message .= "you just hit password recovery request\r\n;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Password Service <service@domain.com> \n";
$headers .= "To-Sender: \n";
$headers .= "X-Mailer: PHP\n";
$headers .= "Reply-To: noreply@domain.com\n";
$headers .= "Return-Path: noreply@domain.com\n";
$headers .= "Content-Type: text/html; charset=utf-8";
$host = "mailgw.domain.com";
$subject = "Pass reminder service";
@mail($email,$subject,$message,$headers);
return str_replace("\r\n","<br/>",$message);
Thanks in advance,
回答1:
Pass your $message and $subject through this function http://php.net/manual/en/function.utf8-encode.php.
To use external server I would recommend to use PHPMailer class https://github.com/PHPMailer/PHPMailer, so you could do something like this:
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
...
来源:https://stackoverflow.com/questions/25781544/how-to-use-phpmailer-with-utf8-with-external-host