admin管理员组文章数量:1134247
I created a function which I use to send an email when a specific button is pressed. This works great, except that it always ends up in spam instead of the inbox.
This is the function:
function search_notify_email() {
// Set variables
$email = $_POST['email'];
$title = $_POST['title'];
$content = $_POST['content'];
$location = $_POST['location'];
$siteurl = $_POST['siteurl'];
$networkurl = network_site_url();
$themeurl = get_stylesheet_directory_uri();
// Call Change Email to HTML function
add_filter( 'wp_mail_content_type', 'set_email_html_content_type' );
$to = $email;
$subject = "Attention: Test!";
$message = "<html>
<body>
Testing
</body>
</html>";
$headers[] = 'From: Example <[email protected]>';
if ( wp_mail($to, $subject, $message, $headers) ) {
// Success
} else {
// Error
}
die();
// Remove filter HTML content type
remove_filter( 'wp_mail_content_type', 'set_email_html_content_type' );
}
add_action('wp_ajax_nopriv_search_notify_email', 'search_notify_email');
add_action('wp_ajax_search_notify_email', 'search_notify_email');
I have other emails sent on my site with the same email address used and these do not end up in the spam folder.
Any idea why this happens?
I created a function which I use to send an email when a specific button is pressed. This works great, except that it always ends up in spam instead of the inbox.
This is the function:
function search_notify_email() {
// Set variables
$email = $_POST['email'];
$title = $_POST['title'];
$content = $_POST['content'];
$location = $_POST['location'];
$siteurl = $_POST['siteurl'];
$networkurl = network_site_url();
$themeurl = get_stylesheet_directory_uri();
// Call Change Email to HTML function
add_filter( 'wp_mail_content_type', 'set_email_html_content_type' );
$to = $email;
$subject = "Attention: Test!";
$message = "<html>
<body>
Testing
</body>
</html>";
$headers[] = 'From: Example <[email protected]>';
if ( wp_mail($to, $subject, $message, $headers) ) {
// Success
} else {
// Error
}
die();
// Remove filter HTML content type
remove_filter( 'wp_mail_content_type', 'set_email_html_content_type' );
}
add_action('wp_ajax_nopriv_search_notify_email', 'search_notify_email');
add_action('wp_ajax_search_notify_email', 'search_notify_email');
I have other emails sent on my site with the same email address used and these do not end up in the spam folder.
Any idea why this happens?
Share Improve this question edited Dec 14, 2019 at 5:17 butlerblog 5,0813 gold badges26 silver badges42 bronze badges asked Mar 25, 2019 at 14:23 jockebqjockebq 4631 gold badge6 silver badges18 bronze badges2 Answers
Reset to default 2You mentioned in the comments of another answer that the email address used is a real address. However, you didn't mention whether wp_mail()
is actually sending through that account (as opposed to from which is different). It may seem confusing, but it's an important distinction.
If you haven't set up to send through that account via SMTP and you're just replacing the "from" address, you are still sending through the web server's email server. There are parts of that process that make your email header look "spammy."
If you're not connecting wp_mail()
to SMTP, I highly recommend that you do. That will solve a number of possible spam issues. If you can't or won't, there are still actions you can take.
When not connecting wp_mail()
to an authenticated SMTP account, I would recommend that you set the "sender" to be the same value as the "from" address. Otherwise, in the email header, if these are two different values, it raises a red flag for the spam filter. Here's how to do it:
add_action( 'phpmailer_init', 'fix_my_email_return_path' );
function fix_my_email_return_path( $phpmailer ) {
$phpmailer->Sender = $phpmailer->From;
}
There's a more detailed overview of the approach in this article.
There are a wide variety of things this could be, so any answers to this question would be speculation. Keep in mind, it's the email that gets marked as spam (and the details of the sender, domain, etc), not the code that generates the email.
That being said, I would investigate the sender information first to make sure the headers in the email are set to show the email is sent via a valid email address with the a domain that matches the sender's domain or is authorized to send on behalf of the domain.
You can try a testing service like MXToolbox to ensure email is being delivered successfully.
本文标签: phpEmail always ends up in spam
版权声明:本文标题:php - Email always ends up in spam 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736782275a1952664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论