通过WP Mail功能通过电子邮件发送自定义表单数据

时间:2019-08-08 作者:The WP Intermediate

在发布此帖子之前,我浏览了以下资源:

On form submission how to send 2 email to different users

https://stackoverflow.com/questions/17157685/php-redirect-to-another-page-after-form-submit

https://stackoverflow.com/questions/10927682/how-to-send-e-mail-from-form-in-wordpress

How to send an email using wp_mail and using more than one BCC in the header

外部URLalso.

我在插件中有一个名为shortcode的文件。php。它有一个表单,在此基础上,我编写了以下代码:

add_action(\'wp_ajax_nopriv_submit_contact_form\', \'submit_visa_form\');
function submit_visa_form() {
    if(isset($_POST[\'submit\'])) {
        $email=$_POST[\'email\'];
        $firstname=$_POST[\'firstname\'];
        $familyname=$_POST[\'familyname\'];
        $other_names=$_POST[\'other_names\'];
        $dob_day=$_POST[\'dob_day\'];
        $dob_month=$_POST[\'dob_month\'];
        $dob_year=$_POST[\'dob_year\'];
        $city_of_birth=$_POST[\'city_of_birth\'];
        $country_of_birth=$_POST[\'country_of_birth\'];
        $gender=$_POST[\'gender\'];


        $form_message .= "First Name: ".clean_string($firstname)."\\n";
        $form_message .= "Family Name: ".clean_string($familyname)."\\n";
        $form_message .= "Email: ".clean_string($email)."\\n";
        $form_message .= "Other Name ".clean_string($other_names)."\\n";
        $form_message .= "DOB: ".clean_string($dob_day)."\\n";
        $form_message .= "DOM: ".clean_string($dob_month)."\\n";
        $form_message .= "DOY: ".clean_string($dob_year)."\\n";
        $form_message .= "COB: ".clean_string($city_of_birth)."\\n";
        $form_message .= "COOB: ".clean_string($country_of_birth)."\\n";
        $form_message .= "GENDER: ".clean_string($gender)."\\n";
        $email_to = "[email protected]";
        $subject = "Form submitted by $email";
        $headers = \'From: \'. $firstname .\' <\'. $email .\'>\' . "\\r\\n";

         if(wp_mail($email_to,$subject,$form_message,$headers)) {
                echo json_encode(array("result"=>"complete"));
            } else {
                echo json_encode(array("result"=>"mail_error"));
                var_dump($GLOBALS[\'phpmailer\']->ErrorInfo);
        }
            wp_die();
    }
}
在当前使用的表单操作中→

action="<?php the_permalink(); ?>"
可查看活动表单以供参考here.

但最终在填写完表格后,我没有收到电子邮件。

我应该遵循哪些故障排除步骤?

在我给出的外部链接中,表单操作是:

action="<?php echo admin_url(\'admin-ajax.php\'); ?>"
当我使用abive URL时,它会将我带到管理页面,并给出以下内容→

“0”

enter image description here

哪一个是正确的?

是否有人在通过电子邮件发送自定义表单时遇到了相同的问题?是否有办法检查导致错误的原因?

1 个回复
SO网友:WPTricksDK

我建议为此使用AJAX。您可以对jQuery部分执行以下操作:

<script type="text/javascript">
jQuery(document).ready(function($) {

  // Execute when user clicks on the "Submit" button
  $(\'#SUBMIT_BUTTON_ID\').on(\'click\', function(e) {

    e.preventDefault();

    var email = $(\'input[name=email]\').val();
    var firstname = $(\'input[name=firstname]\').val();
    var familyname = $(\'input[name=familyname]\').val();
    var other_names = $(\'input[name=other_names]\').val();
    var dob_day = $(\'input[name=dob_day]\').val();
    var dob_month = $(\'input[name=dob_month]\').val();
    var dob_year = $(\'input[name=dob_year]\').val();
    var city_of_birth = $(\'input[name=city_of_birth]\').val();
    var country_of_birth = $(\'input[name=country_of_birth]\').val();
    var gender = $(\'input[name=gender]\').val();

    var formData = new FormData();
        formData.append("action", "submit_contact_form");
        formData.append("email", email);
        formData.append("firstname", firstname);
        formData.append("familyname", familyname);
        formData.append("other_names", other_names);
        formData.append("dob_day", dob_day);
        formData.append("dob_month", dob_month);
        formData.append("dob_year", dob_year);
        formData.append("city_of_birth", city_of_birth);
        formData.append("country_of_birth", country_of_birth);
        formData.append("gender", gender);

        $.ajax({
          url: "/wp-admin/admin-ajax.php",
          type: "post",
          data: formData,
          processData: false,
          contentType: false,
          cache: false,
          success: function(resp) {
            console.log(\'Message sent!\');
          },
          error: function(errorThrown) {
            console.log(errorThrown);
          }
        });
      });
  });
</script>
然后按原样使用您的功能,将电子邮件发送给收件人。还要注意,输入字段必须包含下面的ID,jQuery才能获得输入。在这里使用jQuery/AJAX进行需求检查也更容易。

希望有帮助:)

编辑:

你不是真的在发邮件(!)您只检查邮件是否已发送,而不是运行函数本身。更改此代码位:

if(wp_mail($email_to,$subject,$form_message,$headers)) {
    echo json_encode(array("result"=>"complete"));
} else {
    echo json_encode(array("result"=>"mail_error"));
    var_dump($GLOBALS[\'phpmailer\']->ErrorInfo);
}
为此:

$send_mail = wp_mail($email_to,$subject,$form_message,$headers);
if($send_mail) {
    echo json_encode(array("result"=>"complete"));
} else {
    echo json_encode(array("result"=>"mail_error"));
    var_dump($GLOBALS[\'phpmailer\']->ErrorInfo);
}

相关推荐

Vagrant Access index.php

我相信这是一个合适的地方。首先,我安装了VirtualBox和Vagrant。在那之后,我将cd刻录到以下内容:https://github.com/LearnWebCode/vagrant-lamp (下载了zip,提取了文件夹)然后在运行vagrant up之后,我到达了Congratulations 通过IP分页http://192.168.56.101/之后,我在hosts文件中设置了一个易于阅读的URL,并可以运行它,但它会导致出现祝贺页面,而不是索引。php的Hello World。你知道怎么