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

774

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 authenticatingwp_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]ample.com';
$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.

363

Answer

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 removedPHPMailer fromfunction send_smtp_email(PHPMailer $phpmailer) { and I am able to send mails through SMTP account.

833

Answer

Solution:

Uh, you're not actually telling it to send! You're setting a bunch of settings and that's all. You need to callsend() in order for it to actually send anything. Add this (includes basic error handling):

if (!$phpmailer->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $phpmailer->ErrorInfo;
}

People are also looking for solutions to the problem: php - Adding wamp server to aptana

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.