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.
- Append to "To"
- 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.
|