Symfony 2 and swiftmailer, I can not send mail with gmail suit - php

I'm trying to send symfony 2 a mail. I'm using google suite, I have a registered domain on aruba.it but use google mail for mail. It is a domain address, ie mail#mydomain.com. In the configuration file I have:
Auth_mode: login
Transport: "% mailer_transport%"
Encryption: "% mailer_encryption%"
Host: "% mailer_host%"
Port: "% mailer_port%"
Username: "% mailer_user%"
Password: "% mailer_password%"
Sender_address: "% mailer_sender_address%"
In the parameter file, mailer_transport: smtp mailer_host: smtp.gmail.com. But the mail is not sent. Can someone help me? How can I debug? Is there a log file?
I have tried with mailer_transport: gmail in parameters and transport: gmail in config but don't work.

I am using the following details in my parameters, it always seems to work. You might also want to read through their documentation
mailer_transport: smtp
mailer_encryption: ssl
mailer_auth_mode: login
mailer_host: smtp.gmail.com
mailer_user: youremailaddress
mailer_password: yourpassword
mailer_port: 465

Related

Symfony 3 can't send email test

I'm a newbie with Symfony 3 (Symfony generally)
I want to send an email, just to test my parameters, if everything's going well, but that doesn't work.
Here my parameters.yml file:
database_host: 127.0.0.1
database_port: null
database_name: learnsymfony
database_user: root
database_password: null
mailer_transport: gmail
mailer_encryption: ssl
mailer_auth_mode: login
mailer_host: smtp.gmail.com
mailer_user: 'personalemail#gmail.com'
secret: 7759e1c20bb67e4ec0fbac5c3a4de22beed3b95d
And Here my config.yml code for swiftmailer:
transport: gmail
host: smtp.gmail.com
username: 'personalemail#gmail.com'
password: 'myPassword'
spool: { type: memory }
I found this configuration in one question here in the forum, but for my case, that was given an error "Exception occurred while flushing email queue: Connection could not be established with host smtp.gmail.com [ #0]"
Any suggestion please?
I tried alot of other configurations but nothing works well...
thanks
I also faced this problem today and got resolved by following how to send email in Symfony
After following these steps don't forget to import these in your default controller:
use Symfony\Component\Routing\Annotation\Route as Route;
use Symfony\Component\HttpFoundation\Response;
And make sure your email and password which you are using "#app/config/parameters.yml" are correct.

Send mail from localhost via Symfony2 Command using Swiftmailer

Command function
$message = \Swift_Message::newInstance('test')
->setContentType("text/html")
->setFrom('x#x.com')
->setTo('x#gmail.com');
$message->setBody('test');
if ($this->getApplication()->getKernel()->getContainer()->get('mailer')->send($message))
{
return true;
}
return false;
When I execute command in Command Line I get true like mail is sent.
Paramters.yml
mailer_transport: gmail
mailer_host: smtp.gmail.com
mailer_user: x#gmail.com
mailer_password: xxxxxxxxxxxxx
Config.yml
swiftmailer:
spool:
type: file
path: "%kernel.root_dir%/../swiftmailer"
transport: %mailer_transport%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
I had read somewhere that Symfony Profiler can be used to see if mail has sent but I just want to physically send mail to my address in testing purpose. I just need info where I do wrong things.
I must notice that I using allowed device security tool in gmail account.
Can that be reason why mail is not sent?
Yes! I found a solution.
I had removed spool in Config.yml and add port and encryption.
# Swiftmailer Configuration
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
port: %mailer_port%
encryption: %mailer_encryption%
username: %mailer_user%
password: %mailer_password%
and in Parameters.yml
mailer_transport: gmail
mailer_host: smtp.gmail.com
mailer_port: 465
mailer_encryption: ssl
mailer_user: yourMail#gmail.com
mailer_password: yourNewGeneratedAppPassword
after that I was available to see errors using
$mailer = $this->getContainer()->get('mailer');
$logger = new \Swift_Plugins_Loggers_ArrayLogger();
$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));
Then after message is sent I had used:
echo $logger->dump();
To print error in terminal.
Below is whole function.
public function sendMail($email, $data)
{
$mailer = $this->getContainer()->get('mailer');
$logger = new \Swift_Plugins_Loggers_ArrayLogger();
$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));
$message = \Swift_Message::newInstance('test')
->setContentType("text/html")
->setFrom('x#mdpi.com')
->setTo($email);
$message->setBody($data);
if ($mailer->send($message))
{
echo $logger->dump();
return true;
}
return false;
}
Tips:
Also you must authenticate your app, ofcourse if you using gmail.
via this link https://myaccount.google.com/security
There is link
app password
Here is description https://support.google.com/accounts/answer/185833?hl=en
After click there you will need to give custom name to your app and generate new password which you will using in
Parameters.yml >> mailer_password:
to connect app with gmail account.

