如何在php中使用phpmailer正确发送原始html代码到gmail



我通过phpmailer发送原始代码到gmail。当我尝试运行代码时,代码被格式化为gmail而不是显示为原始代码。

我希望在消息正文中的代码出现的方式,它是在电子邮件按照下面

。换句话说,我如何使代码显示为gmail中的原始代码

<html>
<script>
alert('This is your Code');
</script>
<body><p>
<b>Display as raw codes.</b>
</p>
</body></html>

下面是代码

<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

$email_subject='my codes';
$recipient_email='reciever@gmail.com';
$recipient_name='Ann';
$sender_name='john';
$mycode = "<html>
<script>
alert('This is your Code');
</script>
<body><p>
<b>Display as raw codes.</b>
</p>
</body></html>";

// Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);
try {


//Server settings goes here

$mail->setFrom('nancy@gmail.com', "$sender_name");
$mail->addAddress($recipient_email, $recipient_name);     // Add a recipient

// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $email_subject;
$mail->Body =  $mycode;
//$mail->AltBody = ""; // for Plain text for non-HTML mails
$mail->send();
echo 'Message successfully sent';
} catch (Exception $e) {
echo "Message not sent. Error: {$mail->ErrorInfo}";
}
?>
$mail->isHTML(true); // Set email format to HTML

别那样做。

如果您希望消息显示为纯文本,那么不要告诉客户端它应该显示为HTML。

$mail->isHTML(false);

最新更新