E-mails can easily be sent using HTML and the PHP mail() function. The syntax is:
mail($recipient_email, $subject, $message, $headers);
Let me use this opportunity to point out that using the PHP mail() function to send too many e-mails at a time to, for instance, Yahoo or GMail can lead to a permanent blockage of your server from sending e-mails to that e-mail server. This is because the e-mails will be detected as a spam or virus attack.
Let's get started. First of all create your HTML contact form as this:
<form action="send_email.php" method="post"> <label for="name">Name (required)</label><input name="name" placeholder="Name" /> <label for="email">Email (required)</label><input name="email" placeholder="E-mail"/> <label for="subject">Subject (required)</label><input name="subject" placeholder="Subject"/> <label for="message">Your Message (required)</label><textarea name="message" rows="8" cols="50" placeholder="Your Message"></textarea> <input type="submit" name="send" value="Send" /> </form>
In your PHP file create a function to validate the email of the sender. The validation function
spamcheck($field)
will return true if the email is in the right email format and false if the email is not in the right email format.
function spamcheck($field) { //filter_var() sanitizes the e-mail //address using FILTER_SANITIZE_EMAIL $field = filter_var($field, FILTER_SANITIZE_EMAIL); //filter_var() validates the e-mail //address using FILTER_VALIDATE_EMAIL if (filter_var($field, FILTER_VALIDATE_EMAIL)) { return TRUE; } else { return FALSE; } }
Now get the values from the contact form and use it to send the email.
//Assigning variables to the form values $name = trim($_POST['name']); $email = trim($_POST['email']); $subject = trim($_POST['subject']); $message = trim($_POST['message']); //if all the fields are not empty then proceed to the validation of the email //else display an error message to prompt the user to fill all fields if ($name !== '' && $email !== '' && $subject !== '' && $message !== '') { //if email is filled out, proceed //check if the email address is invalid if (spamcheck($email) == FALSE) { die("<h3> :( Sending message failed. Enter a valid email.</h3>"); } else { //send mail $to = 'dvtitsolutions@gmail.com'; $message = $name . '\n' . $message; $headers = 'From: ' . $email; $send = @mail($to, $subject, $message, $headers); if ($send) { die('<h3> ;) Thank you. Email successfully sent.</h3>'); } else { die('<h3> :( Sending email failed.</h3>'); } } } else { //if "email" is not filled out, kill the page with an error message die("<h3> :( Sending message failed. All fields are required.</h3>"); }
I preceeded the mail() function with the '@' symbol. This will hide any errors that will occur during the sending of the email and rather display the error message in the else block.
Feel free to comment. Peace!
I would like to thank you for the efforts you've put in writing this website. I am hoping to see the same high-grade content from you in the future as well. In truth, your creative writing abilities has inspired me to get my own blog now ;)