Skip to content

Instantly share code, notes, and snippets.

@marcelo-ribeiro
Last active May 22, 2018 15:39
Show Gist options
  • Save marcelo-ribeiro/7562a53662f9da3864e2dfd4d7426f28 to your computer and use it in GitHub Desktop.
Save marcelo-ribeiro/7562a53662f9da3864e2dfd4d7426f28 to your computer and use it in GitHub Desktop.
Sample Function To Send WP Mail
<?php
function send_email() {
$site_name = 'Company Name';
$site_email = 'example@mail.com';
$to = "$site_name <$site_email>";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = "$site_name - " . $_POST['subject'];
$message = $_POST['message'];
$body = "
<p><b>Name:</b><br>$name</p>
<p><b>Email:</b><br>$email</p>
<p><b>Message:</b><br>$message</p>";
$headers = "From: $site_name <$site_email>\r\n";
$headers .= "Reply-To: $name <$email>\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8";
return mail( $to, $subject, $body, $headers );
}
if ( !empty($_POST) ) {
$mail = send_email();
$message = $mail ?
'<b>Thank you</b><br>Your message has been sent successfully!' :
'<b>Sorry</b><br>There was an error sending the message. Please try again.';
echo json_encode( array(
'success' => $mail,
'message' => $message
));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment