Beginners PHP Tutorial – Sending Mail with PHP
November 10, 2008
For users who prefer to learn PHP visually we have a range of PHP video tutorials, this method of training greatly enhances learning and allows beginners to master PHP at their own pace.
View the PHP Tutorial Videos
Beginners PHP Tutorial – Sending E-mail Using PHP
PHP has a built in function that can be used to send email to one or more recipients. Some reasons you may want to use a mail function in your program include:
- Staying in touch with your site members via a newsletter.
- Sending a “welcome” message when someone joins your membership site.
- Sending a purchase receipt to someone who has just bought your product or service.
- Running a “Contact Us” form.
Introducing The PHP mail() Function
The mail() function is PHP’s built-in connection to your web site’s mail server. The function accepts up to 5 parameters. The first 3 are required. Let’s look at the parameters:
| Parameter | Description |
| to | The email address of the recipient. |
| subject | The email’s subject line. |
| message | The body of the email message. |
| headers | Any additional headers you want to pass such as “From”, “Cc”, “Bcc” etc. Although this is an optional parameter, I recommend always passing the “From” parameter (your email address), otherwise your server may substitute strange from addresses such as NULL@Servername.com, where ServerName is the actual name of your mail server. Some servers will not send your mail at all unless the from address is provided. |
| parameters | Other optional parameters sent directly to your mail server. You’ll probably never use these. |
How to Send an Email
A quick and dirty way is to simply call the function with each of the parameters separated by commas. Use double quotation marks as shown below:
mail(“somebody@somesite.com”,
“Welcome to My Site!”,
“Hello Somebody, thank you for joining my site!”,
“From: webmaster@mysite.com“);
What we did was pass the 3 required parameters and one of the optional ones. Each parameter was separated by a comma.
Of course, in real life, you probably won’t be hard coding the recipients address. That will come from a contact form post or a MySQL query result.
In that case, you would put all of your email content into variables and pass the variables to the mail() function.
Let’s say that you received a contact form post. The email address of the person posting the form is passed to your script as $_POST[‘to’], and their name is in $_POST[‘name’]. You would set up the parameters like this:
$to = $_POST['to']; $name = $_POST['name']; $subject = "Thank you for your message"; $message = "Hello $name, thanks for contact me. I’ll be in touch soon."; $from = "webmaster@mysite.com"; $headers = "From: $from"; // Send email mail($to,$subject,$message,$headers);
And that’s all it takes to send simple text-based email from PHP. Sending HTML email, or email with attachments, is more complicated and may be the subject of a future PHP tutorial.
Entry Filed under: PHP. Tags: Beginners PHP Tutorial, PHP Mail Function, PHP Send Mail.
Trackback this post | Subscribe to the comments via RSS Feed