php 在 phpmailer 中添加 HTML 格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13789989/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:04:10  来源:igfitidea点击:

Add HTML formatting in phpmailer

phpphpmailer

提问by TheLettuceMaster

I am using PHP mailer to send an online form directly to email. I want to edit the form like this:

我正在使用 PHP 邮件程序将在线表单直接发送到电子邮件。我想像这样编辑表单:

$message = '<p>The following request was sent from: '</p>;
$message .= '<p>Name: '.$name.'</p><br />';
$message .= '<p>Phone: '.$phone.'</p><br />';
$message .= '<p>Email: '.$email.'</p><br />';

However, in the email I receive back, I get the <p>, <br />, etc. Not the formatting I want, but the actual HTML. It has always worked for me, then I realized I was using the stock PHP mail function. not sure if PHP mailer has different parameters OR if I just missing small here?

然而,在我收到回电子邮件,我得到的<p><br />等不格式化我想要的,但实际的HTML。它一直对我有用,然后我意识到我正在使用股票 PHP 邮件功能。不确定 PHP 邮件程序是否有不同的参数,或者我是否只是在这里遗漏了小?

Where would I set the headers for text/html?

我应该在哪里设置 text/html 的标题?

    $mail = new PHPMailer();
    $mail -> IsSMTP();
    $mail -> Host = "---";
    $mail -> Port = 25;
    $mail -> SMTPAuth = false;
    $mail -> Username = EMAIL_USER;
    $mail -> Password = EMAIL_PASS;
    $mail -> FromName = $from_name;
    $mail -> From = $from;
    $mail -> Subject = $subject;
    $mail -> Body = $message;
    $mail -> AddAddress("---");

    $result = $mail -> Send();

回答by GBD

In PHP mailer, you need to set below

在PHP邮件程序中,您需要在下面设置

$mail->IsHTML(true);

Note: $mailmeans your PHPMailer object.

注意:$mail表示您的 PHPMailer 对象。

Reference Link: PHPMailer

参考链接:PHPMailer

回答by Praveen Kumar Purushothaman

By default, the PHP mail()function is text/plain.

默认情况下,PHPmail()函数是text/plain.

In your mail()headers, change the content-typeto text/htmland try.

在您的mail()标题中,更改content-typetext/html并尝试。

Example:

例子:

<?php 
    $body = "<html>\n"; 
    $body .= "<body style=\"font-family:Verdana, Verdana, Geneva, sans-serif; font-size:12px; color:#666666;\">\n"; 
    $body = $message; 
    $body .= "</body>\n"; 
    $body .= "</html>\n"; 

    $headers  = "From: My site<[email protected]>\r\n"; 
    $headers .= "Reply-To: [email protected]\r\n"; 
    $headers .= "Return-Path: [email protected]\r\n"; 
    $headers .= "X-Mailer: Drupal\n"; 
    $headers .= 'MIME-Version: 1.0' . "\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

    return mail($recipient, $subject, $message, $headers); 
?>

PHP Mailer:

PHP邮件程序:

You need to set IsHTML(true):

您需要设置IsHTML(true)

$mail->IsHTML(true);