php 使用 PHPMailer 发送纯文本电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1124032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending Plain text emails using PHPMailer
提问by Elitmiar
I have a problem sending plain text emails using PHPMailer.
我在使用 PHPMailer 发送纯文本电子邮件时遇到问题。
I have text that I read from a text file and mail it to mail recipient via PHPMailer
我有从文本文件中读取的文本,并通过 PHPMailer 将其邮寄给邮件收件人
When the recipient gets the actual email, the formatting of the mail is not like in the text file, everything is in one line, no new lines and tabs are included in the email that I send. Text wrapping is totally off.
当收件人收到实际电子邮件时,邮件的格式与文本文件中的格式不同,所有内容都在一行中,我发送的电子邮件中没有包含新行和标签。文字环绕完全关闭。
Code:
代码:
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);
$address = "[email protected]";
$mail->AddAddress($address, "John Doe");
$mail->SetFrom(EMAIL_TEST_FROM);
$mail->AddReplyTo(EMAIL_TEST_REPLY);
$mail->Subject = $action." REGISTRATION ".$formName.$tld;
$mail->From = EMAIL_TEST;
$mail->MsgHTML(file_get_contents($newFile));
if($mail->Send()){
return true;
}
回答by bumperbox
You are setting $mail->MsgHTML()to a plain text message, and since whitespace formatting is ignored in HTML, you're getting an inline text.
您设置$mail->MsgHTML()为纯文本消息,并且由于在 HTML 中忽略了空格格式,因此您将获得内联文本。
I haven't used PHPMailer for a while, but from memory try:
我有一段时间没有使用 PHPMailer,但从记忆中尝试:
$mail->Body = file_get_contents($newFile);
回答by elim
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);
$address = "[email protected]";
$mail->AddAddress($address, "John Doe");
$mail->SetFrom(EMAIL_TEST_FROM);
$mail->AddReplyTo(EMAIL_TEST_REPLY);
$mail->Subject = $action." REGISTRATION ".$formName.$tld;
$mail->From = EMAIL_TEST;
// Very important: don't have lines for MsgHTML and AltBody
$mail->Body = file_get_contents($mailBodyTextFile);
// $mail->Body = $_POST["msg"]; //If using web mail form, use this line instead.
if($mail->Send()){
return true;
}
回答by Jitesh Sojitra
Try below code which works fine:
试试下面的代码,它工作正常:
try {
$mail->AddAddress('[email protected]', 'Jit Pal');
$mail->SetFrom('[email protected]', 'Test User');
$mail->Subject = "All machine's tests.";
$mail->Body = "All machine's tests working fine.";
$mail->Send();
echo "<br/>Message sent successfully...<br/><br/>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}

