使用PHP CodeIgniter框架進行開發時發現,當循環發送38封郵件時出現以下錯誤,並且發送很少的幾封郵件,上網查了很多資料,大多數人都使用了sleep(5)解決。但我使用sleep(5),甚至sleep(10)都無法將38封郵件全部發送成功,後來改爲sleep(15)總算解決問題,但發送時間偏長,用了15分鐘。最後,查詢框架源代碼和php手冊,發現瞭解決辦法,最終發送38封郵件僅用4分鐘,並且沒有提示錯誤。
一、開發環境:
Apache/2.4.29 (Win64) OpenSSL/1.0.2n PHP/5.6.38
二、提示問題:
A PHP Error was encountered
Severity: Warning
Message: fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:1420C0CF:SSL routines:ssl_write_internal:protocol is shutdown
Filename: libraries/Email.php
Line Number: 2268
---------------------------------------------------
A PHP Error was encountered
Severity: Warning
Message: fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:1409E10F:SSL routines:ssl3_write_bytes:bad length
Filename: libraries/Email.php
Line Number: 2268
-----------------------------------------------------
三、問題解決:
Search 'fsockopen' from PHP Manual:
Trying to fsockopen an ssl:// connection, but it was hanging for a long time, ignoring timeout parameter, and eventually returning false, without an error message.
Turns out (at least) on Windows it validates certificates and doesn't have a valid certificate store.
This worked for me:
- download Mozilla's certificate store from https://curl.haxx.se/ca/cacert.pem
- copy it inside php installation (for example: c:\php)
- and set it up in php.ini
openssl.cafile="c:\php\cacert.pem"