Skip to content

Instantly share code, notes, and snippets.

@tinshade
Created November 18, 2020 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tinshade/11dacde809fc05ba486ed46ce792e9d8 to your computer and use it in GitHub Desktop.
Save tinshade/11dacde809fc05ba486ed46ce792e9d8 to your computer and use it in GitHub Desktop.
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.
<?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