SwiftMailer is the default way of sending email from a Symfony 2 project, and I'd like to use it in an application. I've pointed it towards the SMTP server provided by my internet provider (Virgin Media, in the UK), but my emails are not successfully sent. The exception thrown says
Expected response code 250 but got code "501", with message "501 HELO requires valid address
in response to the SMTP command
HELO [::1]
[::1] corresponds to localhost in IPv6, and it's not that surprising that the SMTP server would reject that if it's expecting a proper domain name. Tracing execution through the Swiftmailer source, I find that this value comes from _domain on AbstractSmtpTransport; altering the value during the debug session allows the email to be sent. Although it can be set using the public setLocalDomain() method, it isn't anywhere in the Symfony 2 framework, so it seems that there is no way (obvious to me, at least) of easily configuring this value simply by, for example, changing a Symfony config file.
So my question is this: is there some other way of changing this value, ideally using config, without my diving into the Symfony code and changing bits that aren't meant to be changed? And if so, what?
From inside a Symfony controller, you can configure the HELO/EHLO name like that:
$this->get('swiftmailer.transport')->setLocalDomain('your.domain');
Unfortunately no.
The code for get the local domain in SwiftMailer is:
/** Try to determine the hostname of the server this is run on */
private function _lookupHostname()
{
if (!empty($_SERVER['SERVER_NAME'])
&& $this->_isFqdn($_SERVER['SERVER_NAME']))
{
$this->_domain = $_SERVER['SERVER_NAME'];
} elseif (!empty($_SERVER['SERVER_ADDR'])) {
$this->_domain = sprintf('[%s]', $_SERVER['SERVER_ADDR']);
}
}
So, is guessed and it's server configuration.
I opened an issue for SwiftMailer to do this:
https://github.com/swiftmailer/swiftmailer/issues/453
UPDATE: The issue has been resolved on Feb 27th 2018.
Related
I noticed something strange on my sent mails today. Someone must have injected a crack.How do i solve this matter? Below is the content of the mail:
From: mail#wokforge.com
Subject: LARAVEL SMTP CRACK | HOST: mail.wokforge.com
To: rahulethan1993#gmail.com
Success Send,
BY XCATZE
URL :http://129.159.148.xx/.env
HOST :mail.wokforge.com
PORT :587
USER :mail#wokforge.com
PASSW :xxxxxxxxxxxxxxx
SENDER :unknown#unknown.com
Turn off debug mode
APP_ENV=production
APP_DEBUG=false
source: https://laravel-news.com/laravel-smtp-crack
Xcatze was made by me To crack smtps
From websites with laravel method
But you can Secure yourself by
Delete env file or rename it and
Turn off Debug mode..
Answered - but adding a comment.
Leaving Laravel in debug mode exposes the error debug screen - so anyone out in the internet can trigger and exception on your website and view all the configuration variables.
An easy way to trigger an exception is to send a POST request to a GET route.
I'm trying to setup Laravel auth including the 'Password Reset' function. But I am stumbeling upon a error when I try to send the email. The error I get is:
Expected response code 250 but got code "550", with message "550 Bad HELO - Host impersonating domain name
Im using Laravel 5.6 with Cloudflare. My hosting said that that might be the issue but I've already tried to disable all Cloudflare DNS functions inside the Cloudflare Dashboard, but that didnt work. I am sure I am using the correct mails settings since they work fine on my phone and any other devices.
Also, I've tried switching ports and it doesnt matter if I fill in a valid port or not, it will keep giving me this error.
Laravel 5.6 uses $_SERVER["SERVER_NAME"] as your local domain.
You should change localdomain to your MAIL_HOST
This worked for me:
config/mail.php
'localdomain' => env('MAIL_HOST')
And you should make a little change in vendor:
vendor/swiftmailer/swiftmailer/lib/dependency_maps/transport_deps.php
//->asValue(!empty($_SERVER['SERVER_NAME']) && '' === preg_replace('/(?:^\[)?[a-zA-Z0-9-:\]_]+\.?/', '', $_SERVER['SERVER_NAME']) ? trim($_SERVER['SERVER_NAME'], '[]') : '127.0.0.1')
->asValue(config('mail.localdomain'))
Replace commented code,
I Know this is the worst solution but it seems that there is no other solution.
#bardia-mazaheri is right but better solution is to do it like this:
class AppServiceProvider extends ServiceProvider
{
public function register()
{
// There is a bug in which the Swift library loads 'transport.localdomain' value from $_SERVER['SERVER_NAME']
// which results to wrong host name for email server and causes an error:
// Expected response code 250 but got code "550", with message "550 Bad HELO - Host impersonating domain name
// described here https://stackoverflow.com/questions/55279922/550-bad-helo-host-impersonating-domain-name-laravel
\Swift_DependencyContainer::getInstance()
->register('transport.localdomain')
->asValue('mail.domain.com');
}
}
I ran against the same problem and i tried the above answer but it did not help me, but the following worked for me for strange reasons...
In your /config/mail.php add the following line:
'localdomain' => 'localhost'
don't ask me why xD
Ever since OS X 10.8 I've used the steps here to test sending mail with php.
Basically it redirects php's sendmail_path to a .php script that saves the email message as a .emlx file and then opens Apple Mail (and that file) automatically.
This worked well until I upgraded to 10.10
After the upgrade to 10.10 all seems to work, the .emlx file is created as expected right where it is supposed to be but Apple Mail does not open up.
I'm getting the following error in my apache error_log
LSOpenURLsWithRole() failed with error -10810 for the file /Users/jason/smtp_out/2014-10-22_10.12.20_587.emlx.
I've got no clue about what LSOpenURLsWithRole() is all about. I'm guessing this is something around php not being able to open the .emlx file up in Apple Mail automatically (I can dbl click it and it opens in Mail as expected)
I've changed all the permissions as explained in the original site. Is there something I'm missing that would get the .emlx file to open automatically? (Does this need to be changed because of 10.10?)
UPDATE
As a temp fix, I've reverted to using a folder action but I'd still like to figure out what LSOpenURLsWithRole() failed with error -10810 is all about
I suggest to use Mailtrap instead. All you need to do is configure SMTP to use their server:
Host: mailtrap.io
Port: 25 or 465 or 2525
Username: your-username
Password: your-password
Auth: PLAIN, LOGIN and CRAM-MD5
TLS: Optional
All emails will be sent to Mailtrap regardless of the email address. You will then be able to access sent messages on mailtrap.io, you can view the message in HTML and text format, run an analysis on the message, etc.
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".
When I send an email with the PHP Swift mailer to this server: smtp.exchange.example.com like this:
// Load transport
$this->transport =
Swift_SmtpTransport::newInstance(
self::$config->hostname,
self::$config->port
)
->setUsername(self::$config->username)
->setPassword(self::$config->password)
;
// Load mailer
$this->mailer = Swift_Mailer::newInstance($this->transport);
// Initialize message
$this->message = Swift_Message::newInstance();
// From
$this->message->setFrom(self::$config->from);
// Set message etc. ...
// Send
$this->mailer->send($this->message);
I get a strange error back:
Failed to authenticate on SMTP server with username "user#example.com" using 2 possible authenticators
I know for sure that the login-info is correct.
This might be old but somebody might get help through this. I too faced the same problem and received a mail on my gmail account stating that someone is trying to hack your account through an email client or a different site.
THen I searched and found that doing below would resolve this issue.
Go to https://accounts.google.com/UnlockCaptcha‎
and unlock your account for access through other media/sites.
UPDATE : 2015
Also, you can try this, Go to https://myaccount.google.com/security#connectedapps
At the bottom, towards right there is an option "Allow less secure apps". If it is "OFF", turn it on by sliding the button.
UPDATE : 2021
The google URL has now changed to : https://myaccount.google.com/lesssecureapps. You will be asked to relogin
I had the same issue, so I've disabled one setting on my WHM root login, which is as follows :
WHM > Home > Server Configuration > Tweak Settings > Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak) [?]
I really have the same problem, finally, i solved it.
its likey not the Swift Mail's problem. It's Yaml parser's problem.
if your password only the digits, the password senmd to swift finally not the same one.
swiftmailer:
transport: smtp
encryption: ssl
auth_mode: login
host: smtp.gmail.com
username: your_username
password: 61548921
you need fix it with double quotes
password: "61548921"
I just received the same error.
The strange thing is that commented the credential lines
// ->setUsername(self::$config->username)
// ->setPassword(self::$config->password)
And the problem disappeared.
I'm puzzled because the mail server definitely needs those credentials.
I tried almost all the possible suggestions mention here but for me problem got solved after changing "Access for less secure apps" to ENABLE in my Google account security settings tab. Hope this might useful for others !
The server might require some kind of encryption and secure authentication.
see http://swiftmailer.org/docs/sending.html#encrypted-smtp
try putting username in double quote "username", somehow that fixed for me.
I faced the same problem and solved it. Try to log in manually to your gmail account first.
This issue is due to google has detected your gmail account has been abusing. E.g send a lot of emails over a short period of time.
You perhaps use the wrong username.
I had a similar error. Ensure you're not using uppercase while logging into server.
Example: JacekPL#lala.pl
If you use ->setUsername('JacekPL'), this can cause an error. Use ->setUsername('jacekpl') instead. This solved my problem.
First check if there is any connectivity problem and you can reach the SMTP server:
In terminal type:
telnet servername portnumber
If you receive the 220 response you can reach the SMTP server and there is no connectivity problem but if the connection to the server failed see what's wrong in your network.
If the server needs auth try to authenticate on the server by username and password and see if something goes wrong.
At last see if the server requires encryption and if yes openssl and other stuff are configured correctly.
I got the same same error.
I was using gmail account and Google's SMTP server to send emails. The problem was due to SMTP server refusing to authorize as it considered my web host (through whom I sent email) as an intruder.
I followed Google's process to identify my web host as an valid entity to send email through my account and problem was solved.
If you are trying to send mail from your local enviroment eg. XAMPP or WAMP, this error will occur everytime, go ahead and try the same code on your web hosting or whatever you are using for production.
Also, 2 step authentication from google may be the issue.
Strange enough sending emails works again. We did not change anything and the host say they did not either. We think a server restarts or so. It is strange :S