Phpmailer in a function

Im trying to add a function with PHPmailer without composer.

I get error: Mailer Error: Message body empty

Also how can I have use … in the function?

	use PHPMailer\PHPMailer\PHPMailer;  // script not working without these
	use PHPMailer\PHPMailer\Exception; // script not working without these

function sendMail($sendMailAdress, $sendMailName, $MailSubject, $MailMessage) {

    global $gloSysMailSMTP;
    global $gloSysMailSMTPUser;
    global $gloSysMailSMTPPass;
    global $gloSysMailSMTPPort;
    global $mailSenderAddress;
    global $mailSenderName;
	
	require 'system/PHPMailer/src/Exception.php';
	require 'system/PHPMailer/src/PHPMailer.php';
	require 'system/PHPMailer/src/SMTP.php';
	
	$mail = new PHPMailer;
    $mail->isSMTP(); // Set mailer to use SMTP	
	//$mail->SMTPDebug = 2;
   	$mail->Host = $gloSysMailSMTP;
   	$mail->Port = $gloSysMailSMTPPort;
   	$mail->SMTPAuth = true;
   	$mail->SMTPSecure = "ssl";
   	$mail->Username = $gloSysMailSMTPUser;
   	$mail->Password = $gloSysMailSMTPPass;
   	$mail->setFrom($gloSysMail, $gloSysMailName);
   	//$mail->addReplyTo('[email protected]', 'Your Name');
    $mail->CharSet = 'utf-8';
	$mail->addAddress($sendMailAdress, $sendMailName); // Add a recipient
	$mail->Subject = $MailSubject;
    $mail->MsgHTML = $MailMessage;
     //$mail->msgHTML(file_get_contents('$emailtemplate'), __DIR__);
     //$mail->msgHTML(str_replace('%username%', $username, file_get_contents('system/test.html')), dirname(__FILE__));
    //$mail->Body = $sendMailMessage;
	//$mail->addAttachment('test.txt');

   if (!$mail->send()) {
       echo 'Mailer Error: ' . $mail->ErrorInfo;
   } else {
       echo 'The email message was sent.';
   }
}

$emailtemplate = "templates/mail/test.html"; //THIS IS THE RIGHT PATH

 $mailSenderAddress = '[email protected]'; // NOT SHOWING ACTUAL EMAIL
 $mailSenderName = 'Senders Name';
 $sendMailSubject = 'Subject of the email';

 $sendMailMessage = str_replace(
		array(
		  '%gloDomainLogin%',
		  '%LogoUrl%'
		),
		array(
		   $gloDomainLogin,
		   $gloLogoUrl
		),
		file_get_contents($emailtemplate)
	);

sendMail($mailSenderAddress, $mailSenderName, $sendMailSubject, $sendMailMessage);

Content of test.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>

<body style="color: #000000; font-family: Arial, Helvetica, sans-serif;">
<div style="border: 2px solid #3a3a3a; border-radius: 5px; background: #f5f5f5; padding: 10px; text-align: center; width: fit-content; block-size: fit-content;">

            <img src="%LogoUrl%" alt="image" />
            <p>Welcome, visit this link to login: 
			<a href="https://%gloDomainLogin%" target="_blank" style="color: #b59836;">https://%gloDomainLogin%</a>.</p>

</div>
</body>
</html>

It works when I change $mail->MsgHTML = $MailMessage;
to
$mail->IsHTML(true);
$mail->Body = $MailMessage;

Anyone knows why?

Seeing this late, and you probably already figured it out.

It is not working because the PHPMailer instance does not have a property called MsgHTML. However, it has a function called msgHTML() (see the docs) that sets the ContentType property to text/html and sets the Body and AltBody properties.

So, instead of using: $mail->MsgHTML = $MailMessage;

Use: $mail->msgHTML($MailMessage);

Hope this helps!

Sponsor our Newsletter | Privacy Policy | Terms of Service