having issue with fsockopen while sending mail with swiftmailer - php

I was playing around the new swiftmailer 4.0.4 using my zend studio embbeded apache server where i can send successfully emails through gmail smtp.
Now that it's was fine and that i decided to use it in real project and this time along using xampp 1.6.8.any time i run the same function there is this error
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) in C:\xampp\htdocs\project\includes\swift\classes\Swift\Transport\StreamBuffer.php on line 243
i've tried to enable ssl from xampp index file it has accepter the certificate but still my error won't disapear.I'm really concerned about this.Can anyone share his opinion or experience? thanks for reading!

The problem is that you don't have the ssl transport installed for php. Different systems do this in different ways, so I don't know how yours would work.
You can run a quick php app to verify this:
<?php
print_r(stream_get_transports());
Mine returns:
Array
(
[0] => tcp
[1] => udp
[2] => unix
[3] => udg
[4] => ssl
[5] => sslv3
[6] => sslv2
[7] => tls
)
If ssl and tls are not there, then your php installation does not have support for them and you need to find a way to fix that.

i think for some reason it started working.i'm not sure about what happened,i reinstall xampp and didn't see the problem anymore.weird.

Related

Why is my ftp connection not working in php laravel but is working in FileZilla?

I have a PHP Laravel (5.6) system that I need to connect to an FTP server to upload a single file. The FTP server that I am connecting to is restricting access by ip address, uses port 990, and other than that has a seemingly simple configuration. On my local machine (I'm running on Linux Ubuntu if that helps) I am able to connect to the ftp server in FileZilla just fine, FileZilla did seem to automatically choose ftps. I am also able to ping this server.
Now this PHP Laravel (5.6) application is running on NGINX (had this for a while, everything else server-wise seems fine). As of now I am just trying to get this working locally, though there is a production server that it will have to be pushed onto (pretty much identical configuration though).
I started out trying to use the built in PHP function ftp_connect and ftp_ssl_connect - both using the same host and port number (990) as in FileZilla. I have been unable to get past this step - it returns false (so never even gets to my login logic).
$ftp = ftp_connect(env('FTP_HOST'),env('FTP_PORT')); // returns FALSE
$ftp = ftp_ssl_connect(env('FTP_HOST'),env('FTP_PORT')): // returns FALSE
After searching for a while I decided to try Laravel's filesystem to see if that would make it easier, these are my settings in config/filesystems.php:
'ftp' => [
'driver' => 'ftp',
'host' => env('FTP_HOST'),
'username' => env('FTP_USER'),
'password' => env('FTP_PASSWORD'),
'port' => env('FTP_PORT'),
'ssl' => true,
'timeout' => 60,
],
'sftp' => [
'driver' => 'sftp',
'host' => env('FTP_HOST'),
'username' => env('FTP_USER'),
'password' => env('FTP_PASSWORD'),
'port' => env('FTP_PORT'),
'timeout' => 60,
],
I figured I'd try both ftp and sftp here, I then tried the following:
Storage::disk('ftp')->put('test.csv', $file);
and
Storage::disk('sftp')->put('test.csv', $file);
The first just timed out, the second gave me the message: League\Flysystem\Sftp\ConnectionErrorException: Could not login with username: ###, host: ### in...
Any ideas of what this could be or next steps I could take towards troubleshooting would be greatly appreciated, I feel like I just don't know what to try to get a better understanding of what's wrong here. Thanks!
EDIT:
I realized that previously I had always used the quick connect feature in FileZilla for this. I looked into it further and was able to confirm that the encryption has to be implicit FTP over TLS - So I'm wondering if there is a setting for that I'm missing.
The port 990 is for implicit TLS/SSL, as you have eventually figured out.
The implicit TLS/SSL is not supported by the PHP built-in FTP implementation. Neither is implicit TLS/SSL supported by flysystem used by Laravel (which probably internally uses PHP built-in FTP anyway).
The implicit TLS/SSL was a temporary hack back in 90s to allow legacy FTP software to use encrypted connection without modification. It was never something that should have been used in long term, definitely not 30 years later! The implicit FTP never even became part of FTP standard. Only the explicit TLS/SSL was standardized by RFC 4217 in 2005. Since then, noone should be using implicit TLS/SSL ever.
Get your FTP server fixed to use the explicit TLS/SSL.
Take a look at the additional settings (https://flysystem.thephpleague.com/v2/docs/adapter/ftp/) you can add to the filesystem.php for your ftp settings. Match them with what you have on your FileZilla settings and see if it helps.
Laravel underneath uses the flysystem adapters to connect to different storage types and you can reference the settings from the above URL.

Sending an email with SMTP in php using PHPMailer [duplicate]

I am using PHPMailer on PHP 5.6, the increased security around certificated in PHP 5.6 is certainly fun.
I am trying to send a test message to a domain hosted on dreamhost, the error that comes back from PHPMailer is: Could not connect to SMTP host.
That error is not right though, I have logging enabled and here is what is actually going on.
Connection: opening to mx1.sub4.homie.mail.dreamhost.com:25,
timeout=30, options=array ( ) Connection: opened S: 220
homiemail-mx32.g.dreamhost.com ESMTP
C: EHLO s81a.ikbb.com
S: 250-homiemail-mx32.g.dreamhost.com 250-PIPELINING 250-SIZE 40960000
250-ETRN 250-STARTTLS 250-ENHANCEDSTATUSCODES 250 8BITMIME
C: STARTTLS
S: 220 2.0.0 Ready to start TLS
C: QUIT
S: SMTP ERROR: QUIT command failed: Connection: closed
I could not understand why PHPMailer just gives up, issuing a QUIT command when it should start sending the message. I got another clue from another log:
PHP Warning: stream_socket_enable_crypto(): Peer certificate CN=*.mail.dreamhost.com' did not match expected CN=mx1.sub4.homie.mail.dreamhost.com' in /home/ikbb/domains/dev.ikbb.com/public_html/includes/phpmailer/5.2.10/class.smtp.php
If I use some custom options to prevent validation of the cert they are using I can get it to continue. Here is what I have:
$mail->SMTPOptions = array (
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true));
If I put the SMTPOptions in there and skip the peer verification, message goes OK - with no warning in PHP at all.
How can I trap that error, so I know there is an issue but still send the message?
I had the same problem and I found the answer in the PHPMailer documentation.
PHP 5.6 certificate verification failure
In a change from earlier versions, PHP 5.6 verifies certificates on SSL connections. If the SSL config of the server you are connecting to is not correct, you will get an error like this:
Warning: stream_socket_enable_crypto(): SSL operation failed with code 1.
OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
The correct fix for this is to replace the invalid, misconfigured or self-signed certificate with a good one. Failing that, you can allow insecure connections via the SMTPOptions property introduced in PHPMailer 5.2.10 (it's possible to do this by subclassing the SMTP class in earlier versions), though this is not recommended:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
You can also change these settings globally in your php.ini, but that's a really bad idea; PHP 5.6 made this change for very good reasons.
Sometimes this behaviour is not quite so apparent; sometimes encryption failures may appear as the client issuing a QUIT immediately after trying to do a STARTTLS. If you see that happen, you should check the state of your certificates or verification settings.
Solution for WHM/cPanel(s) : Disable SMTP Restriction by following below process:
a) Open WHM and search for SMTP restriction, make sure it's disable.(You can go through Home »Security Center »SMTP Restrictions directly as well)
b) Or Same thing can be done via Tweak Settings (Directly go for Home »Server Configuration »Tweak Settings or you can click on tweak setting link shown in upper image)
For those of you using cPanel, I tried the SMTP check code from the examples folder in PHPMailer and I got this same error:
PHP Warning: stream_socket_enable_crypto(): Peer certificate CN=*.mail.dreamhost.com' did not match expected CN=mx1.sub4.homie.mail.dreamhost.com' in /home/ikbb/domains/dev.ikbb.com/public_html/includes/phpmailer/5.2.10/class.smtp.php
I realized that it was not an error related to PHPMailer, so I searched for similar errors related to CentOS and I found this link that shed some light: Issue sending mails through 3rd party. You have to take a look at "SMTP Restrictions" in cPanel.
For PHP 5.6 use the following. Adding "tls://" is the key.
$mail->Host = gethostbyname('tls://smtp.gmail.com');
See: http://php.net/manual/en/context.ssl.php
Disable SMTP Restriction in WHM
As somebody mentioned here, the issue is an invalid SSL certificate.
Your website might have a valid SSL certificate, but it might not apply to the mail.website.net or smtp.website.net subdomains. If your hosting provider has an interface for generating SSL certificates for your website, try to search if there isn't a possibility to select subdomains for which the certificate will generate.
I had a similar problem after I've upgraded to PHP 5.6 on my WordPress machine. The WP Mail SMTP by WPForms (wp-mail-smtp) plugin were configured to use localhost as SMTP Host. I've changed it to the FQHN (Fully Qualified Host Name) as it is defined in the SSL cert.
After this change it is working fine.
If you just migrated to a different server, most likely you can fix this by disabling SMTP restriction from WHM :

Zend Framework cannot open ssl socket (Abstract.php)

I'm configuring a Magento 1.9.1 to use an external SMTP to send emails, using SMTP PRO (https://www.magentocommerce.com/magento-connect/smtp-pro-email-free-custom-smtp-email.html).
I've setup the configuration (hostname, port, username, password) but when I try to send a test email it fails with a 'Could not open socket' error message. If I dig into the code I see that the error is generated from this piece of code in lib/Zend/Mail/Protocol/Abstract.php:
$this->_socket = #stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
if ($this->_socket === false) {
if ($errorNum == 0) {
$errorStr = $remote.' Could not open socket '.phpversion();
}
/**
* #see Zend_Mail_Protocol_Exception
*/
#require_once 'Zend/Mail/Protocol/Exception.php';
throw new Zend_Mail_Protocol_Exception($errorStr);
}
My PHP environment (5.6.30) has openssl support (see image and snippet below)
[root#ns3023903 httpdocs]# php -r 'print_r(stream_get_transports());'
Array
(
[0] => tcp
[1] => udp
[2] => unix
[3] => udg
[4] => ssl
[5] => sslv3
[6] => sslv2
[7] => tls
)
There are no firewall issues as i can both telnet on the port 465 to the target host or send an email using the same SMTP using phpmailer with the same user I use to run the site virtualhost.
I'm on a CentOS 7 64 bit server, with SELinux disabled and, if that matters, I'm using Plesk 12 to configure virtual hosts.
Name resolution is also working ok (I can ping the SMTP name correctly and the IP address is looked up just fine).
I'm obviously missing something here...but what?
It turned out it was something that changed in the PHP version 5.6.30, in terms of default options assumed by stream_socket_client().
First of all before getting rid of the stupid # in front of the stream_socket_client call in the Magento core code I wasn't actually able to understand what was going on. I'm not even close to a PHP developer so I keep forgotting what that stupid # prefix is for (to be honest I don't even know why it exists).
After removing the # I saw this:
Warning: stream_socket_client(): Peer certificate CN=`*.aruba.it' did not match expected CN=`mail.myclientreserveddomain.com'
I looked a bit into the PHP documentation and I came out with a two line diff to the core code (I know this is not right, I'll refactor it now, but I wanted to share the quick workaround first).
Basically I'm creating a context and using another version of the stream_socket_client call
$context = stream_context_create(['ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]]);
// open connection
$this->_socket = stream_socket_client($remote, $errorNum, $errorStr, 120, STREAM_CLIENT_CONNECT, $context);
Skipping peer name validation is the key. Not sure why/how/when it changed from php pre 5.6.30 to PHP 5.6.30 (have I said I'm not a PHP dev?) but it works. I also tried to run the original code under PHP 5.4.x and it worked without the workaround so it's definitely that.

PHPMailer generates PHP Warning: stream_socket_enable_crypto(): Peer certificate did not match expected

I am using PHPMailer on PHP 5.6, the increased security around certificated in PHP 5.6 is certainly fun.
I am trying to send a test message to a domain hosted on dreamhost, the error that comes back from PHPMailer is: Could not connect to SMTP host.
That error is not right though, I have logging enabled and here is what is actually going on.
Connection: opening to mx1.sub4.homie.mail.dreamhost.com:25,
timeout=30, options=array ( ) Connection: opened S: 220
homiemail-mx32.g.dreamhost.com ESMTP
C: EHLO s81a.ikbb.com
S: 250-homiemail-mx32.g.dreamhost.com 250-PIPELINING 250-SIZE 40960000
250-ETRN 250-STARTTLS 250-ENHANCEDSTATUSCODES 250 8BITMIME
C: STARTTLS
S: 220 2.0.0 Ready to start TLS
C: QUIT
S: SMTP ERROR: QUIT command failed: Connection: closed
I could not understand why PHPMailer just gives up, issuing a QUIT command when it should start sending the message. I got another clue from another log:
PHP Warning: stream_socket_enable_crypto(): Peer certificate CN=*.mail.dreamhost.com' did not match expected CN=mx1.sub4.homie.mail.dreamhost.com' in /home/ikbb/domains/dev.ikbb.com/public_html/includes/phpmailer/5.2.10/class.smtp.php
If I use some custom options to prevent validation of the cert they are using I can get it to continue. Here is what I have:
$mail->SMTPOptions = array (
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true));
If I put the SMTPOptions in there and skip the peer verification, message goes OK - with no warning in PHP at all.
How can I trap that error, so I know there is an issue but still send the message?
I had the same problem and I found the answer in the PHPMailer documentation.
PHP 5.6 certificate verification failure
In a change from earlier versions, PHP 5.6 verifies certificates on SSL connections. If the SSL config of the server you are connecting to is not correct, you will get an error like this:
Warning: stream_socket_enable_crypto(): SSL operation failed with code 1.
OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
The correct fix for this is to replace the invalid, misconfigured or self-signed certificate with a good one. Failing that, you can allow insecure connections via the SMTPOptions property introduced in PHPMailer 5.2.10 (it's possible to do this by subclassing the SMTP class in earlier versions), though this is not recommended:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
You can also change these settings globally in your php.ini, but that's a really bad idea; PHP 5.6 made this change for very good reasons.
Sometimes this behaviour is not quite so apparent; sometimes encryption failures may appear as the client issuing a QUIT immediately after trying to do a STARTTLS. If you see that happen, you should check the state of your certificates or verification settings.
Solution for WHM/cPanel(s) : Disable SMTP Restriction by following below process:
a) Open WHM and search for SMTP restriction, make sure it's disable.(You can go through Home »Security Center »SMTP Restrictions directly as well)
b) Or Same thing can be done via Tweak Settings (Directly go for Home »Server Configuration »Tweak Settings or you can click on tweak setting link shown in upper image)
For those of you using cPanel, I tried the SMTP check code from the examples folder in PHPMailer and I got this same error:
PHP Warning: stream_socket_enable_crypto(): Peer certificate CN=*.mail.dreamhost.com' did not match expected CN=mx1.sub4.homie.mail.dreamhost.com' in /home/ikbb/domains/dev.ikbb.com/public_html/includes/phpmailer/5.2.10/class.smtp.php
I realized that it was not an error related to PHPMailer, so I searched for similar errors related to CentOS and I found this link that shed some light: Issue sending mails through 3rd party. You have to take a look at "SMTP Restrictions" in cPanel.
For PHP 5.6 use the following. Adding "tls://" is the key.
$mail->Host = gethostbyname('tls://smtp.gmail.com');
See: http://php.net/manual/en/context.ssl.php
Disable SMTP Restriction in WHM
As somebody mentioned here, the issue is an invalid SSL certificate.
Your website might have a valid SSL certificate, but it might not apply to the mail.website.net or smtp.website.net subdomains. If your hosting provider has an interface for generating SSL certificates for your website, try to search if there isn't a possibility to select subdomains for which the certificate will generate.
I had a similar problem after I've upgraded to PHP 5.6 on my WordPress machine. The WP Mail SMTP by WPForms (wp-mail-smtp) plugin were configured to use localhost as SMTP Host. I've changed it to the FQHN (Fully Qualified Host Name) as it is defined in the SSL cert.
After this change it is working fine.
If you just migrated to a different server, most likely you can fix this by disabling SMTP restriction from WHM :

Can't connect to IMAP with PHP

i'm trying to connect php with an imap server but i'm getting this error:
Warning: imap_open() [function.imap-open]: Couldn't open stream
{mail.XXX.it:143/imap/notls/norsh}INBOX Array ( [0] => SECURITY
PROBLEM: insecure server advertised AUTH=PLAIN [1] => Can not
authenticate to IMAP server: AUTHENTICATE mechanism not supported,
mate ) Cannot connect: 1
When trying to connect mail.XXX.it:143/imap/notls/norsh
And this error:
Warning: imap_open() [function.imap-open]: Couldn't open stream
{mail.XXX.it:143/imap/secure/notls/norsh}INBOX Array ( [0] => Can't do
secure authentication with this server ) Cannot connect: 1
When trying to connect mail.XXX.it:143/imap/secure/notls/norsh
How can i get around this?
(Hosting has no SSL security)
Thanks
Have you tried checking your ssl certs? If the server has a self-signed cert it might cause
php's imap library to throw a configurable error (possibly what you are getting). Furthermore the imap library is installed as a package, sometime separately, you might want to check your configurations for this lib.
Tell me what you think and a curl -v basic auth attempt readout might be helpful,
Cheers

Categories