1

this is the code where i send email. the file_get_contents(); i retrieve the content but in plain format text. the css style didnt read.

<?php


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

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

include 'connection.php';
$conn = new Connection();

if(!$conn->connect()) die('Configuration error');
echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."<br />";
$to_decode = json_decode(file_get_contents("php://input"));
$email = $to_decode->email;
$mail = new PHPMailer(true);
$template = "template.php";
$dom_details = array(
    "{my-website}" => "Sample Website",
    "{my-number}" => "Country-State-Local Code-Number",
    "{my-message}" => 'MIKOMIKO'
);
if(file_exists($template))
    $temp_message = file_get_contents($template);
else
    die("Template file not found!");
    
    foreach(array_keys($dom_details) as $key){
        $temp_message = str_replace($key, $dom_details[$key], $temp_message);
    }
try{
    $mail->isSMTP();                                        // Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                   // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                               // Enable SMTP authentication
    //use your own account details
    //if you are using a gmail account, turn on less secured app under security
    $mail->Username   = 'mikocoral11@gmail.com';            // SMTP username
    $mail->Password   = 'ssss';                    // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;     // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('mikocoral11@gmail.com');
    $mail->addAddress($email);     // Add a recipient
    //$mail->addCC('xxxxxx@gmail.com');
    $mail->Subject = 'welcmoe blog to my guys';
    $mail->Body    = $temp_message;
    
    //$mail->IsHTML(true);
    $mail->send();
    echo 'Message Sent';
} catch (Exception $e){
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

?>

this is the content of the template i need to be in the email

<html>
    <head>
    </head>
    <body style="background-color:ddd;">
        <div style="margin: auto; width: 40%; text-align: center; font-size: 20px; padding: 1% 2% 3% 3%; background-color: eee; border-radius: 12px; margin-top: 3%;">
            <h1>Hi {client-name}!</h1><hr /><br />
            <span>Welcome to {my-website}. Your new account comes with access to all features and services.
            To get started, click the button below to active your account.</span>
            <br />
            <br />
            <a href="http://localhost/activities/SystemInteg/include-mailer-email-html-2">
                <button style="padding: 1%; background-color:#045791; border-radius:8px;">
                    <span style="font-size: 20px; color: white;">Activate</span>
                </button>
            </a><br />
            <br />
            <p>For any concerns, kindly call or text us at {my-number}.</p>
            <p>Additional message goes here: Visit {website}</p>
        </div>
    </body>
</html>

this is the email i receieve. as you can see all the element of the html and css style is in plain text format. exactly the same as the template. please help how can i make it work with this codes provided. thank you !

<html>
    <head>
    </head>
    <body style="background-color:ddd;">
            <div style="margin: auto; width: 40%; text-align: center; font-size: 20px; padding: 1% 2% 3% 3%; background-color: eee; border-radius: 12px; margin-top: 3%;">
                    <h1>Hi {client-name}!</h1><hr /><br />
                    <span>Welcome to Sample Website. Your new account comes with access to all features and services.
                    To get started, click the button below to active your account.</span>
                    <br />
                    <br />
                    <a href="http://localhost/activities/SystemInteg/include-mailer-email-html-2">
                            <button style="padding: 1%; background-color:#045791; border-radius:8px;">
                                    <span style="font-size: 20px; color: white;">Activate</span>
                            </button>
                    </a><br />
                    <br />
                    <p>For any concerns, kindly call or text us at Country-State-Local Code-Number.</p>
                    <p>Additional message goes here: Visit {website}</p>
            </div>
    </body>
  • help guys. im struggling hehe. – Mikael Tenshio Apr 29 '22 at 11:27
  • 1
    Maybe you should remove the commenting-out from the line `//$mail->IsHTML(true);` then ...? – CBroe Apr 29 '22 at 11:29
  • this is the error now: SSL loaded
    Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
    – Mikael Tenshio Apr 29 '22 at 13:08
  • That probably because you are trying to send using Gmail, so go check https://stackoverflow.com/questions/3949824/smtp-error-could-not-authenticate-in-phpmailer – CBroe Apr 29 '22 at 13:09
  • The "allow less secure apps" option mentioned in there won't stay enabled permanently though, Google will reset that from time to time. If you don't want to deal with that, then you could use XOAUTH2 as an alternative, https://phppot.com/php/sending-email-using-phpmailer-with-gmail-xoauth2/ – CBroe Apr 29 '22 at 13:11
  • bro thanks you very much itworks now. i forget that i change the password to wrong because i post it here hehe. now when i change it to correct it work now thanks bro for helping me. – Mikael Tenshio Apr 29 '22 at 13:13

1 Answers1

3
 //$mail->IsHTML(true);

uncomment the above, so phpmailer sends html mails

Mels_D
  • 109
  • 7