Created
June 26, 2014 13:45
-
-
Save webag/33c1207d169e91d1cda2 to your computer and use it in GitHub Desktop.
Отправка почты с файловым вложением. Необходим phpmailer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form class="ajax-form" enctype="multipart/form-data"> | |
<input type="text" name="cname" placeholder="Введите имя"> | |
<input type="text" name="ctel" placeholder="Введите телефон"> | |
<input type="hidden" value="order" name="ctip"> | |
<input name="attachfile" type="file"> | |
<button type="submit" class="btn btn-primary">Отправить</button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
var form = $(".ajax-form"); | |
form.on("submit", function(e) { | |
var send=false; | |
e.preventDefault(); | |
$(this).find('input').on('focus', function(){ | |
$(this).removeClass('error'); | |
}) | |
if ($(this).find("input[name='ctel']").val() === "") { | |
$(this).find("input[name='ctel']").addClass('error'); | |
send = false; | |
}else{ | |
send = true; | |
} | |
var formData = new FormData($(this)[0]); | |
if (send === true) { | |
$.ajax({ | |
type: "POST", | |
url: "/test/send.php", | |
processData: false, | |
contentType: false, | |
data: formData | |
}).success(function() { | |
form.find("input[name!='ctip']").val(''); | |
$('a.close-reveal-modal').trigger('click'); | |
$('a.sp').trigger('click'); | |
setTimeout(function() { | |
$('a.close-reveal-modal').trigger('click');}, | |
3000); | |
//yaCounter23718715.reachGoal(citem); | |
}); | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- обработка почты необходим phpmailer--> | |
<?php | |
require 'class.phpmailer.php'; | |
$cname = htmlspecialchars(strip_tags(trim($_POST['cname']))); | |
$cphone = htmlspecialchars(strip_tags(trim($_POST['ctel']))); | |
$ctip = htmlspecialchars(strip_tags(trim($_POST['ctip']))); | |
if($ctip=="call"){ | |
$subject = 'Перезвоните мне.'; | |
$message = 'Просьба о звонке от <b>' . $cname . '</b><br/>'; | |
$message .= 'Телефон: <b>' . $cphone . '</b><br/>'; | |
} | |
if($ctip=="order"){ | |
$subject = 'Новая заявка'; | |
$message = 'Новая заявка от <b>' . $cname . '</b><br/>'; | |
$message .= 'Телефон: <b>' . $cphone . '</b><br/>'; | |
} | |
$mail = new PHPMailer; | |
$mail->CharSet = 'utf-8'; | |
$mail->From = '[email protected]'; // от кого | |
$mail->FromName = 'Andrew'; // от кого | |
$mail->AddAddress('[email protected]'); // кому - адрес, Имя | |
$mail->IsHTML(true); // выставляем формат письма HTML | |
$mail->Subject = $subject; // тема письма | |
$mail->Body = $message; | |
if(isset($_FILES['attachfile'])) { | |
if($_FILES['attachfile']['error'] == 0){ | |
$mail->AddAttachment($_FILES['attachfile']['tmp_name'], $_FILES['attachfile']['name']); | |
} | |
} | |
// отправляем наше письмо | |
if(!$mail->send()) { | |
echo 0; | |
} else { | |
echo 1; | |
} | |
?> | |
<!-- обработка почты --> |
Форма рабочая, только нужно подключить скрипты на странице с формой и поменять настройки smtp для phpmailer под свой аккаунт
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Эта рабочая форма?