我有注册表需要发送到第三方软件,然后我需要在成功提交后通过电子邮件发送表格的内容。
我使用jquery ajax表单插件提交表单,然后在成功时调用jquery ajax。我必须使用插件,因为我需要在表单中发送附件,jquery序列化函数不包含文件。表单成功提交跨站点但电子邮件脚本未运行。
以下是代码:
var options = {
success: showResponse, // post-submit callback
// other available options:
url: 'url to third party software', // override for form's 'action' attribute
type: 'post' // 'get' or 'post', override for form's 'method' attribute
};
// bind to the form's submit event
$('#main-form').ajaxSubmit(options);
// post-submit callback
function showResponse() {
var newCustomerForm = jQuery("#main-form").serialize();
jQuery.ajax({
type: "POST",
url: "url to email script on local server",
action: addCustomer,
data: newCustomerForm,
success: function (data) {
$('#form-container').html('<h3>Thank you. Your Online Application has been successfully submitted. </h3>');
}
});
}
//php email sending script
function addCustomer()
{
foreach ($_POST as $key => $value)
{
$str .= "<b> ".str_replace('_',' ',$key).":</b> $value<br>";
}
if(isset($_FILES['file1']))
{
move_uploaded_file($_FILES["file1"]["tmp_name"],WP_CONTENT_DIR .'/uploads/'.basename($_FILES['file1']['name']));
$attachments[$s] = WP_CONTENT_DIR ."/uploads/".$_FILES["file1"]["name"];
}
}
////////////////email settings///////////////////////////////////////////
$emailTo = 'email address';
add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
wp_mail($emailTo, $subject, $body, $headers, $attachments);
$emailSent = true;
////////////////////////////////////////////////////////////////////////
}
add_action('wp_ajax_addCustomer', 'addCustomer');
add_action('wp_ajax_nopriv_addCustomer', 'addCustomer');