PHP Mail Script Example

If you implement this example, make sure to change the elements that apply to you, such as your email address, subject, and message. The first two lines allow you to see any errors that may occur with the script: Next is the To line. This is the email address the email should be sent to. This email address could be yours or someone else’s, depending on why you’re using the PHP mail function. In the Subject line, type whatever you want to be used as the subject of the emails sent through this PHP script. The Message line is where the body of the email goes. The echo message would be a success or error message that displays on the page if the other parameters aren’t properly filled out.

More PHP Email Options

Here are a few more options to keep in mind:

If you want to include a From header line, it’s easy to add it to your PHP script. The mail() function included with stock PHP doesn’t support SMTP authentication. If you need this function, send the email using SMTP authentication. To make sure users enter an actual email address, validate the text field to make sure it contains an email-like structure. If you want to specify the receiver’s name in addition to the To address, add the name within the quotes and then put the email address in brackets.

Protect Your Script From Spammers

If you use the mail() function, especially in combination with a web form, make sure you check that it’s called from the desired page and protect the form with something like a CAPTCHA. It’s also important to check for suspicious strings, for example, a Bcc: followed by a number of email addresses.