I have my website in production, with Hostinger, and with the same parameters and config I can't send emails in production but I could in developing mode. I have changed the parameters and config files with others settings I have seen in other questions but with no success. Anyway, the error I get is the following:
Connection could not be established with host smtp.gmail.com
[Connection timed out #110] 500 Internal Server Error -
Swift_TransportException
My parameters.yml file:
parameters:
...
mailer_transport: gmail
mailer_encryption: ssl
mailer_auth_mode: login
mailer_host: smtp.gmail.com
mailer_user: xxxxx#gmail.com
mailer_password: xxxx
...
My config.yml file:
# Swiftmailer Configuration
swiftmailer:
transport: gmail
host: smtp.gmail.com
username: xxxxx#gmail.com
password: xxxx
My controller file:
public function kontaktuaAction()
{
$kontaktua = new Kontaktua();
$form = $this->createForm(new KontaktuaType(), $kontaktua);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$message = \Swift_Message::newInstance()
->setSubject($kontaktua->getGaia())
->setTo('xxxxx#gmail.com')
->setFrom($kontaktua->getEmaila())
->setBody($kontaktua->getIzena().'(e)k, email helbidea '.$kontaktua->getEmaila().' duena hurrengo mezu hau'.
' bidali dizu: '.$kontaktua->getMezua());
$this->get('mailer')->send($message);
return $this->redirect($this->generateUrl('kontaktua'));
}
}
return $this->render('TaldeaBundle:Default:kontaktua.html.twig', array(
'form' => $form->createView()
));
}
The following settings works for me:
config.yml under swiftmailer
encryption: ssl
auth_mode: login
parameters.yml:
mailer_transport: smtp
mailer_host: smtp.gmail.com
mailer_user: #gmail_user_name
mailer_password: #gmail_password
Related
I want to send confirmation email to users. I did install swiftmailer with composer and these are my configuration in parameters. yml and config.yml
parameters:
database_host: 127.0.0.1
database_port: null
database_name: xxxx
database_user: root
database_password: root
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: xxxxx#xxxxxx.com
mailer_password: xxxx
secret: ea293ee3152cb8522e591a6cb821f950cc67f499
spool:
type: file
mailer_port: 587
mailer_encryption: tls
mailer_logging: '%kernel.debug%'
config.yml
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }
I added this code to my controller and I tested but nothing sent:
$message = \Swift_Message::newInstance()
->setSubject('hello')
->setFrom('example#gmail.com')
->setTo('xx.xx#xxx.tn')
->setBody('#servicom/pages/profile_commercial.html.twig', 'text/html');
# Send the message
$this->get('mailer')
->send($message);
I want to ask if the problem is from the configuration or from the code.
Thanks in advance.
See if you need some other parameters like encryption (typicaly tls) or port (generaly 587).
Documentation for adding properties there
Finally i fixed the issue by debugging first then adding the ini_set() so i share this solution :
Parameters.yml :
parameters:
database_host: 127.0.0.1
database_port: null
database_name: xxxxx
database_user: root
database_password: root
mailer_transport: smtp
mailer_host: ssl0.ovh.net
mailer_user: xxxx#myhost.com
mailer_password: toguess
encryption: ssl
auth_mode: login
secret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Add this to the function send :
ini_set('SMTP','ssl0.ovh.net');
ini_set('smtp_port',587);
And this for the Debug :
if ( $this->get('mailer')->send($message)) {
echo '[SWIFTMAILER] sent email to ' . 'malek.zarkouna#esprit.tn';
echo '' . $mailLogger->dump();
} else {
echo '[SWIFTMAILER] not sending email: ' . $mailLogger->dump();
}
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.
I have the following code to send mails:
/* Emails */
$message = \Swift_Message::newInstance()
->setSubject('Ihre Sammelrechnung')
->setFrom("xxx#myserver.com")
->setTo("recipient#gmail.com")
->setBody(
$this->renderView(
'email/invoiceMultiFinal.html.twig', array(
'company' => 'COMPANY',
)
),
'text/html'
);
/* Send eMail */
if(!$this->get('mailer')->send($message)){
die("ERROR");
}
I know 100% that sender an recipient are correct. In my Logs I have:
{"message":"Exception occurred while flushing email queue: Failed to authenticate on SMTP server with username \"xxx#myserver.com\" using 1 possible authenticators","context":[],"level":400,"level_name":"ERROR","channel":"app","datetime":{"date":"2016-05-31 15:19:09.919207","timezone_type":3,"timezone":"Europe/Berlin"},"extra":[]}
config.yml
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory } #Deleting this changes nothing
parameters.yml
parameters:
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: xxx#myserver.com # adding "" doesn't change sth either
mailer_password: password
secret: xxxxxxx
I am using my own server, so no Gmail etc. involved. I can send and receive emails through the webmailer AND through php bin/console swiftmailer:email:send
Any ideas what can cause such behaviour? Side note, it worked like a charm for almost a month! It stopped today, possibly related to a reboot!
I got a strange behavior when sending email using Swiftmailer
here is the paramaters:
#Swiftmailer parameters # app/config/parameters.yml
parameters:
mailer_transport: smtp
mailer_host: ssl0.ovh.net
mailer_user: my_account#my_domain.com
mailer_password: my_account_password
mailer_port: 465
auth_mode: plain
encryption: ssl
the config
# Swiftmailer Configuration # app/config/config.yml
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
port: "%mailer_port%"
encryption: "%encryption%"
auth_mode: "%auth_mode%"
Now the controller and the action
class MailTestController extends Controller
{
public function SymfonyParamatersAction()
{
$message=\Swift_Message::newInstance()
->setSubject('smtp test with sm params')
->setFrom('my_account#my_domain.com')
->setTo('my_account#gmail.com')
->setBody('this is a test')
;
$result =$this->get('mailer')->send($message);
dump($this->get('mailer')->getTransport());die;
//return $this->render('...');;
}
}
So in the profiler I got one email sent but I dont receive any mail
I added this action to my controller
public function DirectParamatersAction()
{
$transport = \Swift_SmtpTransport::newInstance('ssl0.ovh.net', 465,'ssl')
->setUsername('my_account#my_domain.com')
->setPassword('my_account_password')
;
$transport->start();
$mailer = \Swift_Mailer::newInstance($transport);
$message=\Swift_Message::newInstance()
->setSubject('smtp test with direct params')
->setFrom('my_account#my_domain.com')
->setTo('my_account#gmail.com')
->setBody('this is a test')
;
$result =$mailer->send($message);
dump($transport);die;
return $this->render('.........');
}
with this action I received the message
the funny part (strange behavior) is when running these actions, I get different result:
1=> message is sent
2=> Expected response code 250 but got code "235", with message "235 ok, go ahead (#2.0.0)
3=>Connection could not be established with host ssl0.ovh.net [Connection timed out #110]
I clean the cache every time I change the config
I have to reload the page again and again to send the message
my question is what is wrong with the parameters and the first action?
secondly how to avoid this strange behavior?
the problem was in old configuration in app/conf/config_dev.yml
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