PHP's mail() function
PHP easy E-Mail
The easiest way to send an email through PHP is to send a text email.
<?php $to = "someone@example.com"; // Mail recipient $subject = "Parameter mail"; // Mail title $message = "Hello! This is the content of the message."; // Mail text $from = "someonelse@example.com"; // Email Sender $headers = "From:" . $from; // Header information settings mail($to,$subject,$message,$headers); echo "Message sent"; ?>
PHP Mail form
With PHP, you can create a feedback form on your own site. The following example sends a text message to the specified e-mail address:
<html> <head> <meta charset="utf-8"> <title>Rookie tutorial(runoob.com)</title> </head> <body> <?php if (isset($_REQUEST['email'])) { // Send mail if mailbox parameters are received // Send mail $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; mail("someone@example.com", $subject, $message, "From:" . $email); echo "Mail sent successfully"; } else { // Show form if there is no mailbox parameter echo "<form method='post' action='mailform.php'> Email: <input name='email' type='text'><br> Subject: <input name='subject' type='text'><br> Message:<br> <textarea name='message' rows='15' cols='40'> </textarea><br> <input type='submit'> </form>"; } ?> </body> </html>
PHP prevents E-mail injection
The best way to prevent e-mail injection is to validate the input. Add the input verifier to detect the email field in the form:
<html> <head> <meta charset="utf-8"> <title>Rookie tutorial(runoob.com)</title> </head> <body> <?php function spamcheck($field) { // Filter? Var() to filter e-mail // Use filter? Sanitize? Email $field=filter_var($field, FILTER_SANITIZE_EMAIL); //Filter? Var() to filter e-mail // Use filter? Validate? Email if(filter_var($field, FILTER_VALIDATE_EMAIL)) { return TRUE; } else { return FALSE; } } if (isset($_REQUEST['email']))
{
//Send mail if mailbox parameters are received
// Judge whether the mailbox is legal $mailcheck = spamcheck($_REQUEST['email']); if ($mailcheck==FALSE) { echo "illegal input"; } else { // Send mail $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; mail("someone@example.com", "Subject: $subject", $message, "From: $email" ); echo "Thank you for using our mail form"; } } else { // Show form if there is no mailbox parameter echo "<form method='post' action='mailform.php'> Email: <input name='email' type='text'><br> Subject: <input name='subject' type='text'><br> Message:<br> <textarea name='message' rows='15' cols='40'> </textarea><br> <input type='submit'> </form>"; } ?> </body> </html>