Answer the question
In order to leave comments, you need to log in
Why do emails come empty?
Hello!
There is an ajax form for sending a letter with an attachment to the mail from the site:
HTML
<form id="form-file" class="form" method="post" enctype="multipart/form-data" data-form-validate="false">
<div class="form__row">
<div class="form__input-wrap">
<input id="fname" class="form__input" type="text" name="name" id="form_name">
<label class="form__label" for="name">Ваше имя</label>
</div>
<div class="form__input-wrap">
<input id="email_address" class="form__input" type="text" name="email" id="form_contacts">
<label class="form__label" for="email">Ваша эл. почта</label>
</div>
<div class="form__input-wrap">
<input class="form__input user-phone" type="text" name="phone" id="form_phone">
<label class="form__label" for="phone">Ваша телефон</label>
</div>
<div class="form__input-wrap">
<input class="form__input form__textarea" type="text" name="message" id="form_comment">
<label class="form__label" for="message">Пару слов о проекте</label>
</div>
<div class="form__input-wrap form-file">
<label class="file-label icon">
<i class="icon-clip"></i>
<span class="title-file">Прикрепить бриф или ТЗ (doc, docx, pdf)</span>
<input class="FileUpload1" id="attachment" name="attachment" type="file" accept=".pdf,.doc,.docx"/>
</label>
</div>
<button class="form__submit" type="button" id="submit" value="Отправить">Отправить</button>
</div>
</form>
$("#submit").on('click', function() {
var proceed = true;
if(proceed) {
var m_data = new FormData();
m_data.append( 'first_name', $('#fname').val());
m_data.append( 'email_address', $('#email_address').val());
m_data.append( 'file_attach', $('input[name=attachment]')[0].files[0]);
$.ajax({
url: '../integration.php',
data: m_data,
processData: false,
contentType: false,
type: 'POST',
dataType:'json',
success: function(response){
if(response.type == 'error'){
output = '<div class="error">'+response.text+'</div>';
}else{
output = '<div class="success">'+response.text+'</div>';
}
$("#response").hide().html(output).slideDown(); }
});
}
});
<?php
$to_email = "[email protected]";
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$output = json_encode(array(
'type'=>'error',
'text' => 'Sorry Request must be Ajax POST'
));
die($output);
$first_name = filter_var($_POST["first_name"], FILTER_SANITIZE_STRING);
$email_address = filter_var($_POST["email_address"], FILTER_SANITIZE_EMAIL);
$subject = "Your Email Subject Goes Here...";
if(strlen($first_name)<4){
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>First Name:</strong> </td><td>" . strip_tags($_POST['first_name']) . "</td></tr>";
$message .= "<tr><td><strong>Email Address :</strong> </td><td>" . strip_tags($_POST['email_address']) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
$file_attached = false;
if(isset($_FILES['file_attach']))
{
$file_tmp_name = $_FILES['file_attach']['tmp_name'];
$file_name = $_FILES['file_attach']['name'];
$file_size = $_FILES['file_attach']['size'];
$file_type = $_FILES['file_attach']['type'];
$file_error = $_FILES['file_attach']['error'];
if($file_error>0)
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );
$output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
die($output);
}
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$file_attached = true;
}
if($file_attached) //continue if we have the file
{
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From:Fromname <[email protected]>" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body .= "--" . $separator . $eol;
$body .= "Content-type:text/html; charset=utf-8\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type:".$file_type." ";
$body .= "Content-Type: application/octet-stream; name=\"" . $file_name . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment; filename=\"".$file_name."\"". $eol;
$body .= $encoded_content . $eol;
$body .= "--" . $separator . "--";
}
else
{
$eol = "\r\n";
$headers = "From: Fromname <[email protected]>" . $eol;
$headers .= "Reply-To: ". strip_tags($email_address) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body .= $message . $eol;
}
$send_mail = mail($to_email, $subject, $body, $headers);
if(!$send_mail)
{
//If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$first_name .' Thank you for your order, will get back to you shortly'));
die($output);
}
?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question