Skip to content

Instantly share code, notes, and snippets.

@nandomoreirame
Last active January 1, 2023 04:47
Show Gist options
  • Save nandomoreirame/5966bc4b12588904c214277328ba701a to your computer and use it in GitHub Desktop.
Save nandomoreirame/5966bc4b12588904c214277328ba701a to your computer and use it in GitHub Desktop.
WordPress REST API send email SMTP in with PHPMailer
<?php
function sendWithPhpMailer($subject, $body, $reply) {
require(ABSPATH . WPINC . '/class-phpmailer.php');
require(ABSPATH . WPINC . '/class-smtp.php');
// date_default_timezone_set( 'America/Sao_Paulo' );
$blogname = wp_strip_all_tags( trim( get_option( 'blogname' ) ) );
$smtpHost = wp_strip_all_tags( trim( get_option( 'smtp_host' ) ) );
$mailPort = wp_strip_all_tags( trim( get_option( 'smtp_port' ) ) );
$smtpUser = wp_strip_all_tags( trim( get_option( 'smtp_user' ) ) );
$smtpPass = wp_strip_all_tags( trim( get_option( 'smtp_pass' ) ) );
$mailTo = wp_strip_all_tags( trim( get_option( 'mail_to' ) ) );
$send = false;
$mail = new PHPMailer;
try {
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->Sender = $smtpUser;
$mail->CharSet = 'utf-8';
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Port = $mailPort;
$mail->Host = $smtpHost;
$mail->Username = $smtpUser;
$mail->Password = $smtpPass;
$mail->Subject = $subject;
$mail->From = $smtpUser;
$mail->setFrom($smtpUser, $blogname);
$mail->addReplyTo($reply);
$mail->addAddress($mailTo);
// Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz');
$mail->isHTML(true);
$mail->Body = $body;
$send = $mail->Send();
$mail->ClearAllRecipients();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: $mail->ErrorInfo \n";
echo "Error: $e";
return false;
}
return $send;
}
function sendContactMail( $request ) {
$response = array(
'status' => 304,
'message' => 'There was an error sending the form.'
);
$parameters = $request->get_json_params();
if ( count($_POST) > 0 ) {
$parameters = $_POST;
}
$blogname = wp_strip_all_tags( trim( get_option( 'blogname' ) ) );
$contactName = wp_strip_all_tags( trim( $parameters['contact_name'] ) );
$contactEmail = wp_strip_all_tags( trim( $parameters['contact_email'] ) );
$contactPhone = wp_strip_all_tags( trim( $parameters['contact_phone'] ) );
$contactMessage = wp_strip_all_tags( trim( $parameters['contact_message'] ) );
if ( !empty($contactName) && !empty($contactEmail) && !empty($contactPhone) && !empty($contactMessage) ) {
$subject = "(New message sent from site $blogname) $contactName <$contactEmail>";
$body = "<h3>$subject</h3><br/>";
$body .= "<p><b>Name:</b> $contactName</p>";
$body .= "<p><b>Email:</b> $contactEmail</p>";
$body .= "<p><b>Phone:</b> $contactPhone</p>";
$body .= "<p><b>Message:</b> $contactMessage</p>";
if ( sendWithPhpMailer( $subject, $body, $contactEmail ) ) {
$response['status'] = 200;
$response['message'] = 'Form sent successfully.';
}
}
return json_decode( json_encode( $response ) );
exit();
}
add_action( 'rest_api_init', function () {
register_rest_route( 'contact/v1', '/send', array(
'methods' => 'POST',
'callback' => 'sendContactMail'
));
});
@mauriciopazpp
Copy link

I guess you forgot to create $request variable of WP_REST_Request

function sendContactMail(WP_REST_Request $request) {

line 54

@rjriel
Copy link

rjriel commented Oct 12, 2019

$blogname is used in line 72 but not set in that function (set in sendWithPhpMailer). Should assign that variable in the function before using

@nandomoreirame
Copy link
Author

nandomoreirame commented Oct 12, 2019

thanks guys!!

$blogname is used in line 72 but not set in that function (set in sendWithPhpMailer). Should assign that variable in the function before using

on line 66 @rjriel https://gist.github.com/nandomoreirame/5966bc4b12588904c214277328ba701a#file-function-php-L66

Thank U

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment