Hi, by the end of this post, you’ll understand how to send e-mails with PHP directly from your web server. This post contains 4 methods, as a script, as a form, etc. The syntax is pretty simple, and you will get the hang of it soon enough.
PHP mail() Function
Example
For a simple e-mail, there are 3 required parameters to include. Those being to, subject, and message.
Full Syntax: mail(to,subject,message,headers,parameters);
1 2 3 4 5 6 7 8 9 10 | <?php // Your message $msg = "Hello,\n earthling!"; // Indicate that if lines are longer than 50 chars, use wordwrap() $msg = wordwrap($msg,50); // Send the email mail("[email protected]","The earth is round, not flat",$msg); ?> |
This next step will allow you to add headers to the email, which enables your use of the “from” parameter.
1 2 3 4 5 6 7 8 9 | <?php $to = "[email protected]"; $subject = "The earth is round, not flat"; $body = "Hello earthling!"; $headers = "From: [email protected]" . "\r\n" . "CC: [email protected]"; mail($to,$subject,$body,$headers); ?> |
The following method is of the most efficient. It’s compact and functional.
1 2 3 4 5 6 7 8 9 10 | <?php $to = "[email protected]"; $subject = "The earth is round, not flat"; $body = "Hi,\n\nHow are you?"; if (mail($to, $subject, $body)) { echo("<p>Email successfully sent!</p>"); } else { echo("<p>Email delivery failed…</p>"); } ?> |
Let’s analyze the above script. It begins with the
1 | $to, $subject, and body |
variables. Then, we write an if & then/else statement. We specify it to try and submit the to, subject, and body, via mail(). If the input is proper, we echo “Email successfully sent!”, else, “Email delivery failed…”.
Finally, your script should look like this:
1 2 3 4 5 6 7 8 9 10 | <?php $to = '[email protected]'; $subject = 'The earth is round, not flat'; $body = 'Hi,/n/nHow are you?'; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $body, $headers); ?> |
Ultimately, you can use the variables message, body, txt, etc. for the message body. To keep everything in proper context such as the basic to, headers, and subject, we’re going to use the “body” variable to match.
Finally, this is what I would personally use as a contact form on my website, using the material design framework.
HTML: form.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <!DOCTYPE HTML> <html> <head> <link rel="stylesheet" href="//fonts.googleapis.com/icon?family=Material+Icons"> <link rel="stylesheet" href="//code.getmdl.io/1.3.0/material.indigo-pink.min.css"> <link rel="stylesheet" href="css/styles.css"> <script defer src="//code.getmdl.io/1.3.0/material.min.js"></script> <title>Example Request for contact Form</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form id="formItself" name="contactform" method="post" action="//michaelm.altervista.org/files/systems/phpmail/send.php"> <div id="formparent"> <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label"> <input class="mdl-textfield__input" type="text" id="first_name" maxlength="50" size="30"> <label class="mdl-textfield__label" for="first_name">First Name*</label> </div><br> <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label"> <input class="mdl-textfield__input" type="text" id="last_name" maxlength="50" size="30"> <label class="mdl-textfield__label" for="last_name">Last Name*</label> </div><br> <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label"> <input class="mdl-textfield__input" type="text" id="email" maxlength="80" size="30"> <label class="mdl-textfield__label" for="email">Email Address*</label> </div><br> <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label"> <input class="mdl-textfield__input" type="text" id="telephone" maxlength="30" size="30"> <label class="mdl-textfield__label" for="telephone">Telephone Number</label> </div><br> <div class="mdl-textfield mdl-js-textfield"> <textarea class="mdl-textfield__input" type="text" name="comments" maxlength="1000" cols="25" rows="6" id="comments"></textarea> <label class="mdl-textfield__label" for="comments">Comments</label> </div><br> <button id="sendbtn" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored"> Send </button> </div> </form> </body> </html> |
Additional CSS for form: css/styles.css
1 2 3 4 5 6 7 8 9 10 11 12 | #formItself { padding: 1%; } #formparent { width: 16%; border: 0px solid rgb(63,81,181); } #sendbtn { width: 100%; } |
The PHP: send.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | <?php if(isset($_POST['email'])) { // To/Subject $email_to = "[email protected]"; $email_subject = "New Contact Request from your website"; function died($error) { // Error codes echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.<br /><br />"; echo $error."<br /><br />"; echo "Please go back and fix these errors.<br /><br />"; die(); } // Validate that expected data is present if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email']; // required $telephone = $_POST['telephone']; // not required $comments = $_POST['comments']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The E-mail Address you inputted does not appear to be valid.<br />'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.<br />'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Input details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; // E-mail headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> <!-- Success message --> Successfully sent message. <?php } ?> |