wp_mail()不适用于WordPress插件

时间:2019-03-29 18:03:19

标签: php wordpress smtp

我有一个插件,单击该按钮后会发送电子邮件。众所周知,表单没有问题(因为数据已完美存储在数据库中),此代码段中可能会出错,因为它无法向客户端或管理员发送电子邮件。 如果表单没有问题,SMTP可能是问题所在,这不太可能发生,因为联系表单7可以完美地将邮件发送给客户端和管理员。

功能wte_enquiry_send_mail()是使用wp_mail()功能发送电子邮件的功能。

function wte_enquiry_send_mail()
{
    $email = sanitize_email( $_POST['enquiry_email'] );
    $wp_travel_engine_settings = get_option( 'wp_travel_engine_settings',true );
    $name = esc_attr( $_POST['enquiry_name'] );
    $country = isset( $_POST['enquiry_country'] ) ? esc_attr( $_POST['enquiry_country'] ):'N/A';
    $contact = esc_attr( $_POST['enquiry_contact'] );
    $adult = isset( $_POST['enquiry_adult'] ) ? esc_attr( $_POST['enquiry_adult'] ):'N/A';
    $children = isset( $_POST['enquiry_children'] ) ? esc_attr( $_POST['enquiry_children'] ):'N/A';
    $message1 = esc_attr( $_POST['enquiry_message'] );
    $postid = get_post( $_POST['enquiry_pid'] );
    $slug = $postid->post_name;
    $url = '<a href='.esc_url( get_permalink( $postid ) ).'>'.esc_attr( $slug ).'</a>';
    $subject = isset( $wp_travel_engine_settings['query_subject'] ) ? esc_attr( $wp_travel_engine_settings['query_subject'] ):'Enquiry received';
    $admin_email = get_option ('admin_email');
    $to = sanitize_email($admin_email);
    $email_from = $name.'<'.$email.'>';
    $headers[] = 'From: '.$name.' <'.$admin_email .'>';
    $headers[] = 'Reply-To: '.$name.' <'.$email .'>';
    $message = __("Name: ","wp-travel-engine"). $name.'<br/>';
    $message.= __("Country: ","wp-travel-engine"). $country.'<br/>';
    $message.= __("Trip: ",'wp-travel-engine'). $url.'<br/>';
    $message.= __("Email: ","wp-travel-engine"). $email.'<br/>';
    $message.= __("Contact: ",'wp-travel-engine'). $contact.'<br/>';
    $message.= __("Adult: ","wp-travel-engine"). $adult.'<br/>';
    $message.= __("Children: ",'wp-travel-engine'). $children.'<br/>';
    $message.= __("Message: ",'wp-travel-engine'). $message1.'<br/>';

    add_filter('wp_mail_content_type', create_function('', 'return "text/html"; '));

    if ( strpos( $wp_travel_engine_settings['email']['emails'], ',') !== false ) {
            $wp_travel_engine_settings['email']['emails'] = str_replace(' ', '', $wp_travel_engine_settings['email']['emails']);
        $admin_emails = explode( ',', $wp_travel_engine_settings['email']['emails'] );

        foreach ( $admin_emails as $key => $value ) {
                $admin_sent = wp_mail( $value, $subject, $message, $headers );
            }
    }
    else{
        $wp_travel_engine_settings['email']['emails'] = str_replace(' ', '', $wp_travel_engine_settings['email']['emails']);
        $admin_sent = wp_mail( $to, $subject, $message, $headers );
    }

我希望表格填写后会自动发送给客户和管理员。

1 个答案:

答案 0 :(得分:0)

@Xhynk正确地指示我检查服务器日志。 我做到了,并且意识到由于主机的发件人域策略而导致SMTP错误。

  

如果您的网站是example.com,并且您有一个PHP电子邮件表单发送电子邮件,则该电子邮件中的“发件人”地址必须类似于contact@example.com。从Web服务器使用其他地方(例如Yahoo或Google)的FROM地址发送的电子邮件将被阻止,并且永远不会发送。

我正在使用G-mail,一经更改,它便开始起作用。