php swiftmailer sending mail with smtp transport timeout - php

I am trying to send mail via mandrill app using swift mailer. This is my code:
$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com',587);
$transport->setUsername($username);
$transport->setPassword($password);
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
$message = Swift_Message::newInstance()
// Give the message a subject
->setSubject('New Order '.$reservationNumber)
// Set the From address with an associative array
->setFrom(array('noreply#domain.com' => 'domain.com'))
// Set the To addresses with an associative array
->setTo('test#domain.com')
// Give it a body
->setBody($body,'text/html');
$mailer->send($message);
Credentials are 100% good. And i get timeout error: Connection could not be established with host smtp.mandrillapp.com [Connection timed out #110].
It looks like something is blocking connection. Maybe this is issue with server configurations? We are using WHM software on our centos server

I've just been doing battle with exactly the same problem, but with smtp.gmail.com. It just would not work, even though the username/password etc were all correct.
In my case it seems that when PHP tries to connect to smtp.gmail.com, it gets the IPv6 address back - but their server seems to not be listening on that, since the Swiftmailer responds with the same timeout error you get.
But when I swapped in its IPv4 address (got by ping-ing it), it connected and sent the email just fine.
So find out what smtp.mandrillapp.com's IPv4 address is and try that IP in place of the hostname in the code. Does that connect and send now? It did for me.
It's not ideal coding in an IP address - given that they could change it at any minute - but at least you will get some emails sent

Here it actually worked when I increased the timeout:
$transport = Swift_SmtpTransport::newInstance('mail_server', 'mail_port', 'tls')
->setUsername('mail_user')
->setPassword('mail_pass')
->setTimeout(120)
;
The server I tried to access works with IPv6, maybe there is an issue with related to that.

Related

Using custom SMTP server with Laravel / swiftmailer fails, same settings work in Thunderbird

I'm trying to send an email using SwiftMailer (which is what Laravel uses by default):
$transport = (new Swift_SmtpTransport('smtp.my-server.com', 465, "tls"))
->setAuthMode('PLAIN')
->setUsername('my-username')
->setPassword('my-password');
$mailer = new Swift_Mailer($transport);
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['address#my-domain.com' => 'My Name'])
->setTo(['my-gmail-address#gmail.com'])
->setBody('Here is the message itself');
$mailer->send($message);
echo "\nMail sent! \n\n";
And running this gives me:
Expected response code 220 but got an empty response
Using the same settings in Thunderbird (an email client) works:
I'm able to send emails with these settings in Thunderbird. (from the same system I'm attempting to use swiftmailer)
So how can I debug this? Is there some default setting that I need to change in swiftmailer to make this work? Is there some way I can get more debug information?
My SMTP server doesn't even log an authentication attempt. It's like swiftmailer isn't even trying to connect to my smtp server!
UPDATE: I tried using Swift_Plugins_LoggerPlugin to get more debugging info, but it was no help:
++ Starting Swift_SmtpTransport
!! Expected response code 220 but got an empty response (code: 0)
TLDR; use ssl instead of tls when using a port that forces ssl/tls. Swiftmailer's "tls" actually isn't tls, it's starttls.
It seems that swiftmailer's "tls" encryption option is mislabeled! My server only accepts ssl/tls on port 465, and I was able to connect to it over Thunderbird, but swiftmailer was unable to.
When I switched to port 587, which only supports starttls on my server, it worked, even though the encryption is set to tls (which should NOT be the same as starttls).
Looking at the source files confirmed my suspicion:
if ($this->params['tls']) {
try {
$this->executeCommand("STARTTLS\r\n", [220]);
...
when it's set to tls, it actually means starttls. :P
So if you want to use ssl/tls, do not use the swiftmailer encryption option "tls" 😅
When I changed the encryption to ssl, it worked on the port that enforced ssl/tls. So the answer is to use ssl.
EDIT: I went to open an issue on the swiftmail github, but the issue was already there in "open" status. So I wasn't the only one to be bitten by this. 😛

'No Relay Access Allowed' in Swift Mailer

I recently transferred my server to VPS & now email function doesn't work for external emails.
Following are the settings I'm using:
$transport = Swift_SmtpTransport::newInstance('ns1.example.com', 465, 'ssl')
->setUsername('testing#example.com')
->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);
And the error that I'm getting is this:
SMTP error from remote mail server after initial connection:
host dedrelay.where.example.net [XX.XXX.XXX.XX]: 554
m1plded02-01.prod.mesa1.example.net : DED :
gWqF1p02c0cB4sG01 : DED : ESMTP
No Relay Access Allowed From XXX.XXX.XXX
I've tried telnet & response is ok. I've tried following:
telnet ns1.example.com 465
Response was:
connected to xx.xxx.xxx.xx
I'm also not able to configure my desktop email client. Can anyone tell a solution? Any help will be appreciated.
EDIT
I'm not even able to send an email through server's webmail. Same error.
You tried telnet ns1.example.com 465 and it connected. This shows the SMTP server is up and running and your computer can reach it. But this is not all you need.
The error message No Relay Access Allowed From 123.123.123.123 means the SMTP server is configured to not accept emails from this IP address for relay, i.e. emails that needs to be passed to another server for delivery.
This is an anti-abuse measure and it means the SMTP server is configured correctly.
There is nothing wrong with your SwiftMailer configuration. Any email client (including the desktop client, as you said) you use, the answer is the same.
You need to contact the system administrator of the SMTP server and ask them to allow your IP address to use their SMTP server as relay. If they are your ISP it's also possible that the server allows relay only after authentication: you have an username/password pair (that you use to read the emails, f.e.) and you need to use it in order to send emails through their SMTP server. But this is only a supposition (this is how it usually works); you have to ask them to know for sure.

Swiftmailer working on localhost but not in production

On my localhost (Linux Mint OS) Swiftmailer is working fine with the following code, but whenever I move it up to my server is just hangs at the send function and then gives me an internal server error.
I currently have my email setup through Google Apps, so maybe SMTP will not work for this situation? If thats the case, how do you suggest I change this from SMTP.
Also, whenever I send an email like this, it is showing up with a from address of the one in the username area. I want it to show up with the from address in the "setFrom" function.
//Include the swiftmailer class
require_once 'comm/swiftmailer/swift_required.php';
//Create a message
//Pass it as a parameter when you create the message
$message = Swift_Message::newInstance();
$message->setSubject('My Subject');
$message->setFrom(array('noreply#domain.com' => 'No Reply'));
$message->setTo(array('me#domain.com' => 'Me'));
$message->setBody($emailContent, 'text/html');
//Create transport class and email the message
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')->setUsername('useracctname')->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
Thanks a lot for any help!
This might be a port problem on the server. Port 465 could be closed to prevent spamming. It could also be that the server's version of PHP lacks SSL support.
I am wondering though, could it be that I am not using the local mail server so it wont allow SMTP?
If you address gmail as explicitly as you do, it's very unlikely you're using another transport type or a different server.
Also, what about using one of the other transport types?
I think SMTP is your only option to get it running with Gmail. It could, however, be that your server is providing a mail() based service (obviously with a different sender address than GMail though). In that case, you may be able to use Swiftmailer's mail transport.
I had the same problem : I was able to send gmail email in local (after updating that configuration : https://www.google.com/settings/security/lesssecureapps ) but not in production.
To fix my problem I've logged to my gmail account with my production ip / from my production server and answer the gmail security question.
One of the solution to log to your gmail account with your production ip, is to
open an ssh tunnel (ssh -2NfCT -D 5000 yoursshuser#yourdomain.org)
configure you browser to use that proxy (eg Firefox :to Preferences > Advanced > Network > Settings > Manual proxy configuration > SOCKS Hosts: localhost / Post: 5000 / SOCKS v5 )
log to you gmail account as usual
The error outputed by Symfony was :
app.ERROR: Exception occurred while flushing email queue: Failed to authenticate on SMTP server with username "joe.doe" using 1 possible authenticators [] []
well, here is the solution.
if you are sending SMTP mail you should create email account at the server then use this email info at your code.
the server will not send the email if you don't have valid email account to send using it
when you use "from".
this should be a true correct email account a fake one.
the only possible reason to send using fake account is using mail() function.

Swiftmailer SMTP transport rejecting local IP address

I'm trying to send an email using the Swift_SmtpTransport but I'm getting the following error:
501 5.5.2 <[::1]>: Helo command rejected: invalid ip address
The SMTP server is a remote server and it works from my production server, but not from my development machine, which is running OS X.
It also doesn't bother to throw an exception, instead it required me to use a logger plugin to find out why it wasn't working.
What can I do to make it use a real IP address?
I did some poking around in the code and found it.
When configuring the SMTP transport, you need to call setLocalDomain(). Using PHP on OS X, this defaults to "::1", which is rejected by the remote server. I've just added a line in my development configuration to set that:
$transport = Swift_SmtpTransport::newInstance('mail.pantsburger.com', 587);
if (SITE_ENV == SITE_ENV_DEV) {
$transport->setLocalDomain('[127.0.0.1]');
}
I think this is also a bug with Swiftmailer - it really should be throwing an exception for something like this, rather than just listing each recipient as "failed".

Swift Mailer Error

Im using the following code to send a message:
try
{
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
$smtp =& new Swift_Connection_SMTP("mail.somedomain.net", 587);
$smtp->setUsername("username");
$smtp->setpassword("password");
$swift =& new Swift($smtp);
//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
$message =& new Swift_Message("message title");
$message->attach(new Swift_Message_Part("Hello"));
//Try sending the email
$sent = $swift->send($message, "$myEmail", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
if($sent)
{
print 'sent';
}
else
{
print 'not sent';
}
}
catch (Exception $e)
{
echo"$e";
}
The issue is that it is working fine on my local server (which my xampp server) but not working when the file is uploaded on the real server.
It throwing this error:
'The SMTP connection failed to start [mail.somedomain.net:587]: fsockopen returned Error Number 110 and Error String 'Connection timed out''
Please what should I do to correct this error. Thanks for reading
Make sure that the smtp server domain is valid. Trying pinging it to confirm a response. You may also try trace route to see if any of the switches are returning slow responses.
$smtp =& new Swift_Connection_SMTP("mail.somedomain.net", 587);
is that '587' the port number to connect to? Any reason you're trying that instead of the normal port 25? Port 587 (submission) is normally used for local users to send mail. Once you're running this script on your remote web server, it's no longer "local", and is most likely firewalled off (or the mail server's not listening to that port on external interfaces).
Try switching to port 25 and see if that helps any.
update:
Connection refused is better than "connection timed out". It means at least that the initial data packet got somewhere and was actively refused. Timed out means things just got dropped silently somewhere en-route.
max_execution_time would only come into play if the php script itself went over the max time. If that was the case, you wouldn't be getting a swiftmailer error, because the script would have simply terminated.
Is your webserver running sendmail? Change the connection host to 'localhost' and see if that helps. If you just want to send an email, then that should work. The only reason you might want to connect to a remote SMTP server is for the From: headers to be correctly set and not be possibly flagged as SPAM on the receving end.

Categories