How to send news letters in zend - php

i am trying to send something like news letter Via Zend_Mail but after 12 mail i got this message
Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files\Zend\Apache2\htdocs\forga\library\Zend\Mail\Protocol\Abstract.php on line 321
my Code is like:
$smtpHost = 'smtp.gmail.com';
$smtpConf = array(
'auth' => 'login',
'ssl' => 'tls',
'port' => '587',
'username' =>'xxxxx#xxxxx.com',
'password' => 'xxxxxxxx'
);
$transport = new Zend_Mail_Transport_Smtp($smtpHost, $smtpConf);
foreach($users as $user)
{
$mail = new Zend_Mail();
$mail->setFrom("noreply#forga.com", 'Forga');
$mail->setSubject($subject);
if($html=='on')
$mail->setBodyHtml($message);
else
$mail->setBodyText($message);
$mail->addto($user);
$transport->send($mail);
}

From the script timeout you get I'd assume your host is slow to send eMails and simply cannot handle bulk sending of eMails. You could increase the time until a script times out with
set_time_limit — Limits the maximum execution time
A more elegant way would be to send the eMails in separate processes asynchronously. Check out
Mysteries of Asynchronous Processing by Padraic Brady Part 1, 2 and 3.
Part 3 deals with eMails specifically.

Check out my answer on another posting on parallel processing in PHP Multithreading/Parallel Processing in PHP. I think it's relevant. If you need something to be done outside of the individual request it should probably be passed to some kind of queue.

Do you manage the web server yourself?
Up to version 5.3.0, set_time_limit() only works in safe mode. (deprecated in recent PHP).
You can set safe mode in php.ini.
See also max_execution_time in php.ini.

By the looks of your code your sending a single email to each on individually from your server.. Try doing the following.
$smtpHost = 'smtp.gmail.com';
$smtpConf = array(
'auth' => 'login',
'ssl' => 'tls',
'port' => '587',
'username' =>'xxxxx#xxxxx.com',
'password' => 'xxxxxxxx'
);
$transport = new Zend_Mail_Transport_Smtp($smtpHost, $smtpConf);
$mail = new Zend_Mail();
$mail->setFrom("noreply#forga.com", 'Forga');
$mail->setSubject($subject);
$html=='on' ?$mail->setBodyHtml($message) : $mail->setBodyText($message);
foreach($users as $user)
{
$mail->addto($user);
}
$transport->send($mail);
No i might actually be wrong with this because the other email addresses may be visible to all recipients, im not sure if that's only the concerning section of the email.
Regards

Related

Delay due to email send PHP CodeIgniter

First of all, I would like to thank StackOVerflow and its users for helping me solve a number of technical problems. I created this account today since I could not see similar problem being addressed.
The problem - I am stuck with a problem with sending emails in PHP CodeIgniter. The function $this->email->send() is taking around 3-4 seconds each time to execute.
We have a social platform where users come and post things. Every time the user uploads a new update we want to send an email to him/her.
The problem is the send email function takes around 3-4 seconds and we would love to bring it down under 1 second.
IS there any way wherein we can execute the send email process in parallel ? Say a python code which runs continuously to fetch new updates and send emails to those users. Any better method than that ?
Technology stack - PHP CI, MySQL, Apache, Windows
This is the email code that gets called on every new update -
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => 587,
'smtp_user' => 'mailid#gmail.com',
'smtp_pass' => 'password',
'mailtype' => 'html',
'charset' => 'utf-8',
'smtp_crypto' => "tls"
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->set_mailtype("html");
$this->email->from('mailid#gmail.com', 'Support');
$this->email->to($this->session->userdata['email']);
$this->email->subject('You have posted a new update');
$this->email->message('Please login to check your new update. Do not reply to this email. For any issues reach out to email#emailid.com');
if (!$this->email->send())
{
show_error($this->email->print_debugger());
}
else
{
echo true;
}
If your function is working faster but email sending is slower, it must be problem for your smtp connection. Either your server is not much faster to connect SMTP quickly or the SMTP you are using bit slower to response.
In that case, try with a gmail SMTP and see the result.
It's my suggestion to store the emails in a database and send the emails using a cron job.
The uploading user should not wait to send emails one at a time .
If you need code for a cron job or an smtp mailer, add a comment to this answer.

Codeigniter Email - `554 SMTP synchronisation error

I'm trying to send email using codeIgniter's email library.
Below is my code
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.myserver.com',
'smtp_user' => 'xxx#myserver.com',
'smtp_pass' => '********',
'mailtype' => 'html',
'wordwrap' => false,
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$this->email->from('xxx#yourserver.com', 'xxx');
$this->email->subject('blah blah');
$this->email->message('a simple html message');
$this->email->to('any#validemail.com');
$this->email->send();
Code is on diffrent server & it is using different server's mail (Yes, two different domains)
For example code is on yourserver.com and it is using smtp of myserver.com
It was working fine till morning. but now i'm getting
554 SMTP synchronisation error (see attachment for full print_debugger() output, I hide some sensitive information. i can trust you guys but not all)
Thanks.
I tried very hard to get the issue, but i have not enough access to get details of error log or mail logs, So i changed my code as below and it worked
$config = Array(
'mailtype' => 'html',
'wordwrap' => false,
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$this->email->from('xxx#yourserver.com', 'xxx');
$this->email->subject('blah blah');
$this->email->message('a simple html message');
$this->email->to('any#validemail.com');
$this->email->send();
I would like to say thanks to #cherrysoft for his help offering. I'm posting it here so it may help someone.
thanks to all.
This isn't really a CodeIgniter problem and there could be quite a few things going on here but the first thing I would do is to make sure that the hostname on the web server is set correctly and that your mail server recognises this host. You should tail the mail.log on your mail server (if you have access to do this) whilst you are sending mail from your web server and you will get some more insight as to what is going on. When you have the mail log entries post here and we can look at the issue in more depth.

Setting up Mail (PHP) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Send email using GMail SMTP server from PHP page
I'm pretty new to mail in PHP, and I am wanting to set up mail().
The problem is, after a few hours of trying to get it working, I simply can not!
I am wanting this to happen:
I want to send emails to the users on my website via my gmail address.
I am not sure WHERE i configure SMTP for gmail. Do i edit the settings in php.ini (ssl:smtp.gmail.com; 465)?
Is there a way to send emails using the PHP mail() function without the need to use something like pear? I am just wanting to use the mail() function.
If that is not possible, is there a way I can send emails to my users via the localhost setup?
I am pretty confused after looking around for answers during the past few hours.
Any help would be greatly appreciated!
The easiest way I've found to get PHP to send mail using SMTP is via the Mail Pear package.
This way you don't have to involve and obese third party libraries like PHPMailer.
Here's an example:
<?php
require_once "Mail.php";
$headers = array(
'From' => "Sandra Sender <sender#example.com>",
'To' => $to="Ramona Recipient <recipient#example.com>",
'Subject' => "Hi!"
);
$smtp = Mail::factory('smtp', array(
'host' => "ssl://smtp.gmail.com",
'port' => 465,
'auth' => true,
'username' => "smtp_username",
'password' => "smtp_password"
));
$body = "Hi,\n\nHow are you?";
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
}
else {
echo "mail sent successfully";
}

PHP mail hangs indefinitely - How to debug?

I've set up a simple mail system, which resembles the following:
$from = 'me <me#me.com>';
$to = 'you <you#you.com>';
$subject = 'subject';
$body = 'body';
$host = 'www.me.com';
$headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::Factory('smtp', array('host' => $host, 'auth' => true,
'username' => 'username', 'password' => 'password'));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
throw new Exception('emailException');
}
When I try run the script however the browser (chrome) hangs indefinately. There is no exception thrown, no error page shown by the browser, it just sits and waits for a response (for over 12 minutes, longest I've left it).
My actual program is slightly different (the code is enclosed in a function with info passed as parameters). I have used static debugging and confirmed that the parameters getting passed in are correct, however it just never errors, so I can't even test any error information in $mail.
Does anyone know how I should go about debugging this?
Update I can confirm the page hangs when calling $smtp->send(...)
Debugging this could be done in the logfiles, there you might find some PEAR errors.
Futhermore you could debug by echo statements between each line of code so you know exactly where it hangs.
Long delays are likely to incur when trying to connect to a non existing host. So that's where I would start searching although 12 minutes is very very long. You need to use the PEAR class because you need to send through a designated SMTP server?
IME, more than 99% of reported errors regarding PHP's mail functionality are problems with mail and neither PHP nor it's interface.
Why are you trying to connect directly to the server instead of using PHP's mail()?
Do you have shell access to the machine? It makes the following simpler however you can do these from PHP.
Can you resolve the smtp server hostname?
Can you connect to port 25 on the smtp server?
How is authentication implemented? (should be included in the response to your EHLO)
If you've got shell access then route the connection via a logging proxy or capture usnig tcpdump/wireshark and see where the connection is getting stuck.
Most likely the mailserver is not implementing proper SMTP and the authentication is going awry.

PHP: PEAR Mail connecting but not sending (no error)?

I'm using PEAR's Mail package to send email from my script. I'm pretty sure I have everything connected and declared properly, but when the script runs, it just connects then immediately disconnects to my Mail server without actually sending the email.
From my Postfix logs:
Nov 18 16:15:49 mailer postfix/smtpd[30346]: connect from xxx-xxx-xxx-xxx.static.cloud-ips.com[xxx.xxx.xxx.xxx]
Nov 18 16:15:49 mailer postfix/smtpd[30346]: disconnect from xxx-xxx-xxx-xxx.static.cloud-ips.com[xxx.xxx.xxx.xxx]
What gives?
<?php
require_once('Mail.php'); // loads in PEAR Mail package
$mailer_params['host'] = 'mailer.example.com';
$mailer_params['port'] = 25;
$mailer_params['auth'] = true;
$mailer_params['username'] = 'user#mailer.example.com';
$mailer_params['password'] = 'password';
$mail =& Mail::factory('smtp', $mailer_params);
$headers = array(
'From' => 'user#example.com',
'Reply-To' => 'user#example.com',
'Subject' => 'Test Email'
);
$message = "whatever";
$mail->send('Test <other.user#example.com>', $headers, $message);
?>
I know my Postfix server works, since I have several other applications using it without problems. The user credentials are the same in this script as they are for those other apps.
My Postfix server is using SASL_auth (configured with CRAM-MD5), if that helps. I wish I had an error message or something on either the PHP side or the Postfix side to go on, but all it does is just connect then disconnect with no other explanation.
I had this problem a few days ago. Try $mailer_params['auth'] = 'CRAM-MD5' and also for extra information, try $mailer_params['debug'] and run the script from the command line. If that still doesn't work, try $mail_params['auth'] = 'LOGIN'.
Hope this helps.
Here is the first thing I'd try, see if you can get an exception error from PHP:
<?php
try {
require_once('Mail.php'); // loads in PEAR Mail package
$mailer_params['host'] = 'mailer.example.com';
$mailer_params['port'] = 25;
$mailer_params['auth'] = true;
$mailer_params['username'] = 'user#mailer.example.com';
$mailer_params['password'] = 'password';
$mail =& Mail::factory('smtp', $mailer_params);
$headers = array(
'From' => 'user#example.com',
'Reply-To' => 'user#example.com',
'Subject' => 'Test Email'
);
$message = "whatever";
$mail->send('Test <other.user#xxx.com>', $headers, $message);
} catch (Exception $e) {
echo "Exception: " . $e->getMessage();
}
And I have some other questions, out of curiousity:
You mentioned your postfix server works with other applications, are they on the same server? Is this a remote request, or an application on the same server as the mail
Can you reverse engineer anything on the working server to see what's being done differently?
Are you sending email from the same domain as what's on the server?
Some of the basis behind question 1 and 3 is the fact that a great deal of hosts either block or put restrictions on mailing. This is because spammers will create accounts and abuse them until they are banned. This makes sending mail difficult for the rest of us honest people, but it happens every day.
I hope this gives some food for thought, reply back and let's see if we can find the problem.

Categories