UK WEB HOSTING FORUM FOR DISCUSSION ON WEB HOSTING SERVICE AND SUPPORT
LINUX HOSTING WINDOWS HOSTING PACKAGES SHOPPING CART OSCOMMERCE ZEN CART AGORA
ECOMMERCE HOSTING ASP MSSQL FRONTPAGE HOSTING PHP MYSQL HOSTING DISCUSSION FORUM
CPANEL RESELLER HOSTING DEDICATED SERVER VPS HOSTING PLESK VIRTUOZZO
Quick Search
Your forum announcement here!

  UK Web Hosting | Dedicated Server Windows and Linux VPS Forum > Web Hosting and Domains > PHP Hosting

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 30-12-2007, 15:19
borninblood's Avatar
Premium Member
 
Join Date: May 2007
Posts: 102
Default Sending Email in PHP

Does anyone know a good method of sending emails en mass that are going to email addresses taken from a database.

Currently I use a rather poor method -

PHP Code:
while($row=mysql_fetch_array($result))
{
$to "$row[email]";
$subject "Title";
$body "Body Content";
$headers "From: webmaster@borninblood.co.uk\r\n" .
    
"X-Mailer: php";
if (
mail($to$subject$body$headers)) {
  echo(
"<p>Message sent!</p>");
 } else {
  echo(
"<p>Message delivery failed...</p>");
 }
}

But what happens when I run is the site goes into 403 meltdown assumedly because the server assumes this is a security breech.

Any advice would be greatly appreciated.
__________________
BornInBlood (Metal) || SimplyTied (Bondage)
Reply With Quote
  #2 (permalink)  
Old 02-01-2008, 13:07
mephisto's Avatar
Member
 
Join Date: Feb 2007
Location: Darlington
Posts: 99
Send a message via ICQ to mephisto Send a message via MSN to mephisto
Default

Looks like you should setup a mail queuing system. Try having a look at using Mail_Queue from pear.

http://pear.php.net/package/Mail_Queue
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 07:51
Junior Member
 
Join Date: Jan 2008
Posts: 11
Default

I use something similar and have had no problems:

while($row=mysql_fetch_row($result)) {
$name=$row[0];
$email=$row[1];
$body="";
$headers=$head . "Content-type: text/html; charset=iso-8859-1" . $eol . $eol;
$body="<p> Dear " . $name . ",</p>";
$body.=$tb;
mail($email, $subject, $tbody, $headers);
echo("<br>sent to " . $email );
$body="";
}

I've left out the unsubscribe link and other stuff for clarity.

The only 'problem' I now think I might have is some people's newsletters get classed as spam by Yahoo and others.
This isn't a general thing, but it does happen.
And when I look at the headers of my copy sent to me, it's got:

Sender: Nobody <nobody@sutherland.eukhost.com>

Anyone know a way around this?

I've got:

$head="Reply-To: \"Smallholders Online Newsletter\" <newsletter@smallholders.org>\n";
$head.="Return-Path: <newsletter@smallholders.org>\n";
$head.="From: \"Smallholders Online Newsletter\" <newsletter@smallholders.org>\n";

in the email header.

Ron.
Reply With Quote
  #4 (permalink)  
Old 04-01-2008, 11:32
mephisto's Avatar
Member
 
Join Date: Feb 2007
Location: Darlington
Posts: 99
Send a message via ICQ to mephisto Send a message via MSN to mephisto
Default

You need to use smtp rather than php's native mail function. This example uses the PEAR package Mail.

PHP Code:
<?php
    
require_once ('Mail.php');
    
    
// details of options can be seen at http://pear.php.net/manual/en/package.mail.mail.factory.php
    
$options = array(
        
'host' => 'mail.yoursmtpserver.com',
        
'port' => 25 // or what ever yours is set at,
        
'persist' => true// good for sending multiple emails
    
);
    
    
//If you need smtp authentication then add your type (detection howto below) and credentials    
    
    
$options array_merge($options, array(
        
'auth' => 'LOGIN',
        
'username' => 'yourusername',
        
'password' => 'yourpassword'
    
));
    
    
$mailer =& Mail::factory('smtp'$options);
    
    if(
PEAR::isError($mailer)) {
        die(
'Could not create mailer: ' $mailer->getMessage());
    }
    
    
$recipients 'who@where.com';
    
$headers = array(
        
'From' => 'you@where.com',
        
'To' => 'who@where.com',
        
'Subject' => 'Test Message',
        
'Date' => date('r')
    );
    
    
$body 'email body';
    
    
$error $mailer->send($recipients$headers$body);
    if(
PEAR::isError($error)) {
        die(
'Failed to send email: ' $error->getMessage());
    }
?>
To detect the supported auth types from your mailer server do, server responses in bold

Code:
telnet mail.yoursmtpserver.com 25
220 mail.yoursmtpserver.com NO UCE ESMTP
ehlo localhost
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-PIPELINING
250 8BITMIME
The supported auth types are after the 250-AUTH response, LOGIN, PLAIN etc.

You can of course expand on this further by sending multipart email, text/plain and text/html so that more users can see your email.

PHP Code:
<?php
    
require_once('Mail.php');
    require_once(
'Mail/mime.php');
    
    
$options = array(
        
'host' => 'mail.yoursmtpserver.com',
        
'port' => 25 // or what ever yours is set at,
        
'persist' => true// good for sending multiple emails
        
'auth' => 'LOGIN',
        
'username' => 'yourusername',
        
'password' => 'yourpassword'
    
));
    
    
$mailer =& Mail::factory('smtp'$options);
    
    if(
PEAR::isError($mailer)) {
        die(
'Could not create mailer: ' $mailer->getMessage());
    }
    
    
$recipients 'who@where.com';
    
$headers = array(
        
'From' => 'you@where.com',
        
'To' => 'who@where.com',
        
'Subject' => 'Test Message',
        
'Date' => date('r')
    );
    
    
$txtbody 'email body';
    
$htmlbody '<p>email body</p>';
    
    
$crlf "\n";    
    
$mime = new Mail_mime($crlf);
    
    
$mime->setTXTBody($txtbody);
    
$mime->setHTMLBody($htmlbody);
    
    
$body $mime->get();
    
$headers $mime->headers($headers);
    
    
$error $mailer->send($recipients$headers$body);
    if(
PEAR::isError($error)) {
        die(
'Failed to send email: ' $error->getMessage());
    }
?>
enjoy
Reply With Quote
  #5 (permalink)  
Old 04-01-2008, 12:35
Junior Member
 
Join Date: Jan 2008
Posts: 11
Default

Wow.
Thanks Mephisto.
Lots to get my teeth into there.
Should keep me occupied for a while.

Thanks again.

Ron
Reply With Quote
  #6 (permalink)  
Old 04-01-2008, 13:04
borninblood's Avatar
Premium Member
 
Join Date: May 2007
Posts: 102
Default

Quote:
Originally Posted by mephisto View Post
Looks like you should setup a mail queuing system. Try having a look at using Mail_Queue from pear.

http://pear.php.net/package/Mail_Queue
Sounds perfect, I will have a wee read of it, cheers!
__________________
BornInBlood (Metal) || SimplyTied (Bondage)
Reply With Quote
  #7 (permalink)  
Old 10-01-2008, 08:28
Junior Member
 
Join Date: Jan 2008
Posts: 11
Default

So far so good.

I'm going to PEAR's Mail Factory.
The first try sent the mail server into panic mode after about 50 had been sent.
Not surprising really.

Not keen on a queue system that uses a database, which might be huge and anyway all the mail addresses are coming from a database already.
So rather than put a ginormous delay after each 'send', it would be nice to have some feedback when the mail server had sent each one, then I could step onto sending the next.

Is there any way this can be done?

Ron
Reply With Quote
  #8 (permalink)  
Old 19-01-2008, 20:40
mephisto's Avatar
Member
 
Join Date: Feb 2007
Location: Darlington
Posts: 99
Send a message via ICQ to mephisto Send a message via MSN to mephisto
Default

You could try something like

PHP Code:
// build up Mail instance above
$recipients = array();

// add recipients into array
$recipients[] = 'me@mydomain.com';
// ...

foreach($recipients as $recipient) {
   if(
PEAR::isError($result $mail->send($recipient$headers$body)) {
      
// log not sent, or how ever you handle the fact they are not sent
      
echo $result->getMessage();
   }

Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 19:13.

 

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by Web Hosting 3.1.0
Copyright © 2001-2008, eUKhost.com. All rights reserved.

 
Site Map

knowledgebase articles

popular blog categories