How to use WP phpmailer on separate php file.


/* Send Mail */

require_once '../../../wp-includes/class-phpmailer.php';

//Gmail settings
define('GUSER', 'sender@gmail.com'); // Gmail username
define('GPWD', 'xxxxx'); // Gmail password

//Host settings
define('SMTPUSER', 'xerxis@domain.com'); // sec. smtp username
define('SMTPPWD', '12314'); // sec. password
define('SMTPSERVER', 'mail.domain.com'); // sec. smtp server

function smtpmailer($to, $from, $from_name, $subject, $body, $is_gmail = true) {
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
if ($is_gmail) {
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
} else {
$mail->Host = SMTPSERVER;
$mail->Username = SMTPUSER;
$mail->Password = SMTPPWD;
}
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}

$msg = 'Hello World';
$subj = 'test mail message';
$to = 'kaixersoft@gmail.com';
$from = 'from@mail.com';
$name = 'test email';

//try using gmail
if (smtpmailer($to, $from, $name, $subj, $msg)) {
echo json_encode(array('status' => 'message send via Gmail'));
//try host settings
} else {
if (!smtpmailer($to, $from, $name, $subj, $msg, false)) {
if (!empty($error)) echo $error;
} else {
echo json_encode(array('status' => 'message send via Host Settings'));
}
}

Advertisement

About kaixersoft

Proud to be Pinoy. Batangueno in nature. Love to see and write computer codes. View all posts by kaixersoft

One Response to “How to use WP phpmailer on separate php file.”

  • Heath Sharber

    The following time I read a blog, I hope that it doesnt disappoint me as a lot as this one. I mean, I do know it was my choice to read, however I truly thought youd have something attention-grabbing to say. All I hear is a bunch of whining about one thing that you could repair if you werent too busy on the lookout for attention.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.