I am using the FOSUser bundle within my Symfony2 application. I am trying to get the email functionality working but can't get any emails to send successfully.
My development environment is a Ubuntu virtual machine. I tried to use my Gmail account details as described here. I also checked the error logs but nothing regarding the email sending is logged.
I attempted to send a test email using a test controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class TestEmailController extends Controller
{
public function sendEmailAction()
{
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('send#example.com')
->setTo('me#myemailaddress.com') // use a valid email in actual code...
->setBody('yo, wassup!');
$this->get('mailer')->send($message);
return $this->render('SysDevPunctualityBundle:TestEmail:sendEmail.html.twig', array(
// ...
));
}
}
However this triggers an error with Symfony's web profiler toolbar and I get the following message; "Token "3c494e" was not found in the database."
After some digging around I found this question I followed the suggestion of removing the spool option and also added a 'from_email' option. This made a difference, I am now getting a timeout error:
Connection could not be established with host smtp.gmail.com [Connection timed out #110]
My guess is that the firewall is blocking the request, I don't know how to find out for sure.
Appreciate if anyone can point me in the right direction.
I discovered that the source of the problem was that my work firewall was blocking access to the Gmail SMTP server. I switched to another SMTP server (set up by our IT department) and email sending now works fine.
Check these two links from the cookbook, to find out how to handle emails while developing: http://symfony.com/doc/current/cookbook/email/dev_environment.html & http://symfony.com/doc/current/cookbook/email/spool.html
Related
I am working on my first Symfony 5 project and struggle to send mail using the build in MailerInterface. I have worked with Swift Mailer in Symfony 3 before and never had similar issues before.
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Mailer\MailerInterface;
...
class SomeController extends AbstractController {
public someAction(Request $request, MailerInterface $mailer) {
...
$email = (new TemplatedEmail())
->from(new Address('address#example.com', 'My Symfony Mail'))
//->to($user->getEmail())
->to('receiver#example.com')
->subject('Subject')
->htmlTemplate('email.html.twig');
$mailer->send($email);
}
}
// .env
#MAILER_DSN=smtp://user:pass#smtp.example.com:25
MAILER_DSN=sendmail://default
If MAILER_DSN has some malformed format an exception is thrown and shown on the Symfony debugger page, e.g. The "invaliddsn" mailer DSN must contain a scheme... Thus it seems that the configured DSN smtp://user:pass#smtp.example.com:25 is correct. Using the same credentials, host and port in other mail applications is no problem.
However, when using this code no error/exception is shown and I receive no mail at all. Of course I have double checked the logs (no errors), the receivers spam folder (nothing). Specifying an SMTP server or sendmail does not make any difference.
The Symfony docs only explain how to handle exceptions but in my case no exceptions are thrown.
While there are a lot of other questions about mailing issues in Symfony, most of them deal with the older Swift Mailer or other, specific problems.
How can I figure out if a mail is send and not received or not send at all?
If it's not throwing an exception, you can assume the transport did not raise an error condition.
From the docs:
Handling Sending Failures
Symfony Mailer considers that sending was successful when your transport (SMTP server or third-party provider) accepts the mail for further delivery. The message can later be lost or not delivered because of some problem in your provider, but that’s out of reach for your Symfony application.
If there’s an error when handing over the email to your transport, Symfony throws a Symfony\Component\Mailer\Exception\TransportExceptionInterface. Catch that exception to recover from the error or to display some message
You can also check the object that send() returns:
The Symfony\Component\Mailer\SentMessage object returned by the send() method of the Symfony\Component\Mailer\Transport\TransportInterface provides access to the original message (getOriginalMessage()) and to some debug information (getDebug()) such as the HTTP calls done by the HTTP transports, which is useful to debug errors.
Finally, if you have the Symfony Web Profiler, since you are doing the sending during a Web Request, you can check the profiler's output for information about the mail sending attempt.
MailerInterface use Messenger, the asynchrone functionality.
If you comment the default configuration in config/packages/messenger.yaml
#Symfony\Component\Mailer\Messenger\SendEmailMessage: async
Yours emails will be sent immediatly.
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 am currently in the process of doing a project in magento.
I have a problem with my contact form: emails are not sent to me.
I did a test and I still get this message:
Unable to submit your request . Please, try again later
It's probably because an exception is thrown as you can see in the file
app/code/core/Mage/Contacts/controllers/IndexController.php in the postAction() function, you should try to debug from here.
Maybe the contact email failled to be sent (if you are in localhost and have no mail server?), that could cause the exception that shows this error.
I've been struggling with this for a day as nobody gives a clear answer on the topic.
In order to get it working there are 2 options:
Configure smtp server for email sending using smtp pro extension from Magento connect
Make Magento use the SendMail function of the webserver
As the first option didn't work for me, as my hosting provider somehow block the smtp outgoing connection, I needed to use the SendMail function.
The quick and dirty trick I used, was to change /app/code/core/Mage/Core/Model/Email/Template.php line 116:
Zend_Mail::setDefaultTransport($transport); → //Zend_Mail::setDefaultTransport($transport);
After this my error message at the contact form was gone and I received
the email correctly.
Be carefull: This is not a good solution as it will be undone at a Magento update. Also it was not tested for the other email functions of Magento.
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 work with Symfony2, up to date (2.4.1). I send an email with Swifmailer, but it never delivered.
Symfony's logs say the email is sent. I tried with memory and file spooling. With the file spooling when I execute swiftmailer:spool:send --env=dev, it says "Processing default mailer... 1 emails sent". So everything is supposed to work. The config is ok (no delivery_address defined for dev or other way to catch mail while developing).
My SysAdmin says there is no problem on the serverside. The others SF2 host on the same server (other Virtual host) are sending mail fine. So I tried to place my app on a supposed working other virtual host, but no way, emails don't arrived. I begin to think it's an issue with the last version of Symfony (my others app are on 2.4.0). I tried to create a new symfony app, from zero, and I get the same problem. I'm lost. Does somebody have a similar problem? Or a way to fix it?
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('noreply#domain.com')
->setTo('myemail#example.com')
->setBody('TEST');
$this->get('mailer')->send($message);
PS: When I send an email with mail() function of PHP, it works.