sender mail isn't the chosen with symfony2

I am trying to send an email from some user to the site owner.
I use swiftmailer Bundle of Symfony. this is my code:
Controller
$mailTo = $this->container->getParameter('mailer_user');
$mailFrom = $message->getEmail();
$mail = Swift_Message::newInstance()
->setSubject('Contact ' . $message->getCountry() . ' from Lemarquis')
->setFrom($mailFrom)
->setTo($mailTo)
->setBody($this->renderView(
'WsMailerBundle:Email:message1.html.twig', array(
'message' => $message)))
->setContentType('text/html');
$this->get('mailer')->send($mail);
app/config.yml
swiftmailer:
transport: %mailer_transport%
encryption: %mailer_encryption%
auth_mode: %mailer_auth_mode%
port: %mailer_port%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
app/parameters.yml
parameters:
database_driver: pdo_mysql
database_host: localhost
database_port: null
database_name: ...
database_user: ...
database_password: ...
mailer_transport: smtp
mailer_encryption: ssl
mailer_port: 465
mailer_auth_mode: login
mailer_host: smtp.gmail.com
mailer_user: someaddress#somedomain.tn
mailer_password: somepwd
The problem is that e-mail is sent from %mailer_user% to %mailer_user%, even with different user addresses.
->setFrom($mailFrom)
You can use this to overwrite %mailer_user% in "FROM" field, however email will still be send from %mailer_user%. This field should be used only to add name of the sender, not to change his email address.
If You still want to change shown email You can try, but You can have problems on many servers (for example GMAIL is blocking that option: How to set 'FROM' property using Swiftmailer with Gmail? ).

Send email with symfony2

I can't send email with symfony2
app/config/config.yml
swiftmailer:
transport: smtp
encryption: ssl
auth_mode: login
host: smtp.gmail.com
username: mymail#gmail.com
password: mypassword
src/Ga/CmsBundle/Resources/config/routing.yml
emailing:
pattern: /emailing
defaults: { _controller: "GaCmsBundle:main:emailing" }
src/Ga/CmsBundle/Controller/mainController.php
public function emailingAction()
{
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('mymail#gmail.com')
->setTo('anothermail#yahoo.com')
->setBody('mahdi musawie');
$this->get('mailer')->send($message);
return new response("mahdi");
}
try to set a port (this one was 465 for google apps) or use transport: gmail
this one works fine for me: (i use google apps)
mailer_transport: gmail
mailer_host: smtp.gmail.com
mailer_user: xxx#my-domain.com
mailer_password: xxx
mailer_port: 465

symfony2: emails are not sent

$mail = \Swift_Message::newInstance()
->setSubject($message->getSubject())
->setFrom($message->getEmail())
->setBody($message->getBody());
$this->get('mailer')->send($mail);
Mails are not sent when using swiftmailer. Yet, when I go step by step in the debugger, it seems to send the email and returns sent=1. However I don't receive anything in my mailbox (gmail). I use my gmail account for sending emails, as shown below:
parameters:
mailer_transport: gmail
mailer_host: ~
mailer_user: username#gmail.com
mailer_password: my-password
delivery_address: username#gmail.com
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
spool: { type: memory }
I've checked apache error log, nothing. I've run php app/console swiftmailer:spool:send just in case, but no luck.
What could prevent emails from being sent ?
You need either to:
1) Remove the spool line in your swiftmailer configuration, this one:
spool: { type: memory }
2) Trigger the spool queue to be sent:
php app/console swiftmailer:spool:send
But I think you are looking for option 1)
u can try this one...
Parameter.yml
mailer_transport: gmail
mailer_encryption: ssl
mailer_auth_mode: login
mailer_host: smtp.gmail.com
mailer_user: 'xxxxxxxxxxxx'
config.yml
swiftmailer:
transport: gmail
host: smtp.gmail.com
username: 'Yourmail-id#gmail.com'
password: 'Password'
The ->setTo part was missing ! That solved it.
$mail = \Swift_Message::newInstance()
->setSubject($message->getSubject())
->setFrom($message->getEmail())
->setTo("me#gmail.com")
->setBody($message->getBody());
First, when you set spool: { type: memory } it means that you actually want to send the emails manually by running the spool command: php app/console swiftmailer:spool:send. So removing that spool line is going to help.
Second, you need to properly configure your mailing host. For development purposes, I strongly recommend that you use a service such as SendGrid and configure it as detailed in this Symfony2 Mailing Setup Tutorial
The tutorial also contains details on how to properly setup a mailing service in Symfony2 and how to build mailing templates.
Third, if you want to stick with Gmail, then the correct data is:
mailer_transport: smtp
mailer_host: smtp.gmail.com
mailer_user: your_gmail_address#gmail.com
mailer_password: 'your_gmail_password'
mailer_port: 587

Categories