TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   PHPMailer (http://www.talkphp.com/general/6042-phpmailer.html)

captainmerton 11-21-2011 10:15 AM

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.

maeltar 11-22-2011 07:43 AM

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/

captainmerton 11-22-2011 08:54 PM

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.

sketchMedia 11-23-2011 12:59 PM

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

captainmerton 11-23-2011 07:48 PM

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.

sketchMedia 11-24-2011 01:56 PM

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.

Rainman 02-19-2013 05:30 AM

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!


All times are GMT. The time now is 04:55 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0