Nancy Moore
Nancy Moore

Reputation: 2470

How to properly send raw html code to gmail using phpmailer in php

Am sending raw code to gmail via phpmailer. when I tried to run the code, the code is formatted in the gmail instead of appearing as raw codes.

I want the code in the message body to appear the way it is in the email as per below

. In order words, how do i make the code to appear as raw codes in gmail

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

below is the code

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;


$email_subject='my codes';
$recipient_email='[email protected]';
$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('[email protected]', "$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}";

}
?>

Upvotes: 0

Views: 235

Answers (1)

Quentin
Quentin

Reputation: 943843

$mail->isHTML(true); // Set email format to HTML

Don't do that.

If you want the message to be displayed as plain text, then don't tell the client that it should be displayed as HTML.

$mail->isHTML(false);

Upvotes: 1

Related Questions