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 = "test@test.com";
$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;
}
3 个解决方案
#1
23
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);
#2
10
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);
$address = "test@test.com";
$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;
}
#3
0
Try below code which works fine:
尝试下面的代码工作正常:
try {
$mail->AddAddress('jitpal@domain.com', 'Jit Pal');
$mail->SetFrom('testuser@domain.com', '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();
}
#1
23
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);
#2
10
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);
$address = "test@test.com";
$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;
}
#3
0
Try below code which works fine:
尝试下面的代码工作正常:
try {
$mail->AddAddress('jitpal@domain.com', 'Jit Pal');
$mail->SetFrom('testuser@domain.com', '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();
}