Send secure Email through PHP
PHP E-mail Injections
First, look at the PHP code from the previous post:
<html> <body><?php if (isset($_REQUEST['email'])) //if "email" is filled out, send email { //send email $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 //if "email" is not filled out, display the form { 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>
The problem with the code above is that unauthorized users can insert data into the mail headers via the input form.
What happens if the user adds the following text to the email input field in the form?
someone@example.com%0ACc:person2@example.com
%0ABcc:person3@example.com,person3@example.com,
anotherperson4@example.com,person5@example.com
%0ABTo:person6@example.com
The mail() function puts the text above into the mail headers as usual, and now the header has an extra Cc:, Bcc:, and To: field. When the user clicks the submit button, the e-mail will be sent to all of the addresses above!
PHP Stopping E-mail Injections
The best way to stop e-mail injections is to validate the input.
The code below is the same as in the previous chapter, but now we have added an input validator that checks the email field in the form:
<html> <body> <?php 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; } }if (isset($_REQUEST['email'])) {//if "email" is filled out, proceed//check if the email address is invalid $mailcheck = spamcheck($_REQUEST['email']); if ($mailcheck==FALSE) { echo "Invalid input"; } else {//send email $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 {//if "email" is not filled out, display the form 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>
In the code above we use PHP filters to validate input:
- The FILTER_SANITIZE_EMAIL filter removes all illegal e-mail characters from a string
- The FILTER_VALIDATE_EMAIL filter validates value as an e-mail address
Waqar Hussain
a project management professional and blogger who write about project management and entrepreneurship in Pakistan
Website - Twitter - Facebook - More Posts
Join the mailing lists
Recent Stories
- Motivate your employees for more sucess
- Why we Pakistanis reach late at work?
- Why Pakistani entrepreneurs wait till disaster for fixing their business?
- Learn to say “NO” to your bosses
- Time management is the key to efficiency
- De-Variables a new entrepreneur from Pakistan
- Importance of Team in Project
- Workshop on “Arena Multimedia Specialist Pro”
- Why Ecommerce is essential for business in Pakistan?
- Why good feasibility report is required?
Facebook Fan Page
Twitter Feeds
- So far great parties for #EBS #GoLive 12 hrs ago
- Congratulation #TeamPakistan 1 day ago
- Innovation Pakistan looking for bloggers! Message me 1 day ago
- Hello all! Morning! Hows everyone at twitter? 1 day ago
- Any one have an idea about ptcl EVO 3.1 mbps service in North Karachi..... 4 days ago
- Yes! We are live with Oracle EBS R12 Supply Chain and Financial Modules! We seem junior & inexperience, but yet we have done GURU's work! 4 days ago
- I am not tweeting and it does not mean that I do not exists! :P 1 week ago
- Hello!!!!! No twitter for this month! no no no I am not protesting for #Aafia :p Stuck at office for work! :S 3 weeks ago
- RIP Arfa karim! We proud on you! 3 weeks ago
- RIP! Arfa karim! A little legend of Pakistan! We proud on u! 3 weeks ago
- More updates...
Posting tweet...












