天道酬勤,学无止境

Using wp_mail() instead of mail() in Wordpress does not work

Since PHP mail has been disabled on my server it has stopped a theme integrated contact form from working.

The theme is called Boldy and it has its own sendmail.php file which uses mail() instead of wp_mail().

Changing mail() to wp_mail() does not work, but I'm not sure why?

<?php
if (isset($_POST['submit']))
{
    error_reporting(E_NOTICE);

    function valid_email($str)
    {
        return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
    }

    if ($_POST['name'] != '' && $_POST['email'] != '' && valid_email($_POST['email']) == TRUE && strlen($_POST['comment']) > 1)
    {
        $to = preg_replace("([\r\n])", "", $_POST['receiver']);
        $from = preg_replace("([\r\n])", "", $_POST['email']);
        $subject = "Website contact message from ".$_POST['name'];
        $message = $_POST['comment'];
        $match = "/(bcc:|cc:|content\-type:)/i";

        if (preg_match($match, $to) || preg_match($match, $from) || preg_match($match, $message))
        {
            die("Header injection detected.");
        }

        $headers = "From: ".$from."\r\n";
        $headers .= "Reply-to: ".$from."\r\n";

        if (mail($to, $subject, $message, $headers))
        {
            echo 1; //SUCCESS
        }
        else
        {
            echo 2; //FAILURE - server failure
        }
    }
    else
    {
        echo 3; //FAILURE - not valid email
    }

}
else
{
    die("Direct access not allowed!");
}
?>

评论

Insert at line 2:

    define('WP_USE_THEMES', false);
    require('../../../wp-load.php');

The trick is that sendmail.php as originally written doesn't actually load in the wordpress gear, so wp_mail isn't defined.

The first line should be optional. I took it from the sample code at http://butlerblog.com/2012/09/23/testing-the-wp_mail-function/.

I've lost about 30 minutes figuring this out.

If you're using anything that allows you to configure SMTP within WordPress, take it out.

Then put everything into a function:

add_action('init','delay_until_init');
function delay_until_init(){
   // call wp_mail() here
}

By default, the Wordpress function wp_mail() uses PHPs internal mail() function so the change you've made won't fix the issue.

If your web server has disabled the standard PHP mail() function then you will need to switch over to using SMTP for sending your emails.

There's a few different ways to achieve this, but the easiest method I've found has been to use the Easy WP SMTP plugin. Once installed, you'll need to configure the plugin to use the login details for your SMTP server. If you hvae a Gmail address, you can use those details for setting it up.

EDIT: You still need to switch your code over to using the wp_mail() function which will automatically use SMTP (if you have the WP SMTP plugin installed).

