Sending email with PHP using the 'mail' function is simple. Check out these examples:

Here is a simple PHP mail example. Run this script and then check your email. Don't forget to change the value of '$ToAddress' to your email.

<?php

// The email address to send to

$ToAddress = 'YourFriend@example.com';

// The subject line of the email

$SubjectLine = 'This is a php mail example';

// The actual email message

$Message = 'This php mail example is just a simple example. Have a nice day!';

// This invokes PHP's mail function and sends the email

mail($ToAddress,$SubjectLine,$Message);

?>

After running the previous PHP mail example, you should have an email in the inbox of the email address you specified.

If you don't see anything in your inbox like I did at first, you might want to check your Spam folder. That's where the email ended up when I ran the simple PHP mail example script.

Another PHP mail example

Now we'll add a header to the email sent with the PHP mail example.

<?php

// The email address to send to

$ToAddress = 'somemailaddress@aserver.com';

// The 'From' email address

$FromAddress = 'fromaddress@someserver.com';

// Create an email header

$EmailHeader = 'From: '.$FromAddress;

// The subject line of the email

$SubjectLine = 'This is a php mail example';

// The actual email message

$Message = 'This php mail example is just a simple example. Have a nice day!';

// This invokes PHP's mail function and sends the email

mail($ToAddress,$SubjectLine,$Message,$EmailHeader);

?>

I hope these two PHP mail examples served as a good primer for sending email with PHP.

Of course, PHP is capable of doing much more with sending email.

If you'd like to learn more about sending email with PHP and would like to see some other PHP mail examples, why not take a look at the PHP manual page us3.php.net/manual/en/function.mail.php

Share

Comments (0)

No comments found!

Write new comment