Search code examples
phpwordpressphpmailer

In WordPress, wp_mail is not sending mails for my Custom Form


Below code should send email on form submission:

function form_submit() {

if (isset($_POST['submit'])) {

    // get the info from the form
    $to = sanitize_email($_POST['email']);

    // The email subject
    $subject = 'you got mail';

    // Build the message
    $message = 'Message from...';

    //set the form headers
    $headers = array('Content-Type: text/html; charset=UTF-8', 'From: Me <[email protected]');

    //send the mail
    $sendmail = wp_mail($to, $subject, $message, $headers);

    return $sendmail;
}
}

Any assistance on what should be corrected? I have even tried authenticating wp_mail with my SMTP server using below code: Source (https://gist.github.com/butlerblog/c5c5eae5ace5bdaefb5d)

add_action('phpmailer_init', 'send_smtp_email');

function send_smtp_email($phpmailer) {

$phpmailer->isSMTP();
$phpmailer->Host = 'example.com';
$phpmailer->SMTPAuth = true;
$phpmailer->Port = '465';
$phpmailer->Username = '[email protected]';
$phpmailer->Password = 'password';
$phpmailer->SMTPSecure = 'ssl';
$phpmailer->From = '[email protected]';
$phpmailer->FromName = 'Me';
}

Update #1

In WordPress v.5.8.2, below form is sending the plain text mail:

function form_submit() {

if (isset($_POST['submit'])) {

    // get the info from the form
    $to = sanitize_email($_POST['email']);

    // The email subject
    $subject = 'you got mail';

    // Build the message
    $message = 'Message from...';

    //set the form headers
    $headers[] = 'Content-Type: text/html; charset=UTF-8';
    $headers[] = 'From: Me <[email protected]';

    //send the mail
    wp_mail($to, $subject, $message, $headers);
  }
}

With below code, I am trying to send the SMTP mail:

add_action('phpmailer_init', 'send_smtp_email');

function send_smtp_email(PHPMailer $phpmailer) {

$phpmailer->isSMTP();
$phpmailer->Host = 'example.com';
$phpmailer->SMTPAuth = true;
$phpmailer->Port = '465';
$phpmailer->Username = '[email protected]';
$phpmailer->Password = 'password';
$phpmailer->SMTPSecure = 'ssl';
$phpmailer->From = '[email protected]';
$phpmailer->FromName = 'Me';
$phpmailer->send();
if (!$phpmailer->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $phpmailer->ErrorInfo;
}
}

With above cannot send SMTP mail.


Solution

  • Below is the working PHPmailer code:

    add_action('phpmailer_init', 'send_smtp_email');
    
    function send_smtp_email($phpmailer) {
    
    $phpmailer->isSMTP();
    $phpmailer->Host = 'example.com';
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port = '465';
    $phpmailer->Username = '[email protected]';
    $phpmailer->Password = 'password';
    $phpmailer->SMTPSecure = 'ssl';
    $phpmailer->From = '[email protected]';
    $phpmailer->FromName = 'Me';
    }
    

    I have just removed PHPMailer from function send_smtp_email(PHPMailer $phpmailer) { and I am able to send mails through SMTP account.