Skip to content

Instantly share code, notes, and snippets.

@MrAbalogu
Created June 11, 2016 11:53
Show Gist options
  • Save MrAbalogu/a40fb8e6530d16261a3c6f5a1d70573c to your computer and use it in GitHub Desktop.
Save MrAbalogu/a40fb8e6530d16261a3c6f5a1d70573c to your computer and use it in GitHub Desktop.
Send mail from contact form with PHPmailer & Mailgun very easy
// FIRST CREATE A FREE MAILGUN ACCOUNT HERE https://www.mailgun.com/
// html contact form example
<form action="mail.php" method="post">
<h5>name</h5>
<input type="text" name="name">
<h5>email address</h5>
<input type="text" name="email">
<h5>message</h5>
<textarea name="message"></textarea>
<input type="submit" name="submit" value="submit">
</form>
// mail.php file
// dont forget to download composer here https://getcomposer.org/download/ and phpmailer here https://github.com/PHPMailer/PHPMailer
<?php
require 'vendor/autoload.php';
$mail = new PHPMailer;
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mailgun.org'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'MAILGUN USERNAME'; // SMTP username
$mail->Password = 'MAILGUN PASSWORD'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom( $email , $name );
$mail->addAddress('info@youraddress.com', 'Chinedu Abalogu'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Someone has something to say about xxxx';
$mail->Body = $message;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
header("Location: /error.html");
} else {
header("Location: /thankyou.html");
}
}
?>
@arally2021
Copy link

i am a programmer please i hope code to run sucessfully

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment