Bwyss
Bwyss

Reputation: 1834

How to send email in php with wp_mail

I have seen similar code in use all over the place, but I just can't get it to work...

$to = '[email protected]';
$subject = 'test';
$message = 'test';
$headers = 'just a test';
wp_mail( $to, $subject, $message, $headers );

I also tried with:

if (wp_mail( $to, $subject, $message )) {
    echo "success";
} else {
    echo "fail";
}

Nothing gets printed. What am I missing?

I would prefer not to use a plugin for this.

Upvotes: 1

Views: 6858

Answers (1)

montrealist
montrealist

Reputation: 5693

This is slightly different from how I have it:

try {
    $sent = @wp_mail( $to, $subject, $message );
} catch (Exception $e) {
    error_log('oops: ' . $e->getMessage()); // this line is for testing only!
}

if ( $sent ) {
    error_log('hooray! email sent!'); // so is this one
} else {
    error_log('oh. email not sent.'); // and this one, too
}

Oh, and it will not work on localhost without proper setup.

Upvotes: 1

Related Questions