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 <onlineform@example.com');
//send the mail
$sendmail = wp_mail($to, $subject, $message, $headers);
return $sendmail;
}
}
Any assistance on what should be corrected?
I have even tried authenticating wp_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 = 'me@example.com';
$phpmailer->Password = 'password';
$phpmailer->SMTPSecure = 'ssl';
$phpmailer->From = 'me@example.com';
$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 <onlineform@example.com';
//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 = 'me@example.com';
$phpmailer->Password = 'password';
$phpmailer->SMTPSecure = 'ssl';
$phpmailer->From = 'me@example.com';
$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.
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 = 'me@example.com';
$phpmailer->Password = 'password';
$phpmailer->SMTPSecure = 'ssl';
$phpmailer->From = 'me@example.com';
$phpmailer->FromName = 'Me';
}
I have just removed PHPMailer
from function send_smtp_email(PHPMailer $phpmailer) {
and I am able to send mails through SMTP account.