Skip to content

Instantly share code, notes, and snippets.

@cdsaenz
Last active April 22, 2023 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdsaenz/aa16e51cee16af1abd24653c7b281bc4 to your computer and use it in GitHub Desktop.
Save cdsaenz/aa16e51cee16af1abd24653c7b281bc4 to your computer and use it in GitHub Desktop.
WP Mail using authenticated SMTP without a plugin
<?php
use PHPMailer\PHPMailer\PHPMailer;
// Set up SMTP settings
add_action( 'phpmailer_init', 'wpse_phpmailer_init' );
function wpse_phpmailer_init( PHPMailer $mailer ) {
$mailer->isSMTP();
$mailer->Host = ''; // replace with your SMTP server hostname
$mailer->SMTPAuth = true;
$mailer->Username = ''; // replace with your SMTP username
$mailer->Password = ''; // replace with your SMTP password
$mailer->SMTPSecure = 'ssl'; // or tls
$mailer->Port = 465; // replace with your SMTP port number
}
$to = 'testemail@gmail.com';
$subject = 'Test Email with HTML Content';
$message = '<html><body><h1>Hello, World!</h1><p>This is a test email with <strong>HTML content</strong>.</p></body></html>';
$headers = array('Content-Type: text/html; charset=UTF-8');
// Send the email
if (wp_mail($to, $subject, $message, $headers)) {
error_log('Email sent');
}
else {
error_log('Email NOT sent');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment