Hi guys i have a problem with my form in ajax, i am using ajaxSubmit because i need upload files in the form, so this is my jquery code for the forms:
var send_form = function(form_id, btn_id, fail_id, done_id){
var sending_contact_form = false;
var contact_captcha = $('.codecatch').text();
setTimeout(function(){
$(".botCode").each(function(){
$(this).val(contact_captcha);
});
}, 3000);
$(form_id).submit(function(e){
e.preventDefault();
e.stopPropagation();
var form = $(this).ajaxSubmit({});
if ($(".codecatch").html() == $(".botCode").val() ) {
if(!sending_contact_form){
sending_contact_form = true;
var action = $(this).attr('action');
var btn = $(this).find(btn_id);
btn.val($(btn_id).data('sending'));
var xhr = form.data('jqxhr');
xhr.fail(function(){
$(done_id).fadeOut();
$(fail_id).fadeIn();
}).done(function(data){
btn.val($(btn_id).data('send'));
$(fail_id).fadeOut();
$(done_id).fadeIn().css("display","inline-block");
//$(this).trigger("reset");
setTimeout(function() {
$(done_id).fadeOut();
}, 3000 );
sending_contact_form = false;
},'json');
}
}else{
$(done_id).fadeOut();
$(fail_id).fadeIn().css("display","inline-block");
setTimeout(function() {
$(fail_id).fadeOut();
}, 3000 );
}
return false;
});
}
This is my code for form process
if(count($_POST)>0 && $_POST['f_codecatch'] == $contact_captcha_code){
$attachments = [];
if(!empty($_FILES)){
$files = $_FILES; // file array
foreach ($files as $file) {
$upload_dir = wp_upload_dir();
$path = $upload_dir['basedir'].'/files/'; //upload dir.
if(!is_dir($path)) { mkdir($path); }
$attachment_id = upload_user_file( $file ,$path);
$attachments[] = $path . $file['name'];
}
}
$msg = '';
$msg .= '<html><body>';
$msg .= '<div style="padding-top: 30px;padding-right: 20%;padding-left: 20%;">';
$msg .= '<div style="width: 100%; margin: 0 auto; text-align: center; margin-bottom: 20px;">';
$msg .= '<img src="'.THEME_PATH.'/images/logo-atiker.png" width="250px" alt="Atiker México" title="Atiker México">';
$msg .= '</div>';
if (!empty($_POST['f_name'])) {
$msg .= '<div style="display: -webkit-box;display: -webkit-flex;display: -ms-flexbox;display: flex;width: 100%;padding: 10px;-webkit-justify-content: space-around;-ms-flex-pack: distribute;justify-content: space-around;-webkit-box-align: center;-webkit-align-items: center;-ms-flex-align: center;align-items: center;border-top: 1px dashed #c3c3c3;">';
$msg .= '<div style="-webkit-box-flex: 1;-webkit-flex: 1;-ms-flex: 1;flex: 1;font-weight: 700;">';
$msg .= 'Nombre:';
$msg .= '</div>';
$msg .= '<div style="-webkit-box-flex: 1;-webkit-flex: 1;-ms-flex: 1;flex: 1;color: #6b6b6b;font-weight: 700;">';
$msg .= htmlspecialchars($_POST['f_name']);
$msg .= '</div>';
$msg .= '</div>';
}
if (!empty($_POST['f_email'])) {
$msg .= '<div style="display: -webkit-box;display: -webkit-flex;display: -ms-flexbox;display: flex;width: 100%;padding: 10px;-webkit-justify-content: space-around;-ms-flex-pack: distribute;justify-content: space-around;-webkit-box-align: center;-webkit-align-items: center;-ms-flex-align: center;align-items: center;border-top: 1px dashed #c3c3c3;">';
$msg .= '<div style="-webkit-box-flex: 1;-webkit-flex: 1;-ms-flex: 1;flex: 1;font-weight: 700;">';
$msg .='Email:';
$msg .= '</div>';
$msg .= '<div style="-webkit-box-flex: 1;-webkit-flex: 1;-ms-flex: 1;flex: 1;color: #6b6b6b;font-weight: 700;">';
$msg .= htmlspecialchars($_POST['f_email']);
$msg .= '</div>';
$msg .= '</div>';
}
if (!empty($_POST['f_phone'])) {
$msg .= '<div style="display: -webkit-box;display: -webkit-flex;display: -ms-flexbox;display: flex;width: 100%;padding: 10px;-webkit-justify-content: space-around;-ms-flex-pack: distribute;justify-content: space-around;-webkit-box-align: center;-webkit-align-items: center;-ms-flex-align: center;align-items: center;border-top: 1px dashed #c3c3c3;">';
$msg .= '<div style="-webkit-box-flex: 1;-webkit-flex: 1;-ms-flex: 1;flex: 1;font-weight: 700;">';
$msg .= 'Teléfono:';
$msg .= '</div>';
$msg .= '<div style="-webkit-box-flex: 1;-webkit-flex: 1;-ms-flex: 1;flex: 1;color: #6b6b6b;font-weight: 700;">';
$msg .= htmlspecialchars($_POST['f_phone']);
$msg .= '</div>';
$msg .= '</div>';
}
if (!empty($_POST['f_type'])) {
$msg .= '<div style="display: -webkit-box;display: -webkit-flex;display: -ms-flexbox;display: flex;width: 100%;padding: 10px;-webkit-justify-content: space-around;-ms-flex-pack: distribute;justify-content: space-around;-webkit-box-align: center;-webkit-align-items: center;-ms-flex-align: center;align-items: center;border-top: 1px dashed #c3c3c3;">';
$msg .= '<div style="-webkit-box-flex: 1;-webkit-flex: 1;-ms-flex: 1;flex: 1;font-weight: 700;">';
$msg .= 'Tipo de Cotización:';
$msg .= '</div>';
$msg .= '<div style="-webkit-box-flex: 1;-webkit-flex: 1;-ms-flex: 1;flex: 1;color: #6b6b6b;font-weight: 700;">';
$msg .= htmlspecialchars($_POST['f_type']);
$msg .= '</div>';
$msg .= '</div>';
}
if (!empty($_POST['f_model'])) {
$msg .= '<div style="display: -webkit-box;display: -webkit-flex;display: -ms-flexbox;display: flex;width: 100%;padding: 10px;-webkit-justify-content: space-around;-ms-flex-pack: distribute;justify-content: space-around;-webkit-box-align: center;-webkit-align-items: center;-ms-flex-align: center;align-items: center;border-top: 1px dashed #c3c3c3;">';
$msg .= '<div style="-webkit-box-flex: 1;-webkit-flex: 1;-ms-flex: 1;flex: 1;font-weight: 700;">';
$msg .='Año y Modelo:';
$msg .= '</div>';
$msg .= '<div style="-webkit-box-flex: 1;-webkit-flex: 1;-ms-flex: 1;flex: 1;color: #6b6b6b;font-weight: 700;">';
$msg .= htmlspecialchars($_POST['f_model']);
$msg .= '</div>';
$msg .= '</div>';
}
$msg .= '</div>';
$msg .= '</body></html>';
$to = 'myemail';//htmlspecialchars($_POST['f_email']);
$subject = htmlspecialchars($_POST['f_name']).' <'.htmlspecialchars($_POST['f_email']).'>';
$body = $msg;
$headers = array(
'From:'.htmlspecialchars($_POST['f_name']).' <'.htmlspecialchars($_POST['f_email']).'>',
'Content-Type: multipart/form-data; charset=UTF-8',
'Reply-To:'.htmlspecialchars($_POST['f_name']).' <'.htmlspecialchars($_POST['f_email']).'>'
);
wp_mail( $to, $subject, $body, $headers, $attachments);
echo json_encode(array('success'=>true));
exit;
}
I already get the email fine but that come in format plain html, is for the header content or what is your suggest because i already try many things xD, thanks for any help
This is the email i get
have you used enctype="multipart/form-data" in form tag
The problem was the headers this the correct way for the accents and more, thanks for any help :D
Content-type: text/html; charset=UTF-8