Php 在WordPress中,wp_mail并没有为我的自定义表单发送邮件
以下代码应在提交表格时发送电子邮件:Php 在WordPress中,wp_mail并没有为我的自定义表单发送邮件,php,wordpress,phpmailer,Php,Wordpress,Phpmailer,以下代码应在提交表格时发送电子邮件: 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
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;
}
}
呃,你并不是真的让它发送!您正在设置一系列设置,仅此而已。您需要调用
send()
,以便它实际发送任何内容。添加以下内容(包括基本错误处理):
wp_mail()函数($sendmail)的返回值为false?不,应该为true。如果应该,您可以更正。我知道它应该是真的,我会询问它是否为假以确定问题在哪里:)问题是我没有看到邮件正在发送。我是否应该将此添加到
表单提交()
或发送smtp\u电子邮件($phpmailer)
?在您希望发送的任何一个中,可能是后者。
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';
}
if (!$phpmailer->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $phpmailer->ErrorInfo;
}