As of December 2024 - CPanel and email servers like GMAIL have some updated requirements. I tried dozens of configurations and this is a full working sample and PHP snippet. I left the commented lines in there to show you some extra settings that I tried. You don't need the commented lines. This example assumes you have the PHPMailer library as a folder/directory on your server. You can also use composer. I was able to send emails out with nothing but $mail->Host = 'localhost';
to send directly from CPanel, but those emails only made it through to less secure email services, such as AOL. To send to GMAIL, you need to enable SSL and SMTPAuth, or else GMAIL will block the email entirely and the email won't even make it to the spam folder.
You don't need to change settings on the CPanel - the auto setting in CPanel email works out of the box.
You can set your from and reply-to as a gmail or other email if you prefer.
You don't have to add DKIM or MX records, although it will help with your email score.
SMTPDebug = SMTP::DEBUG_SERVER; $mail->SMTPDebug = 2; // Enables detailed debugging output // Server settings // $mail->Host = 'smtp.gmail.com'; // Gmail SMTP server // $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'localhost'; $mail->Port = 465; $mail->SMTPAuth = true; // Enable SMTP authentication $mail->SMTPSecure = 'ssl'; $mail->setFrom('any email - preferable a domain email', 'Domain Name'); $mail->addReplyTo('any email - preferable a domain email', 'Domain Name'); // $mail->isSMTP(); // $mail->Username = 'your_email'; // CPanel email address // $mail->Password = "your_pass"; // $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Use SMTPS (SSL encryption) // $mail->Port = 25; // SMTP port for SSL // Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = "Your Subject"; $mail->Body = "Thank you message or whatever you want.Some info
Maybe a welcome message.
Extra paragraph if needed.
"; $mail->AltBody = "Thank you message Info without html tags Maybe a welcome message Extra paragraph if needed."; $mail->addAddress("the email you want to send to"); // Add a recipient $mail->addAddress("add a second recipient email if you want"); // Add a recipient $mail->addBcc("bcc an email if you want"); // Add a recipient $mail->Send(); } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; }When testing with just the host set to localhost, I would not receive an error from the GoDaddy CPanel server, but GMAIL servers blocked it because of the lack of SSL. The code samples previously answered did not work or help troubleshoot. This sample will work right away, assuming you have PHPMailer installed in the public_html folder.
Here is the sample without the commented lines to show how simple the code is:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once($_SERVER['DOCUMENT_ROOT'].'/PHPMailer/src/Exception.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/PHPMailer/src/PHPMailer.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/PHPMailer/src/SMTP.php');
$mail = new PHPMailer(true);
try {
$mail->Host = 'localhost';
$mail->Port = 465;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->SMTPSecure = 'ssl';
$mail->setFrom('any email - preferable a domain email', 'Domain Name');
$mail->addReplyTo('any email - preferable a domain email', 'Domain Name');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = "Your Subject";
$mail->Body = "<h1>Thank you message or whatever you want.</p>
<p>Some info</p>
<p>Maybe a welcome message.</p>
<p>Extra paragraph if needed.</p>";
$mail->AltBody = "Thank you message
Info without html tags
Maybe a welcome message
Extra paragraph if needed.";
$mail->addAddress("the email you want to send to"); // Add a recipient
$mail->addAddress("add a second recipient email if you want"); // Add a recipient
$mail->addBcc("bcc an email if you want"); // Add a recipient
$mail->Send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>