Sending mail from your site with PHP

 Everyone today has an email account and getting your newsletters or infomation to subscribers of your website to your user's email is a great way to let them know what is going on at your site.

PHP has many different mail functions from the imap library that will help you to manage and edit email accounts from your site and send emails but we are not going to be focusing on that, we are going to be looking at how to send a simple email message from your website to your users.

The function looks like this:


 I am going to show you a simple example of sending a mass email to everyone in a database table. This could be for situations where someone wishes to be notified of an update via email.

$query = mysql_query('SELECT * FROM `email_table` WHERE `notify_me` = 1', $connection);

while($row = mysql_fetch_array($query))
   $SUBJECT = 'Update at SiteName!';
   $MESSAGE = 'Dear '. $row['username'] .'

There has been an update to SiteName.

Thank you,
    mail($row['email_address'], $SUBJECT, $MESSAGE, 'From:');

One thing I want to point out that is important, any newline breaks in your variable string for message will result in a newline in the email itself, also any tabs or spacing will result in strange spacing, so make sure you don't tab your message in to make your code look nice because the email will look funny.

Also, the "from" section is a header that you set with the function and not an actual seperate parameter. There are a lot of headers that you can include such as:

  • Reply-To:
  • CC:
  • BCC:
  • Content-Type (for HTML emails)

Now you can help drive return traffic back to your website by letting users know when there is new information for them to checkout!

More by this Author

  • Regex for BBCode in PHP

     Regular Expressions (Regex) are a way to parse information and match it with a larger string. For example, If you wanted to validate an email address, you could write a regular expression that would try and match...


No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.

    Click to Rate This Article