Sending emails in PHP

Many PHP applications can send emails. There are many ways to write PHP code to send out emails. Quite common are email contact forms where users enter comments in a webform which then get sent as an email to an administrator.

One simple way to send email using PHP is to use the mail() function as described here in w3schools.com. Here are the requirements and the php.ini settings that can affect the behavior of the mail function.

However that tutorial was just to explain the basics of sending email in PHP. Do not used this in a production application, because it is subject to email injection attacks as explained by w3schools and nyphp.org. Email injections are also known as "Form Post Hijacking" where hackers are taking over your email contact form to send out spam to other people.

In a production application, you need to add much more security. w3schools mentions the use of PHP Filters to validate inputs as one was to prevent email injection. PhpBuilder.com also has an article on how to prevent spam when using PHP mail.

AppTools.com has an more detailed tutorials on sending email using PHP as well as explaining how to create an more secure contact form. Lesson 5 even explains how to send attachments.

Unfortunately, hackers are very sophisticated these days, and it is quite difficult for a begining PHP programmer to write contact forms are that secure enough.

Fortunately, there are some pre-made contact form scripts out there such as the one by DagonDesign or one by Mike Cherim
that you can use. They both have a Wordpress version here and here.