在我的 WordPress v6.0 中,我配置了通過(guò) $phpmailer
發(fā)送 SMTP 郵件 (no-reply@example.com),工作正常。
我想使用另一個(gè) SMTP 電子郵件帳戶 (contact@example.com) 進(jìn)行所有自定義聯(lián)系表單通信。
使用 wp_mail()
發(fā)送聯(lián)系表單電子郵件,如下所示:
wp_mail($form_to, $form_subject, $form_body, implode("\r\n", $form_headers));
如何識(shí)別上述 wp_mail
并使用特定的 SMTP 帳戶?下面是我的代碼,沒(méi)有真正的 if
條件來(lái)檢查:
// MAILER add_action('phpmailer_init', 'send_smtp_email', 10, 1); function send_smtp_email($phpmailer) { $phpmailer->isSMTP(); $phpmailer->isHTML(true); if (wp_mail == "contact_form") { // not a real condition, this is what I want to achieve $phpmailer->Host = 'smtp.example.com'; $phpmailer->SMTPAuth = 'true'; $phpmailer->Port = 465; $phpmailer->Username = 'contact@example.com'; $phpmailer->Password = 'password1'; $phpmailer->SMTPSecure = 'ssl'; $phpmailer->From = 'contact@example.com'; $phpmailer->FromName = 'Site Contact Form'; } else { $phpmailer->Host = 'smtp.example.com'; $phpmailer->SMTPAuth = 'true'; $phpmailer->Port = 465; $phpmailer->Username = 'no-reply@example.com'; $phpmailer->Password = 'password2'; $phpmailer->SMTPSecure = 'ssl'; $phpmailer->From = 'no-reply@example.com'; $phpmailer->FromName = 'Site Mail'; } }
這就是我解決這個(gè)問(wèn)題的方法。
在評(píng)論表單電子郵件中包含自定義標(biāo)頭:評(píng)論:評(píng)論表單
。
使用wp_mail
過(guò)濾器,檢查評(píng)論表單發(fā)送的電子郵件是否如下:
add_filter('wp_mail', 'set_smtp_email_accounts'); function set_smtp_email_accounts($mail) { // Comment form mails custom header $header = $mail['headers']; $header_text = 'Comments: Comment Form'; $header_check = in_array($header_text, $header); //if comment form mails if ($header_check) { add_action('phpmailer_init', 'send_smtp_email_comments', 10, 1); // if comments form mail } else { add_action('phpmailer_init', 'send_smtp_email', 10, 1); } return $mail; }