Been struggling with swiftmail for a while now.
I have a linux server and properly working sendmail. Sending mail from command line works.
And sending with swiftmail via command line works as well so the configuration must be OK.
But from code it doesn't. Must have read all the issues of this and I'm totally confused what's going on.
The token is created OK and everything seems to work. No errors in logs. But the mail just doesn't come. I have tried to send to different domains as well. And as I said, it works from command line with mail and swiftmail commands like this:
bin/console swiftmailer:email:send --subject="Test" --body="test" --from="info#domain.com" --to="test#anotherdomain.com"
Here is the current code:
$mailer = $this->get('mailer');
$message = $mailer->createMessage()
->setSubject('You forgot password')
->setFrom('info#domain.com')
->setTo($username)
->setBody('You forgot your password. Go and change it from http://url.com/change-password/'.$user->getConfirmationToken());
$mailer->send($message);
Here is config.yml:
swiftmailer:
transport: sendmail
host: /usr/sbin/sendmail -bs
I had the same issue and fixed it by disabling spool on swift mailer.
You didn't specify the Symfony version you are using. If you are using Symfony 4, there should be a config file in /config/packages/swiftmailer.yaml
Here is mine:
swiftmailer:
url: '%env(MAILER_URL)%'
#spool:
#type: 'memory'
#type: file
#path: '%kernel.project_dir%/var/spool'
Related
I am working on a Symfony 4 app using Swift_Mailer to send e-mails with.
Since there is no possibility in my case to use SMTP (don't ask why…) I have to use something like sendmail.
By default the config of Swift Mailer is done in Symfony's .env file in URL notation in an option named MAILER_URL. The default value is "null://localhost" which doesn't send mails at all.
All I could find for what the value has to be is an example for Gmail or for SMTP in general as documented in the Symfony docs as well as in the sample .env as generated by Composer.
Default content of .env:
# …
###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password#localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=null://localhost
###< symfony/swiftmailer-bundle ###
# …
What I don't know and therefore what my question is:
What do I need to do to make sendmail work with this?
I already tried something like:
MAILER_URL=sendmail://localhost
and
MAILER_URL=sendmail://my-domain.com
but without success so far.
When using the console command
bin/console swiftmailer:email:send
I even get an "[OK] 1 emails were successfully sent." as result, but in fact no e-mail is sent.
(… No spooling, by the way. bin/console swiftmailer:spool:send returns "0 emails sent".)
Mail delivery seems to be interupted or so, since the mails won't arraive at my mail account, also my SPAM is empty.
Directly invoking the sendmail command on the other hand does work. (My test mails arrive at my SPAM though, but still: The mails are sent.)
Again, how do I have to configure the MAILER_URL for Swift_Mailer in my .env in Symfony 4 to use sendmail?
Is it possible at all?
Alright, the notation already was correct. So this one is valid for using sendmail:
MAILER_URL=sendmail://my-domain.com
My actual problem was that spooling of mails was active. I commented out the spool entry from my swiftmailer.yaml config.
Having a look into the Symfony Profiler helped me a lot here, by the way.
… My mails are still not arriving, but I am sure this has nothing to do with the MAILER_URL. So, my question is answered.
commenting spool in yaml fixed issue for me
Try this config:
swiftmailer:
url: '%env(MAILER_URL)%'
spool: { type: 'memory' }
transport: 'sendmail'
command: '/usr/sbin/sendmail -oi -t'
Path to file:
/my_sf4_project/config/packages/swiftmailer.yaml
I'm using Symfony 4.3.1 and SwiftMailer 6.2.0. You can read in SwiftMailer bundle documentation that:
/**
* Start the standalone SMTP session if running in -bs mode.
*/
and then:
/**
* Set the command to invoke.
*
* If using -t mode you are strongly advised to include -oi or -i in the flags.
* For example: /usr/sbin/sendmail -oi -t
* Swift will append a -f<sender> flag if one is not present.
*
* The recommended mode is "-bs" since it is interactive and failure notifications
* are hence possible.
*
* #param string $command
*
* #return $this
*/
Path to file with these adnotations:
vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/SendmailTransport.php
Before changing into '/usr/sbin/sendmail -oi -t' i was receiving error from SwiftMailer:
app.ERROR: Exception occurred while flushing email queue: Expected response code 220 but got an empty response [] []
After changing sendmail parameters now i'm sending mails successfully.
Not sure I got your problem correctly but in my case I use a Gmail account and here is the 23rd line of my .env file
MAILER_URL=gmail://myadress#gmail.com:myemailpassword#localhost
Have you already installed the SwiftMailer by composer require symfony/swiftmailer-bundle ?
(Sorry for my english)
Ive recently migrated this website (made with Symfony 2) to a new server, and we decided to go with Google Apps to send emails.
After some connection problems, Ive managed to set it up. I know is working because I can send email in the command line using:
app/console swiftmailer:email:send --subject="Test" --body="test" --from="xxxx" --to="yyyyy"
It works, the email is sent and I receive it immediately.
The problem is: inside the site, is not working. Here is the code:
$mailer = $this->get('mailer');
$message = $mailer->createMessage()
->setSubject('Caderno Mágico: Redefinir Senha')
->setFrom(array($this->container->getParameter('server_mail_address') => 'Caderno Mágico'))
->setTo($usuario[0]->getEmail())
->setBody($this->renderView('Emails/recuperar_conta.html.twig', array('username' => $usuario_selecionado->getUsername(), 'dados' => $hash)), 'text/html');
$mailer->send($message);
Seems right, was working before. The user email there is the same email I used on the command line test. And the config used there is supposed to be the same that's being used when I send it though the command line:
swiftmailer:
transport: smtp
host: smtp.gmail.com
username: yyyy
password: ******
auth_mode: login
port: 465
encryption: ssl
spool: { type: memory }
swiftmailer.plugins.loggerplugin:
class: 'Swift_Plugins_LoggerPlugin'
arguments: ['#swiftmailer.plugins.loggerplugin.logger']
tags: [{ name: 'swiftmailer.default.plugin' }]
swiftmailer.plugins.loggerplugin.logger:
class: 'Swift_Plugins_Loggers_EchoLogger'
arguments: [false]
There is no errors on any log, and the "send" method return true. It just dont works. (Ive tried with and without the spool: no difference). No email is sent.
Ive set this "EchoLogger" plugin, and it works on the command line. I see can all messages and errors at the output, which helped me setting the config and fixing the connection problems. But that doest not seems to do anything when being executed by the server, I cant see any messages on the screen or in any logs.
Ive even tried writing all the values in the code exactly like they are on my command line test, to rule out the possibility of something being wrong there, and still nothing:
$mailer = $this->get('mailer');
$message = $mailer->createMessage()
->setSubject('Test')
->setFrom('yyyyy')
->setTo('xxxxx')
->setBody('test');
$mailer->send($message);
So what can I do? What could be wrong? I dont even know how to debug this. Why would it work when run at the command line and fail silently when run inside the website? Is there any way or any place to see those EchoLogger messages when the code is being run at the server?
Ok, Ive found my own solution. Well, more like a workaround, actually.
It seems like I was having a spool problem: messages were going to the spool and then never sent, for some reason.
So Ive found 2 ways to make ir work:
force a spool flush right after sending:
$mailer->send($message);
$spool = $mailer->getTransport()->getSpool();
$transport = $this->get('swiftmailer.transport.real');
$spool->flushQueue($transport);
explicitly disable the spool in the config:
spool:
enabled: false
In both ways, emails are sent normally
This might be because you didn't tell the controller to send the mail, use this:
$this->get('mailer')->send($message);
Here's a description of my environment first, since it's a bit particular :
I use a Windows 8.1 PC combined with a virtual machine running with Vagrant. It's basically a Debian 64bits machine.
This VM is in fact my server.
Then, I use Symfony 2 for my project and I'm trying to use SwiftMailer with gmail (using my own gmail adress) in order to send emails thanks to a contact form (email address of the user, name of the user and content of the mail).
My action fills the email data with the ones given in the form and sends the mail.
Btw, it checks if the data is valid by using the ->isValid() method.
My problem comes after I submit the form, I receive an Exception from SwiftMailer : Connection could not be established with host smtp.gmail.com [ #0]
Here's my config_dev.yml :
swiftmailer:
transport: gmail
username: "%gmail_user%"
password: "%gmail_password%"
delivery_address: "%gmail_user%"
encryption: ssl
host: smtp.gmail.com
port: 465
auth_mode: login
The "%gmail_user%" and stuff is defined in a parameters.yml file. Here's the content :
parameters:
mailer_transport: gmail
mailer_user: null
mailer_password: null
gmail_user: my_gmail_address#gmail.com
gmail_password: my_gmail_passwd
After a lot of search through the whole Internet, I've tried many things from here or from elsewhere, and nothing did work : Using tls, trying port 587, trying with the IP of the smtp.gmail.com server gave me the same error.
The SSL extension seems to be enabled too :
php --info | grep openssl
OpenSSL support => enabled
and some other stuff that tells the version of OpenSSL.
I checked my IMAP settings and the general setting that enables or disables applications to access to my gmail, it is enabled.
I also tried to telnet smtp.google.com 465 and it worked fine since I reached to connect.
I just don't know what to do now. I hope some of you had the same issue and can help me.
Have a nice day !
Edit : Problem solved and a new one appeared
After further investigation, I randomly tried to change the config_dev.yml file that way :
transport: mail
This solved my problem with the SwiftMailerException, though the mail doesn't come to the delivery_address email address even if I change it to another address I own (an hotmail one).
I'll work on it, but I hope my solution can help.
Edit #2 : Tried to solve the sending problem but nothing worked.
This is going to be a book..
I tried to add a spool system by adding this in the config.yml :
spool: {type: memory }
The mail seem to be sent since the profiler tells me he sent it.
I added this to my controller to send all the mails that could be spooled :
$spool = $transport->getSpool();
$sent = $spool->flushQueue($this->container->get('swiftmailer.transport.real'));
I also added this to check if it's really sent :
if ($this->mailer->send($message)) {
echo '[SWIFTMAILER] sent email to ' . $toEmail;
} else {
echo '[SWIFTMAILER] not sending email: ' . $mailLogger->dump();
}
I am really lost and don't know what to do..
Here's the code that send the mail in the controller :
$message = \Swift_Message::newInstance()
->setSubject("The subject from the form")
->setFrom("The address from the form")
->setTo("The address which comes from an entity")
->setBody("The body from the form made with a twig template");
$mailer = $this->container->get('mailer');
$mailLogger = new \Swift_Plugins_Loggers_ArrayLogger();
$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger));
if ($mailer->send($message)) {
echo 'sent email';
} else {
echo 'not sending email: ' . $mailLogger->dump();
}
This always tells me 'sent email', but I never receive it.
Did you try to make a connection with cURL? I had something similar with elasticsearch (elastica), apparently it used a cURL HTTP proxy I didn't know about because it was configured in php.ini.
off-topic: You can also try if mailcatcher works for your needs.
I had the same error (Connection could not be established with host smtp.gmail.com [ #0]) and the reason was in antivirus (Avast!). When I disabled it error has gone.
I found the solution and never answered here but since this question has like 2000+ views I feel like it's important.
So, everything was fine and the only thing I didn't know about was that during development (aka when in dev environment), swiftmailer seems to disable delivery, as told here :
http://symfony.com/doc/current/email/dev_environment.html#disabling-sending
All I had to do was to set this in config_dev.yml and config_test.yml files :
swiftmailer:
disable_delivery: false
Just be careful to which address your mails go. For example, on this project, it would have sent dummy mails to our clients which would have been quite problematic.
To prevent this, you can do what's told in the symfony documentation (http://symfony.com/doc/2.8/email/dev_environment.html#sending-to-a-specified-address-es)
# app/config/config_dev.yml
swiftmailer:
delivery_addresses: ['dev#example.com']
I'm trying to simply send an email from my server running Symfony 1.4.
I have the usual basic mail server setup, with the server name mail.tixxit.com.au and port 25. I have this configuration in my factories.xml:
mailer:
param:
transport:
class: Swift_SmtpTransport
param:
host: mail.tixxit.com.au
port: 25
encryption: ssl # Not sure on this but have tried "tls" too
username: myaccount#tixxit.com.au
password: mypassword
Then I have a basic bit of send code in one of my actions:
$this->getMailer()->composeAndSend(
"myaccount#tixxit.com.au",
"anotheraccount#gmail.com",
"Message title",
"Message content"
);
From my what I've seen mentioned in other documentation, this should be all I need to do to send email from my server using Symfony in PHP, but this is not working. The send code does not give any errors, I can hit the mail.tixxit.com.au server from my web server fine, my credentials are definitely correct, and I have confirmed with support from my email hosting company that this should work and no configuration is required on their side to allow this.
But my mail isn't being sent. I have tried a whole lot of different settings in factories.xml but nothing works. It appears to send, but nothing ever arrives at anotheraccount#gmail.com. I have tried this on my local machine and my web server and get the same result.
What am I missing here? What Symfony/PHP/server/mail account settings do I need to actually make this work? Is there some fundamental other configuration that I am supposed to have set before the Symfony stuff that will allow me to send?
I couldn't find an answer to this. Nothing seemed to work. I gave up and used the natural Swift classes e.g.:
$transport = Swift_SmtpTransport::newInstance('mail.tixxit.com.au', 25)
->setUsername('myaccount#tixxit.com.au')->setPassword('mypassword');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance("Message title")
->setFrom(array('myaccount#tixxit.com.au' => 'App'))
->setTo(array('anotheraccount#gmail.com'));
$message->setBody("Message content.");
if ($mailer->send($message, $errors)) {
$success = true;
}
I have a web application made with Symfony 2.4 and I use SwiftMailer to send emails.
I use like mailer transport sendmail.
When user send an email, I don't receive this email. But If I do in command line:
$php app/console swiftmailer:spool:send --env=prod
I receive all emails.
¿What can I do?
Thanks a lot!
----Edit----
I have removed all references to spool in my config file, but the problem persist.
----Edit 2----
Config.yml
mailer_transport: sendmail
mailer_host: ****
mailer_user: *****
mailer_password: ****
Delete the spool line from your configuration (parameters.yml i believe? I've never used symfony so i'm interpreting the docs).
Once you've deleted any mention of the spool from your config, it should stop spooling and send immediately.
Having said that, the reason for spooling is so that it sends the email at the end of the execution of the request so that you don't have the performance hit (slowed down requests), probably via a shutdown function... the only reason this function wouldn't execute and therefore your email's not send, is if the script is ending early (i.e. a die/exit or uncatched exception), so you may want to look at addressing the root cause rather than patching around it.
Edit: manual flush (fund this on a command line script example, i've edited it to what i think should make it work for you....)
// now manually flush the queue
$container = $this->getContainer();
$mailer = $this->get('mailer');
$spool = $mailer->getTransport()->getSpool();
$transport = $container->get('swiftmailer.transport.real');
$spool->flushQueue($transport);
http://symfony.com/doc/current/cookbook/console/sending_emails.html