A simple mailer script with PHP Mailer to send mails via GMail. This does not include attachments but that's pretty easy to do once you get the mail working.
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
<?php | |
require('phpmailer/PHPMailerAutoload.php'); | |
//Taking data from a form | |
$name = trim($_POST['con_name']); | |
$email = trim($_POST['con_email']); | |
$subject = trim($_POST['con_subject']); | |
$message = trim($_POST['con_comment']); | |
if($name != null && $email != null && $subject != null && $message != null){ | |
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) | |
{ | |
$signal = 'bad'; | |
$msg = 'Invalid email. Please check'; | |
} | |
else{ | |
$mail = new PHPMailer; | |
$mail ->isSMTP(); | |
$mail->Host = 'smtp.gmail.com'; | |
$mail->SMTPAuth = true; | |
$mail->Username = 'username@gmail.com'; | |
$mail->Password = 'secret_password'; | |
$mail->SMTPSecure = 'tls'; | |
$mail->Port = 587; | |
$mail->setFrom('username@company.com', 'FirstName LastName'); | |
$mail->addAddress('username@company.com'); | |
$mail->addReplyTo($email, $name); | |
$mail->isHTML(true); //This allows for HTML tags so you can make your message as fancy as you want! | |
$mail->Subject = $subject; | |
$mail->Body = 'Name: '.$name.' <br />Subject: '.$subject.' <br />Message: '.$message; | |
if(!$mail->send()) { | |
echo 'Message could not be sent.'; | |
echo 'Mailer Error: ' . $mail->ErrorInfo; | |
} else { | |
$signal = 'ok'; | |
$msg = 'Form submitted, We will reply ASAP!'; | |
} | |
} | |
} | |
else{ | |
$signal = 'bad'; | |
$msg = 'Please fill in all the fields.'; | |
} | |
$data = array( | |
'signal' => $signal, | |
'msg' => $msg | |
); | |
echo json_encode($data); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment