Skip to content

Instantly share code, notes, and snippets.

@tinshade
Created November 18, 2020 08:11
Show Gist options
  • Save tinshade/3bdeed721cbf28604ea731d13137db5c to your computer and use it in GitHub Desktop.
Save tinshade/3bdeed721cbf28604ea731d13137db5c to your computer and use it in GitHub Desktop.
A simple mailer script with PHP mailer to send mails via CPanel's email service. This does not include attachments.
<?php
require('phpmailer/PHPMailerAutoload.php');
//Getting 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->Host = 'mail.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'secret_password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 993;
$mail->setFrom('[email protected]', 'FirstName LastName');
$mail->addAddress('[email protected]');
$mail->addReplyTo($email, $name);
$mail->isHTML(true); //Allows the message to have HTML tags!
//Make your HTML as fancy as you want with HTML tags.
$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