Skip to content

Instantly share code, notes, and snippets.

@jkuip
Created September 18, 2015 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jkuip/d118cde4a7f93712ab29 to your computer and use it in GitHub Desktop.
Save jkuip/d118cde4a7f93712ab29 to your computer and use it in GitHub Desktop.
Simple PHP email
<!DOCTYPE html>
<html>
<head>
<title>Email</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container" style="margin-top: 50px">
<?php
// Suppress PHP error notices
error_reporting(E_ALL & ~E_NOTICE);
// If the submit button has been pressed
if(isset($_POST['submit']))
{
// Empty error array
$error = array();
// Check for an e-mail address
if(empty($_POST['email']))
{
$error['email'] = 'An email address is required';
}
// Check for a message
if(empty($_POST['message']))
{
$error['message'] = 'A message is required';
}
// If there are no errors, send the email
// If there are errors, display them to the user
if(sizeof($error) == 0)
{
// Send the message (to, subject, message, from)
mail($_POST['email'], 'Default subject', stripslashes($_POST['message']), "From: {$_POST['email']}");
// Create confirmation message
echo "<div class=\"alert alert-success\">Your e-mail has been sent!</div>";
}
}
?>
<!-- email Form -->
<form method="post" action="email.php">
<div class="form-group">
<label for="email">Your e-mail address:</label>
<input name="email" type="text" value="<?php echo $_POST['email']; ?>" class="form-control" />
<span class="text-danger"><?php echo $error['email']; ?></span>
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea name="message" rows="8" class="form-control"><?php echo $_POST['message']; ?></textarea>
<span class="text-danger"><?php echo $error['message']; ?></span>
</div>
<input name="submit" type="submit" value="Send Message" class="btn btn-primary" />
</form>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment