TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-21-2011, 10:15 AM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default PHPMailer

I am looking for some guidance on using the phpMailer class.

I am currently calling the php mail() function in a loop to take input from a contact form on my website and send it to 60 email recipients - its is running like a dog and i realise i need something better. I believe doing this is calling starting the whole process with the mail server for every single email request. Will phpMailer improve this performance? How easy is it to implement? Cheers.
captainmerton is offline  
Reply With Quote
Old 11-22-2011, 07:43 AM   #2 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

I use Swift Mailer, for my mailing needs which can be quite demanding, and never had a problem with it..

When I'm doing mailing I tend to run the script from a cron job so it doesn't impact the webpage, as with each itteration or mail sent you can't leave the page so you are in limbo, whereas running a php script from a cron job every hour or whatever period you need it gets you away from that issue. As long as you have your database setup well, maybe add a field with a "mailsent" type bool, int set it 1 or 0 as required, then it sends to the required recipients dependant on a small query...

http://swiftmailer.org/
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 11-22-2011, 08:54 PM   #3 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Thanks Maeltar. A cron job is the way i want to go eventually but at present i dont think my hosting package has it. Therefore i wanted to do something to better process real time bulk mail sending. I am sure i read somewhere that if you use the standard php mail() function and loop it to send for example 50 emails it runs like a dog as its opening,processing then closing the mail protocol or something as opposed to just opening the protocal sending 60 emails then closing it again. Not particularly well explained but its clear looping on the php mail() function is causing some kind of processing issues around how the mail protocol works on the php installation. Needinhg a quick fix and i believe phpMailer will do that but looking on guidance re. how best to use.
captainmerton is offline  
Reply With Quote
Old 11-23-2011, 12:59 PM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

mail() runs like a dog because, as you say, it will close the socket after each mail is sent, thus making it have to re-open again.

mail is good because its portable, but its not very good at sending multiple emails and also its very easy to fraff up the mail headers and cause your mail to be flagged as spam. also you must take into account your hosts send limit, and try to throttle the amount of emails sent by using sleep. Its all abit of a pain tbh.

SwiftMailer can do this properly, I haven't used PHPMailer so I can't say if it will or not.

First I think I should explain how mail() works across platforms.

If you are on a sane OS mail() uses sendmail (unless configured to use SMTP in the php config)
However if you are using a redmond operating system, for what ever reason (sane reasons for using windows so far have escaped my knowledge), mail() will be using SMTP anyway as sendmail doesn't exist on winblows, 'coz its bad :).

Swiftmailer provides tools for connecting to both SMTP and sendmail (which ends up being smtp anyway ... but I digress)

OK!

You will need to know either of the 2 following:
1. The path of the installed MTA binary of your server (sendmail, Postfix, Exim) if different from default: /usr/sbin/sendmail
2. SMTP server details

For this example I'm gonna use the sendmail method, using the default binary.
PHP Code:
//Create the Transport
$transport Swift_SendmailTransport::newInstance(); 
/*to specify the binary just stick it in the params for 'newInstance()' 
 * e.g. $transport = Swift_SendmailTransport::newInstance('/<path>/sendmail <flags>'); 
 */

//Create the Mailer using the transport above
$mailer Swift_Mailer::newInstance($transport);

//Create the message
$message Swift_Message::newInstance('This is an imaginative subject')
  ->
setFrom(array('noreply@xxx.xxx' => 'The amazing Bob Carolgees mailing list!'))
  ->
setBody('Here is the message itself')
  ;

//this is my array of addresses!
$to = array(
  
'receiver@domain.org'
  
'sam@domain.org' => 'Sam'
);

foreach (
$to as $address => $name)
{
  if (
is_int($address)) {
    
$message->setTo($name);
  } else {
    
$message->setTo(array($address => $name));
  }

  
$mailer->send($message);

You can also add a failed list:

PHP Code:
//Create the Transport
$transport Swift_SendmailTransport::newInstance();

//Create the Mailer using the transport above
$mailer Swift_Mailer::newInstance($transport);

//Create the message
$message Swift_Message::newInstance('This is an imaginative subject')
  ->
setFrom(array('xxx@xxx.xxx' => 'Bob Carolgees'))
  ->
setBody('Here is the message itself')
;

//this is my array of addresses!
$to = array(
  
'receiver@domain.org'
  
'sam@domain.org' => 'Sam Roberts'
);

$fail = array();

foreach (
$to as $address => $name)
{
  if (
is_int($address)) {
    
$message->setTo($name);
  } else {
    
$message->setTo(array($address => $name));
  }
  
$mailer->send($message$fail);

$fail will hold an array of emails that the system couldnt send to.

more info on sending mails and transport types: Send Docs


Plugins are by far the best reason to use Swift.

more information: Plugin Docs
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 11-23-2011, 07:48 PM   #5 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Thanks sketchMedia this looks pretty straightforward and will easily into the class i have created for sending of all mails.

I aint got a clue about any of the set ups i have though. So i develop on my personal laptop with php and mysql installed on windows XP. I cant send emails anyway as there is no mail server.

On the hosted webspace environment i am sure it is something like CentOS or something. As i aint doing anything complicated havent really found any greate issue loading the php code developed and tested on Windows onto the Unix shared server environment. Any ideas how i would likely need to call Swiftmailer on the shared hosted enviorponment to make it work? Cheers.
captainmerton is offline  
Reply With Quote
Old 11-24-2011, 01:56 PM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

CentOS is based on RHEL I think, so I'd wager your sendmail path is default: '/usr/sbin/sendmail', so you shouldnt need to specifiy a sendmail path and it should work fine.

As for your development env, why dont you set your gmail to be a smtp, so then you can test the functionality of the code before pushing it into the wild.

change the transport to be:
PHP Code:
$transport Swift_SmtpTransport::newInstance('smtp.gmail.com'465'ssl')
  ->
setUsername('me@gmail.com')
  ->
setPassword('pass'); 
and the rest should work as is.

Google will most likly have a form of send limit, so you might wanna check before using it to send bulk messages.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
annieblack13 (03-14-2012)
Old 02-19-2013, 05:30 AM   #7 (permalink)
The Wanderer
 
Join Date: Feb 2013
Posts: 17
Thanks: 0
Rainman is on a distinguished road
Default

Welcome to mmoggg website to buy RS Gold, offer a lot, of course, Diablo 3 Gold and Cheap RS Gold, to be purchased at any time, at any time shipment, and Diablo 3 Gold Kaufen look forward to your visit!
Rainman is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHPMailer don't work for me -.-.. Nor General 7 02-10-2009 05:57 AM


All times are GMT. The time now is 03:47 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design