Skip to content

Instantly share code, notes, and snippets.

@philsinatra
Created August 15, 2013 12:25
Show Gist options
  • Save philsinatra/6240423 to your computer and use it in GitHub Desktop.
Save philsinatra/6240423 to your computer and use it in GitHub Desktop.
A simple sample of a contact form submission script using PHP Mailer
<?php
require "includes/class.phpmailer.php"; // https://github.com/Synchro/PHPMailer
if (isset($_POST["submit"])) {
$mail = new PHPMailer;
$mail->From = "[email protected]"; // Email address the message will be sent from
$mail->FromName = "My Website Contact Form"; // Name associated with email address being sent from
$mail->AddAddress("[email protected]"); // Email address that will receive the message
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "My Website Contact Form Submission"; // Subject of the email message
$message = "<p>The contact form on mywebsite.com has been submitted:</p>";
$message .= "<p><b>Submitted by:</b> " . $_POST["name"] . "</p>";
$message .= "<p><b>Email:</b> " . $_POST["email"] . "</p>";
$message .= "<p><b>Comments:</b> " . $_POST["comments"] . "</p>";
$mail->Body = $message;
$mail->AltBody = $_POST["name"] . " " . $_POST["email"] . " " . $_POST["comments"];
if (!$mail->Send()) {
$error_message = "Message could not be sent.<br><br>";
$error_message .= "Mailer Error: " . $mail->ErrorInfo;
exit;
}
else {
$success_message = "Thank you!";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Mailer</title>
</head>
<body>
<form class="pure-form pure-form-aligned" method="post" action="<?php $_SERVER["PHP_SELF"]; ?>">
<fieldset>
<div class="pure-control-group">
<label for="name">Name</label>
<input id="name" name="name" type="text" placeholder="Name" required>
</div>
<div class="pure-control-group">
<label for="email">Email Address</label>
<input id="email" name="email" type="email" placeholder="Email Address" required>
</div>
<div class="pure-control-group">
<label for="comments">Comments</label>
<textarea name="comments" id="comments" cols="30" rows="10" required></textarea>
</div>
<div class="pure-controls">
<button type="submit" class="pure-button pure-button-primary" name="submit">Submit</button>
</div>
</fieldset>
</form>
<?php
if (isset($error_message)) {
echo "<p>" . $error_message . "</p>";
}
if (isset($success_message)) {
echo "<p>" . $success_message . "</p>";
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment