发送带有html代码的phpmailer电子邮件

发送带有html代码的phpmailer电子邮件,php,email,phpmailer,Php,Email,Phpmailer,我制作了一个发送邮件的函数,但它们解释html代码。如何在正文中发送准确的html代码?例如,用户应该收到文本,而不是粗体的“strong” function sendmail($dest,$subj ,$htmlmessage,$textmessage) { $Mail = new PHPMailer( true ); try{ $Mail->IsSMTP(); // Use SMTP $Mail->SMTPAuth = TRUE;

我制作了一个发送邮件的函数,但它们解释html代码。如何在正文中发送准确的html代码?例如,用户应该收到
文本
,而不是粗体的“strong”

function sendmail($dest,$subj ,$htmlmessage,$textmessage) {
    $Mail = new PHPMailer( true );
    try{

        $Mail->IsSMTP(); // Use SMTP
        $Mail->SMTPAuth = TRUE; // enable SMTP authentication
        //$Mail->SMTPDebug = 2; // 2 to enable SMTP debug information

        $Mail->Host = "smtp.gmail.com"; // Sets SMTP server
        $Mail->Username = 'mymail@gmail.com'; // SMTP account username
        $Mail->Password = 'mypassword'; // SMTP account password
        $Mail->SMTPSecure  = "ssl"; //Secure conection
        $Mail->Port = 465; // set the SMTP port

        $Mail->SetFrom ( 'mymail@gmail.com', 'def' );
        $Mail->FromName    = 'myname';
        //$Mail->addReplyTo( 'mymail@gmail.com', 'Reply here' );

        $Mail->addAddress($dest, 'to' ); // To:
        $Mail->isHTML( false );

        $Mail->Subject=$subj;
        $Mail->Body=$htmlmessage;
        $Mail->AltBody=$textmessage;

        $Mail->Send();
    }
    catch ( phpmailerException $e ) {

        file_put_contents( 'Mailerrors.txt', $e->errorMessage() , FILE_APPEND );
        die( "Problem with emailing." );
    }
}
我在这里寄信

$dest='sendto@gmail.com';
$subj='hello';
$htmlmessage='this is the body<strong>asd</strong>';
$textmessage='this is the body';

sendmail($dest,$subj, $htmlmessage,$textmessage);
$dest='1!'sendto@gmail.com';
$subc='hello';
$htmlmessage='这是主体asd';
$textmessage='this is the body';
sendmail($dest、$subc、$htmlmessage、$textmessage);

请注意,该函数确实起作用并向我发送电子邮件,只是它们解释html代码。

我做到了。我所要做的就是:

 $Mail->Body=htmlspecialchars($htmlmessage);

不确定这是否是个好主意,即使它有效。我相信一定还有别的办法。

有几种可能的解决办法

要仅发送纯文本,请执行以下操作:

$mail->isHTML(false);
并且不要在
$mail->AltBody
中放入任何内容。这样,消息将以纯文本形式发送,即使它包含HTML标记

如果只希望消息正文的一部分转义HTML呈现,则可以使用
htmlspecialchars
或将标记的该部分包装在
标记中


htmlspecialchars
应用于整个消息正文有点毫无意义,因为它获得了与
isHTML(false)
相似的外观结果,但效率要低得多。

设置
$Mail->isHTML(true)仍然将代码解释为HTML。请尝试
$htmlmessage='这是正文asd'仍然可以解释它。