KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

Send mail in PHP

If you want to start a image hosting business read the following lesson first:

This lesson shows how to send bulk mails by PHP.

Send bulk HTML mail

Assume you have a lot of clients. Sometimes you need send them emails all at once.

It is easy to do this chore in PHP if you have saved all your client email address in MySQL database.

Suppose your have an table named emailTable, which has a column email storing email addresses.

Pull email addresses from MySQL

First, you need pull out email addresses from MySQL database using the following code:

//open database connection
$username = "yourname";
$password = "yourpassword"; //input your password here.
$database = "yourdb";
//connect to database
$link=mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");

//get emails from database

$query="select email from emailTable";
$result=mysql_query($query) or die('Error, query failed');
mysql_close($link);

Add addresses to PHP

After pulling out email addresses from PHP database, it is time to put all of them into PHP code.

There are 2 options about where to put these emails.

  1. Append to "To"
  2. Append to "BCC" (Blind Carbon Copy)

The first choice is to put all email addresses in "To" fields, like user1@site.com,user2@site2.com,.... The drawback of this approach is that every user will see this long email list. You just broadcast all user's email address to everyone.

The second choice is to put all email addresses in "BCC", although in code, you will see user1@site.com,user2@site2.com,.... long mailing list in field "BCC", each individual user will only see his/her own email address.

So, we use the second options: adding email addresses in BCC field. If you went through the lesson Retrieve multiple records , the following code will be easy for you.

 

$row=1;
$numrows=mysql_num_rows($result);
$bccfield="Bcc: ". mysql_result($result,0,"email");
while($row<$numrows)
{
$email=mysql_result($result,$row,"email");
$bccfield .= "," . $email;
$row++;
}
$bccfield .= "\r\n";

 

Combine Code together

You may use the following sendMail.php to send bulk emails.

<?php

//send to your self
$to ="youemail@yoursite.com";

$subject = 'System email';

// message
$message = "<html>".
"<head>".
" <title>System email</title>".
"</head>".
"<body>".
"Your message is here ".
"</body>".
"</html>";

//get email list

//open database connection
$username = "yourname";
$password = "yourpassword"; //input your password here.
$database = "yourdb";
//connect to database
$link=mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");

$query="select email from emailTable";
$result=mysql_query($query) or die('Error, query failed');
mysql_close($link);

$row=1;
$numrows=mysql_num_rows($result);
$bccfield="Bcc: ". mysql_result($result,0,"email");
while($row<$numrows)
{
$email=mysql_result($result,$row,"email");
$bccfield .= "," . $email; //seperate by comma
$row++;
}
$bccfield .= "\r\n";

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= $bccfield;

$headers .= 'From: System <yourname@yoursite.com>' . "\r\n";

// Mail it
mail($to, $subject, $message,$headers);
?>

In the above code, HTML header must be set for HTML mail. If you don't set header, you just send a plain text file by calling mail($to,$subject,$message). In the "To" field, set your email to it.

 
PHP Calendar
Comprehensive PHP Web Calendar
Configurable PHP Calendar
PHP Calendar
PHP Image Processing
PHP grahic softwares
PHP create real-time dynamic chart
create antispam image(image verification)
Add water mark to image
PHP Image processing introduction
Add text on Image
Create thumbnail for image
Create image from text on fly
PHP code sample
PHP create dynamic PDF with FPDF
PHP create dynamic PDF with PDFLib
PHP create dynamic PDF with ClibPDF
Improved PHP configurable hit counter
Install phpMyAdmin,MySql, PHP
PhpMyAdmin export/import data
A simple PHP RSS feed reader
PHP include (SSI)
Working with Forms
PHP formmail Generator
Using phpMyAdmin Create Database
Insert Form Data into Database
Retrieve data from Database
Retrieve multiple records
Batch insert text using phpMyAdmin
Using phpMyAdmin insert binary file
Insert binary file using PHP code
Using form insert binary file into MySQL
Retrieve binary file from MySQL
Image hosting business in minutes
Send HTML mail in PHP
Send bulk emails in PHP
Polling/Rating System in PHP
Polling/Rating System in PHP(radio button)
Check visitor's IP address
Check reciprocal link
PHP tutorial
What is PHP?
Install PHP on Windows XP (part1)
Install PHP on Windows XP (part2)
PHP open and closing tags
PHP types
Variables
Constants
Operators(part1)
Operators(part2)
Logic Structures
Include files
Loops
Set cookie and delete cookie
Most often used MySQL functions

 

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer