zend smtp mail crashes after 100+ mails - php

While sending a newsletter-kind-of mail I came across this weird problem.
In a for loop, I loop through all the users in a database and try to send them all an HTML mail with some basic information. Now the thing is that the first 200 or so mails run fine, but then the script crashes and gives the following error:
Warning: fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:140D00CF:SSL routines:SSL_write:protocol is shutdown in /opt/zendframework2/library/Zend/Mail/Protocol/AbstractProtocol.php on line 263 Warning: fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:140D00CF:SSL routines:SSL_write:protocol is shutdown in /opt/zendframework2/library/Zend/Mail/Protocol/AbstractProtocol.php on line 263 Fatal error: Uncaught exception 'Zend\Mail\Protocol\Exception\RuntimeException' with message 'Could not read from smtp.gmail.com' in /opt/zendframework2/library/Zend/Mail/Protocol/AbstractProtocol.php:308 Stack trace: #0 /opt/zendframework2/library/Zend/Mail/Protocol/AbstractProtocol.php(339): Zend\Mail\Protocol\AbstractProtocol->_receive(300) #1 /opt/zendframework2/library/Zend/Mail/Protocol/Smtp.php(358): Zend\Mail\Protocol\AbstractProtocol->_expect(221, 300) #2 /opt/zendframework2/library/Zend/Mail/Protocol/Smtp.php(394): Zend\Mail\Protocol\Smtp->quit() #3 /opt/zendframework2/library/Zend/Mail/Protocol/AbstractProtocol.php(115): Zend\Mail\Protocol\Smtp->_disconnect() #4 [internal function]: Zend\Mail\Protocol\AbstractProtocol->__destruct() #5 {main} thrown in /opt/zendframework2/library/Zend/Mail/Protocol/AbstractProtocol.php on line 308
Now, I'm not to familiar with smtp, ssl & tls but I believe the most important line of the error is: 'Could not read from smtp.gmail.com'. Which doesn't make any sense to me.
We send emails all the time ( lost passwords, registration mails, etc ) and this always (to my knowledge) works fine. The script just crashes after it has sent too many mails in a short period of time.
Ok, that was the problem, now let me explain the setup :)
I'm running Zend 2.2.6 on a standard LAMP server ( PHP 5.3.10 ) and using standard SMTP mail scripts provided by Zend. We're using Google business apps as a mail client. The following is the first couple of lines of the mail script:
<?PHP
namespace Mail\Mails;
use Zend\Mail;
use Zend\Mail\Message;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Part as MimePart;
use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;
use Mail\Config\Config;
class Base
{
private $transport, $text, $html, $to, $subject;
public function __construct()
{
$config = new Config();
$transport = new SmtpTransport();
$options = new SmtpOptions(array(
'name' => 'mydomain.com',
'host' => 'smtp.gmail.com',
'port' => 587,
'connection_class' => 'login',
'connection_config' => array(
'username' => $config->username,
'password' => $config->password,
'ssl' => 'tls'
),
));
$transport->setOptions($options);
$this->transport = $transport;
// This happens in different parts of the code.
$this->subject( $subject );
$this->to( $address );
$this->html( $html );
$this->text( $text );
$this->send();
}
?>
What I've tried so far:
Running the script again
This has different outcomes: sometimes it breaks earlier, sometimes it breaks further on, but this proves the script doesn't break on a particular address. I get the feeling that the server has some kind of cooling down: the first run it processes like 200 addresses but when I rerun the script directly after, it can break after 20 addresses. When I try it like an hour later the script breaks after about 200 addresses again.
I've tried to change the ssl option to 'ssl' and the port option to '465' but this has the exact same result.
Is anybody familiar with this problem? I'm not really sure where to look for problems, maybe someone can give me a push in the right direction?
Thanks in advance!

Older site but probably your problem
http://www.serversmtp.com/en/limits-of-gmail-smtp-server
That's because Google places all sorts of limits on the use of its SMTP service by marketers. Those limits begin with a restriction on the number of recipients who can receive the same message. If Gmail's SMTP server detects that your message is going to more than 500 people, it disables your account.

Related

Laravel Imap connect() slow | reuse?

I am using laravel-imap to connect to my email server and get all mailboxes and messages.
I noticed that the connect() method is slow, it takes 4-5 sec.:
$start = microtime(true);
$oClient = new Client([
'host' => env('MAIL_HOST_IN'),
'port' => env('MAIL_PORT_IN'),
'encryption' => env('MAIL_ENCRYPTION_IN'),
'validate_cert' => true,
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'protocol' => 'imap'
]);
// Connect to the IMAP Server
$oClient->connect();
\Log::info('Connect: ' . strval(microtime(true) - $start));
Thats it, there is no additional code. I also use getFolders() and loop through them to get messages.
I count/get unseen messages and do more operations, but they are all fast.
I am asking this, because if I login into my mailbox through the website, the performance is much better 1-2 sec. And the operation is the same, I get the mailboxes and a count on unseen messages. So why it is slow via script?
In case it simply is like that and I cant change it, what are some possible solutions? The only idea I have right now it to reuse the connection in multple functions, so at least I connect only once.
Thanks #everyone who commented.
I can confirm now that there is no issue with the code/package.
It seems that something on my local machine was responsible for the slow connect time.
I dont know exactly what it was, maybe a broken firewall.
I uploaded the code to my server. The performance was much better, it connected immediately.

MQTT Subscribe with PHP to IBM Bluemix

I want to connect to IBM Bluemix through the MQTT protocol using PHP to subscribe to messages come from IoT Foundation.
I use this code:
<?php
require("../phpMQTT.php");
$config = array(
'org_id' => 't9m318',
'port' => '1883',
'app_id' => 'phpmqtt',
'iotf_api_key' => 'my api key',
'iotf_api_secret' => 'my api secret',
'device_id' => 'phpmqtt'
);
$config['server'] = $config['org_id'] .'.messaging.internetofthings.ibmcloud.com';
$config['client_id'] = 'a:' . $config['org_id'] . ':' .$config['app_id'];
$location = array();
// initialize client
$mqtt = new phpMQTT($config['server'], $config['port'], $config['client_id']);
$mqtt->debug = false;
// connect to broker
if(!$mqtt->connect(true, null, $config['iotf_api_key'], $config['iotf_api_secret'])){
echo 'ERROR: Could not connect to IoT cloud';
exit();
}
$topics['iot-2/type/+/id/phpmqtt/evt/+/fmt/json'] =
array("qos"=>0, "function"=>"procmsg");
$mqtt->subscribe($topics, 0);
// process messages
while ($mqtt->proc(true)) {
}
// disconnect
$mqtt->close();
function procmsg($topic, $msg) {
echo "Msg Recieved: $msg";
}
?>
But the browser show this message:
Fatal error: Maximum execution time of 30 seconds exceeded in /Library/WebServer/Documents/phpMQTT/phpMQTT.php on line 167
subscribe is not meant to run in the web browser as it has an infinite look, its best being run from the command line.
If you are using the subscribe method to receive messages you can look at persistent msgs and breaking out of the loop on msg receipt.
There is an example of how to use phpMQTT in the web browser in the file
web-app.php of this respository https://github.com/vvaswani/bluemix-iotf-device-tracker
You don't provide very much information about what you want to achieve by doing this; do you want to keep sending messages to the browser until the page is closed in the browser?
Server Sent Events or Websockets might be a better bet, and PHP might not be the best choice for this, because it uses up quite a lot of memory per connection (compared to node.js for example).
However if you just want to remove the 30 second PHP timeout, then you can use this function:
http://php.net/manual/en/function.set-time-limit.php
Or set max_execution_time in php.ini:
http://php.net/manual/en/info.configuration.php
Setting the maximum execution time to 0 should stop it from timing out.
But be warned that PHP and/or your webserver will have a limited number of concurrent HTTP connections.

Uable to connect Splunk using php sdk

I am trying to connect Splunk using PHP SDK but I am getting following errors:
https://api.9xnn-6gtz.data.splunkstorm.com:80/services/auth/login Warning: array_shift() expects parameter 1 to be array, null given in /home/u354848659/public_html/oauth/splunk/Splunk/Http.php on line 124
Warning: Invalid argument supplied for foreach() in /home/u354848659/public_html/oauth/splunk/Splunk/Http.php on line 126
Login Failed. Reason: String could not be parsed as XML
$SplunkExamples_connectArguments = array(
'host' => 'example.com',
'port' => '80',
'username' => 'xxxx',
'password' => 'xxxx',
'token' => 'xxxxxxxxxxx',
);
I passed all above details. But when I am trying to run it is not working. Kindly help me out in resolving the issue at the earliest. Thanks
You need to give more code of what you're doing with this. One of your warning messages states you're passing a null, Here you're showing us an array, which gives me some ideas of what might be wrong, but you're not showing us how you use this array. You should try putting together a SSCCE.
From the getting started guide, the example does not pass a token. The reason for this, if you dig into the documentation you'll see that if you provide a token, then the username and password is ignored and you don't need to call login().
Furthermore, using the PHP SDK (and other SDKs), you need to hit the Splunk REST API port, which by default is port 8089, not port 80. This is configurable of course, so you should double check with your Splunk Administrator.

CakePHP emails suddenly stopped working

I have been using CakePHP's email class for close to a year now. The system has been sending html emails without problems. Two days ago I received reports that customers were not receiving their notifications. I noticed as well that I wasn't receiving my admin notifications.
My first thought was something changed with my server (hosted by GoDaddy). I do not believe it is my hosting provider as I have tested a php's raw mail() function and even sent a quick text email through CakePHP's class. I also tested each debug mode and error logs, nothing to indicate an error occurred. Here is the code I have been using (successfully) since a couple days ago. This code is the same around 10 times throughout my app.
$Email = new CakeEmail();
$Email->template('order_status_change', 'default')
->emailFormat('html')
->to($order['Order']['email'])
->subject('blah blah ' . $order['Order']['id'] . ' status change: ' . $status['Status']['label'])
->from(array('myemail#gmail.com' => 'blah blah'))
->viewVars(array(
'id' => $order['Order']['id'],
'firstName' => $order['Order']['first_name'],
'statusLabel' => $status['Status']['label'],
'statusDescr' => $status['Status']['description']
))
->send();
A couple things to note:
I have confirmed that the html template 'order_status_change' and
default layout are in the correct folders (they were working before)
I have confirmed that the variables are all correctly set at the
time of email
If I have to switch to text emails I will but I cannot seem to fathom how the code would suddenly stop working. The only thing I have changed is the debug mode (which I have been doing since the beginning).
Update
Emails stopped working again so I switched over to text mail. However for some odd reason when I put a url in the body (like http://www.google.com) the mail fails. However if I remove just the .com then it will go through. What the hell is happening?
Update 2
This is my current config that will send out text emails without links in them (when I say links I mean a url string)
class EmailConfig {
public $godaddy = array(
'transport' => 'Smtp',
'host' => 'relay-hosting.secureserver.net',
'port' => 25,
'timeout' => 300,
'log' => true,
'charset' => 'utf-8',
'headerCharset' => 'utf-8'
);
}
I confirmed that Go Daddy does not need authentication and the port is 25.

AWS SDK Guzzle error when trying to send a email with SES

Getting a weird error I have no idea how to fix.
This is the error:
( ! ) Catchable fatal error: Argument 2 passed to Guzzle\Service\Client::getCommand() must be an array, string given, called in phar://C:/wamp/www/PHPCodeLance/WebTech/Projects/MIB v2/lib/aws/aws.phar/vendor/guzzle/guzzle/src/Guzzle/Service/Client.php on line 93 and defined in phar://C:/wamp/www/PHPCodeLance/WebTech/Projects/MIB v2/lib/aws/aws.phar/vendor/guzzle/guzzle/src/Guzzle/Service/Client.php on line 113
Call Stack
# Time Memory Function Location
1 0.0009 676280 {main}( ) ..\test.php:0
2 0.0557 3311632 Aws\Ses\SesClient->send_email( ) ..\test.php:30
3 0.0557 3312128 Aws\Common\Client\AbstractClient->__call( ) ..\test.php:30
4 0.0557 3312208 Guzzle\Service\Client->__call( ) ..(null):103
5 0.0557 3312296 Guzzle\Service\Client->getCommand( ) ..(null):93
This is the code I used (straight from the AWS page)
$client = SesClient::factory(array(
'key' => '',
'secret' => '',
'region' => 'us-east-1'
));
$response = $client->send_email(
'no-reply#amazon.com', // Source (aka From)
array('ToAddresses' => array( // Destination (aka To)
'myemail#hotmail.nl'
)),
array( // Message (short form)
'Subject.Data' => 'Email Test ' . time(),
'Body.Text.Data' => 'This is a simple test message ' . time()
)
);
// Success?
var_dump($response->isOK());
UPDATE!!!:
Fixed the issues above, now I got an SSL certificate issue:
Guzzle\Http\Exception\CurlException: [curl] 60: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed [url] https://email.us-east-1.amazonaws.com/ in phar://C:/wamp/www/PHPCodeLance/WebTech/Projects/MIB v2/lib/aws/aws.phar/vendor/guzzle/guzzle/src/Guzzle/Http/Curl/CurlMulti.php on line 578
Thanks in advance
For an answer to the first (now supposedly resolved - HOW?) issue, see AWS SDK Guzzle error when sending email with SES
Please, if you have a solution to an issue, particularly one as arcane as this, POST IT for others to use.
First of all, it seems that you should include this code for instantiating the client and sending the email within a try-catch block, that will certainly resolve the Catchable fatal error part and allow your code to continue executing.
As far as the getCommand parameter problem, my guess is that there is some issue with your arguments to send_email() that are passed down the call stack. Without digging through the SDK I don;t know off the top of my head what arguments are specifically passed to getCommand, but you have all the information you need there to debug the issue, as you should be able to map how your arguments are passed through each of the calls shown in the stack trace, debugging along the way to verify what is passed to each function is what is expected.
The problem with the SSL is because CURL does not bundle CA certs anymore, you'd need to set the proper CA info.
Solution 1 (Changes to PHP.ini):
Download CA bundle (cacert.pem) from http://curl.haxx.se/docs/caextract.html
Place it on your local system (for eg: C:\xampp\cacert.pem)
Open your php.ini
Set the curl.ca_info options to point to the location of the cacert.pem
Example: curl.ca_info="C:\xampp\cacert.pem"
Restart Apache
Solution 2 (Set options before each CURL call)
Download CA bundle (cacert.pem) from http://curl.haxx.se/docs/caextract.html
Place it on your local system (for eg: C:\xampp\cacert.pem)
Write the following code:
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt ($ch, CURLOPT_CAINFO, "pathto\cacert.pem");
Source: http://tumblr.wehavefaces.net/post/52114563111/environment-windows-xampp-curl-library

Categories