It makes sense, because wp_mail() also has similair server requirements, which can be found here: http://codex.wordpress.org/Function_Reference/wp_mail

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • wp_mail(Wordpress 邮件功能)不发送到 gmail 或 live.co.uk 帐户(wp_mail (Wordpress mail function) not sending to gmail or live.co.uk accounts)
    问题 我有一个网站,允许访问者通过两种方式与我联系 - 通过其自己页面上的联系表和侧栏中的“ajax”小联系表。 这两种联系方式都会向我的 Gmail 帐户发送一封电子邮件。 它自己页面上的联系表单工作正常,但 ajax 表单不会发送到 gmail(也不会发送到以 .live.co.uk 结尾的电子邮件地址) 如果我将 ajax 联系表单上的“收件人”地址更改为我的其他域之一(即不同的电子邮件地址),则它可以正常工作。 我检查了发送的电子邮件的标题,并最终安装了一个“插件”,该插件可以更改返回路径,以防 Google(和 live.co.uk)在接受电子邮件之前进行检查。 这没有任何区别。 在这些论坛和其他论坛上有很多关于 wp_mail 没有发送到 gmail 但没有实际解决方案的喋喋不休。 我已经检查了 gmail 垃圾邮件文件夹等,以防电子邮件进入那里但它们没有。 我也试过只使用 'mail()' 函数,但无济于事。 我在 ajax 调用所在页面上的代码如下所示: $headers = 'From: myWebsite <enquiries@mywebsite.co.uk>' . "\r\n"; $headers .= 'Reply-To: myWebsite <enquiries@mywebsite.co.uk>'; $email_to = "myemailaddy
  • wp_mail(Wordpress 邮件功能)不发送到 gmail 或 live.co.uk 帐户(wp_mail (Wordpress mail function) not sending to gmail or live.co.uk accounts)
    问题 我有一个网站,允许访问者通过两种方式与我联系 - 通过其自己页面上的联系表和侧栏中的“ajax”小联系表。 这两种联系方式都会向我的 Gmail 帐户发送一封电子邮件。 它自己页面上的联系表单工作正常,但 ajax 表单不会发送到 gmail(也不会发送到以 .live.co.uk 结尾的电子邮件地址) 如果我将 ajax 联系表单上的“收件人”地址更改为我的其他域之一(即不同的电子邮件地址),则它可以正常工作。 我检查了发送的电子邮件的标题,并最终安装了一个“插件”,该插件可以更改返回路径,以防 Google(和 live.co.uk)在接受电子邮件之前进行检查。 这没有任何区别。 在这些论坛和其他论坛上有很多关于 wp_mail 没有发送到 gmail 但没有实际解决方案的喋喋不休。 我已经检查了 gmail 垃圾邮件文件夹等,以防电子邮件进入那里但它们没有。 我也试过只使用 'mail()' 函数,但无济于事。 我在 ajax 调用所在页面上的代码如下所示: $headers = 'From: myWebsite <enquiries@mywebsite.co.uk>' . "\r\n"; $headers .= 'Reply-To: myWebsite <enquiries@mywebsite.co.uk>'; $email_to = "myemailaddy
  • wp_mail (Wordpress mail function) not sending to gmail or live.co.uk accounts
    I have a website that allows visitors to contact me two ways - via a contact form on it's own page and via an 'ajax' small contact form in the sidebar. Both forms of contact send an email to my gmail account. The contact form on it's own page works fine but the ajax form doesn't send to gmail (nor does it send to email addresses ending in .live.co.uk) If I change the 'to' address on the ajax contact form to one of my other domains (i.e. a different email address) then it works fine. I've checked the headers of the emails that are sent and ended up installing a 'plugin' that alters the return
  • wp_mail wordpress html,样式不适用(wp_mail wordpress html with style not aplying)
    问题 我正在尝试从我的 wordpress 插件发送邮件,但是当我检索它时,它没有样式或图像。 我这样做: <?php $headers = array( "From: Enara <no-reply@enara.es>", "MIME-Version: 1.0", "Content-type: text/html; charset=UTF-8'" ); $message = "<html> <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> </head> <body> <div style='background-color: #16a085;'> <table> <tr><td colspan='2'><img style=\'margin-botton: 20px;\' src='http://enara.litrodeenergia.com/wp-content/uploads/2015/12/enara-white.png' /></td></tr> <tr><td colspan='2'>DATOS DE CLIENTE</td></tr> <tr><td colspan='2'>$particular</td></tr> <tr> <td>Nombre:</td>
  • 在 WordPress 中将电子邮件列表添加到 wp_mail()(Adding a list of emails to wp_mail() in WordPress)
    问题 我正在尝试从某个用户角色向 wp_mail() 添加电子邮件地址列表。 我有一个以逗号分隔的列表存储为$user_email_list但无法将其输出到$multiple_recipients数组中。 任何帮助将不胜感激。 // Get users and their roles, create list of emails to send notification to. $user_args = array( 'role__in' => 'test_role', 'orderby' => 'user_nicename', 'order' => 'ASC' ); $users = get_users($user_args); foreach ( $users as $user ) : $user_email_list = $user->user_email . ', '; endforeach; // Email Data $multiple_recipients = array( $user_email_list ); $subject = $post->post_title; $body = $post->post_content; 回答1 更新您的 foreach 代码并在最终检查您的 $multiple_recipients 变量,它将是逗号分隔值。 foreach
  • 在 wp_mail 函数 wordpress 中添加 html(add html in wp_mail function wordpress)
    问题 我已经在 wordpress 中创建了一个注册表单,但是我的电子邮件确认有问题,我正在尝试在正文中添加一些 html 但我做错了,你能帮我吗? 这是我在functions.php中的代码的一部分,是将电子邮件发送给用户的代码: if ( $user_id && !is_wp_error( $user_id ) ) { $code = sha1( $user_id . time() ); $activation_link = add_query_arg( array( 'key' => $code, 'user' => $user_id ), get_permalink(168)); add_user_meta( $user_id, 'has_to_be_activated', $code, true ); wp_mail( $email, 'BLOOOGERS Activación cuenta', '<img src="..."> <br /> <br />Bienvenido a nuestra comunidad, este es el link para que actives tu cuenta:<br /> <br />' . $activation_link ); add_filter( 'wp_mail_content_type', 'set_html
  • publish_future_post in functions.php (wordpress) don't run script made with Facebook SDK 3
    Dear Wordpress Hackers and Lovers, I'm getting mad trying to let my script publishing a photo automatically to a fan page everytime a scheduled post is being published using the facebook SDK 3 (https://github.com/facebook/facebook-php-sdk). The problem is that the script stop running when the wp_cron execute a line in functions.php that has to populate the object Facebook when published using the add_action('publish_future_post', 'future_post_being_published', 10, 1);. With publish_post works perfectly. The script stop running when come across the following: $facebook = new Facebook(array(
  • functions.php(wordpress)中的publish_future_post不运行使用Facebook SDK 3制作的脚本(publish_future_post in functions.php (wordpress) don't run script made with Facebook SDK 3)
    问题 尊敬的Wordpress黑客和爱好者, 每次尝试使用Facebook SDK 3(https://github.com/facebook/facebook-php-sdk)发布预定帖子时,我都想让我的脚本自动将照片自动发布到粉丝页面上,这让我很生气。 问题是,当wp_cron在functions.php中执行一行时,脚本停止运行,该行必须使用add_action('publish_future_post','future_post_being_published',10,1)发布时必须填充对象Facebook 。 。 与publish_post完美配合。 遇到以下情况,脚本将停止运行: $facebook = new Facebook(array( 'appId' => '1xxxxxx1', 'secret' => '28xxxxxxd91', 'fileUpload' => true )); 直接从后台发布帖子时不会发生此问题,实际上是函数add_action('publish_post','future_post_being_published',10,1); 完美地工作。 我可以说它停止了,因为我添加了一些调试行,向我发送了代码各部分的电子邮件,但是只有当publish_post被激活时,我才获得全部信息,而不是future_post_being_published
  • wordpress wp_mail发送我的html消息作为文本附件(wordpress wp_mail is sending my html message as a text attachment)
    问题 这是发送邮件的代码: $to = $_POST['leadEmail']; $subject = "my subject"; $message = ( $_POST['leadEmailPref'] == "html" ) ? $messageh : $messagep ; $headers[] = "From: My Name <ny_name@gmail.com>"; $headers[] = "Content-type: ".$_POST['leadEmailPref'] ; wp_mail( $to, $subject, $message, $headers ); 当我转到收件箱时,该邮件将作为附件发送,但看不到该邮件。 显然,我不需要附件,我希望接收电子邮件的人立即查看该消息。 回答1 您的Content-type:是错误的。 它应该是text/html或text/plain ,而不是html 。 $headers[] = "Content-type: text/".$_POST['leadEmailPref'] ; 回答2 你应该试试: add_filter( 'wp_mail_content_type', 'set_html_content_type' ); wp_mail( 'To Email', 'Subject', '<h1>Html Email</h1>
  • add html in wp_mail function wordpress
    i have created a register form in wordpress but i have a problem with the email confirmation, i'm trying to add some html in the body but i made something wrong, can you help me? this is part of my code in functions.php, is the code that send the email to user: if ( $user_id && !is_wp_error( $user_id ) ) { $code = sha1( $user_id . time() ); $activation_link = add_query_arg( array( 'key' => $code, 'user' => $user_id ), get_permalink(168)); add_user_meta( $user_id, 'has_to_be_activated', $code, true ); wp_mail( $email, 'BLOOOGERS Activación cuenta', '<img src="..."> <br /> <br />Bienvenido a
  • Adding a list of emails to wp_mail() in WordPress
    I am trying to add a list of email addresses to wp_mail() from a certain user role. I have a comma delimited list stored as $user_email_list but cannot get that to output into the $multiple_recipients array. Any help would be greatly appreciated. // Get users and their roles, create list of emails to send notification to. $user_args = array( 'role__in' => 'test_role', 'orderby' => 'user_nicename', 'order' => 'ASC' ); $users = get_users($user_args); foreach ( $users as $user ) : $user_email_list = $user->user_email . ', '; endforeach; // Email Data $multiple_recipients = array( $user_email_list
  • wp_mail wordpress html with style not aplying
    I'm trying to send mail from my wordpress plugin but when I retrieve it, it has not styles or images. I'm doing this way: <?php $headers = array( "From: Enara <no-reply@enara.es>", "MIME-Version: 1.0", "Content-type: text/html; charset=UTF-8'" ); $message = "<html> <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> </head> <body> <div style='background-color: #16a085;'> <table> <tr><td colspan='2'><img style=\'margin-botton: 20px;\' src='http://enara.litrodeenergia.com/wp-content/uploads/2015/12/enara-white.png' /></td></tr> <tr><td colspan='2'>DATOS DE CLIENTE</td><
  • 如何在wordpress中发送带有附件的电子邮件?(How to send an email with attachment in wordpress?)
    问题 现在我可以发送没有附件的电子邮件: wp_mail( $to, $subject, $message, $headers); 但是如何发送带有附件的电子邮件? 回答1 <?php wp_mail( $to, $subject, $message, $headers, $attachments ); ?> http://codex.wordpress.org/Function_Reference/wp_mail 回答2 参考下面例如 $attachments = array(WP_CONTENT_DIR . '/uploads/file_to_attach.zip'); $headers = 'From: My Name <myname@mydomain.com>' . "\r\n"; wp_mail('test@test.com', 'subject', 'message', $headers, $attachments); 回答3 add_filter( 'wpcf7_mail_components', 'mycustom_wpcf7_mail_components' ); function mycustom_wpcf7_mail_components( $components ) { $components['attachments'][] = 'full path of
  • 发送电子邮件将 Wordpress 显示为发件人(Sending email displays Wordpress as sender)
    问题 我在 PHP 中设置了一个电子邮件表单并通过 WORDPRESS 发送它。 我以为我已经涵盖了所有内容,但显然它一直将WORDPRESS显示为发件人,而不是我配置的电子邮件。 我在下面添加了我的代码,但我想我在发送电子邮件时缺少§from_name字段? 我用谷歌搜索wp_mail()问题,但找不到/理解任何东西。 $from_name = "MyWebsite"; $from_email = "info@website.com"; $admin_email = "[receiver email]"; $admin_email_subject = "A new message has been sent from MyWebsite!"; $email_styles = "font-family:Verdana; font-size:11px;"; $admin_email_message = <<<ADMIN_EMAIL ...[CONTENT]...ADMIN_EMAIL; $admin_email_message = str_replace($find_these, $replace_these, $admin_email_message); $admin_email_message = '<div style="' . $email_styles . '">'
  • wordpress wp_mail is sending my html message as a text attachment
    This is the code to send the mail: $to = $_POST['leadEmail']; $subject = "my subject"; $message = ( $_POST['leadEmailPref'] == "html" ) ? $messageh : $messagep ; $headers[] = "From: My Name <ny_name@gmail.com>"; $headers[] = "Content-type: ".$_POST['leadEmailPref'] ; wp_mail( $to, $subject, $message, $headers ); When I go to my inbox, the message is sent as an attachment and I don't see the message. Obviously I don't want the attachment, and I want the person who receives the email to see the message immediately.
  • WordPress 中的 Cron 工作不起作用(Cron job in WordPress not working)
    问题 我做了一个 WordPress cron 工作,应该每小时发送一封邮件: function mail_cron_job() { $time = date( 'h:ia', time() ); wp_mail( '****@******', 'Hourly E-mail from WP Cron', 'This message was sent on ' . $time ); } 我在表单提交中添加了此代码: if ( !wp_next_scheduled('mail_cron_job') ) { wp_schedule_event( time(), 'hourly', 'mail_cron_job'); } 表单数据提交正常,但 cron 作业无法启动。 没有错误报告。 我缺少什么吗? 回答1 您需要使用动作挂钩将事件和功能联系在一起。 add_action( 'mail_cron_job', 'mail_cron_job' ); 你可以在函数之后添加它。 回答2 wordpress 中的 Cron 作业计划 //Add Interval [ Day ] function cron_add_daily($schedules) { // Adds once every minute to the existing schedules. $schedules['daily'] =
  • Cron job in WordPress not working
    I have made a WordPress cron job that should send out a mail each hour: function mail_cron_job() { $time = date( 'h:ia', time() ); wp_mail( '****@******', 'Hourly E-mail from WP Cron', 'This message was sent on ' . $time ); } I added this code on a form submission: if ( !wp_next_scheduled('mail_cron_job') ) { wp_schedule_event( time(), 'hourly', 'mail_cron_job'); } The form data is submitting OK, but the cron job won't start. No error reports. Anything I'm missing?
  • wp_mail 无法使用 MAMP 在本地主机上工作(wp_mail not working on localhost with MAMP)
    问题 我试图让 wp_mail 函数给自己发送一封电子邮件,但由于某种原因我无法让它工作。 有人可以帮我吗? 这是我的基本脚本... $to = "someone@gmail.com"; $subject = 'wp_mail function test'; $message = 'This is a test of the wp_mail function: wp_mail is working'; $headers = "From: Blog Name <admin@myemail.com> \r\n"; if ( $sent = wp_mail( $to, $subject, $message, $headers ) ) echo 'The test message was sent. Check your email inbox.'; else echo 'The message was not sent!'; 如果我 var_dump($phpmailer) 然后我得到以下... object(PHPMailer)[288] public 'Priority' => int 3 public 'CharSet' => string 'UTF-8' (length=5) public 'ContentType' => string 'text/plain' (length
  • wp_mail() 循环,只发送到最后一个地址(wp_mail() in a loop, only sending to last address)
    问题 我正在使用一个插件,它允许您向x人发送电子邮件。 您所要做的就是输入他们的“姓名”和“电子邮件地址”。 这些条目分别命名为friend_name[0]和friend_email[0] 。 有+ & -按钮用于添加和删除输入字段。 $key中的数字相对于字段的数量上升和下降。 我已经确认了报名表上的所有$keys更改。 当表单提交并通过循环处理时,所有电子邮件都会发送,但只发送到最后输入的电子邮件。 因此,如果我输入 5 个姓名和 5 个电子邮件,它们会发送以下数据: 姓名: John Doe 1电子邮件: JohnDoe5@gmail.com 姓名: John Doe 2电子邮件: JohnDoe5@gmail.com 姓名: John Doe 3电子邮件: JohnDoe5@gmail.com 姓名: John Doe 4电子邮件: JohnDoe5@gmail.com 姓名: John Doe 5电子邮件: JohnDoe5@gmail.com 这是循环: function invfr_sendmail() { $post = ( !empty( $_POST ) ) ? true : false; if( $post ) { $subject = invfr_get_settings( 'subject' ); $message = invfr_get_settings
  • wp_mail() in a loop, only sending to last address
    I'm using a plugin that allows you to send an e-mail to x amount of people. All you have to do is enter their "name" and "e-mail address". These entries are named friend_name[0] & friend_email[0] respectively. There are + & - buttons for adding and removing entry fields. The number in the $key goes up and down relative to the number of fields. I have confirmed all the $keys change on the entry form. When the form is submitted and is processed through the loop, the e-mails all send, but only to the last e-mail entered. So if I enter 5 names and 5 e-mails, they send with this data: Name: John