symfony swiftmaile strange behavior - php

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

Related

Symfony 3 - Swiftmailer not sending email

I'm attempting to send an email using SwiftMailer (version = Swift-5.4.8) after a user submits a form. I cannot determine why the email is not being sent. I have only tried using gmail setup as I've heard it is easiest to use (am new to web dev).
I have tried changing config.yml, parameters.yml
parameters.yml:
parameters:
database_host: 127.0.0.1
database_port: null
database_name: symfony
database_user: root
database_password: null
mailer_transport: gmail
mailer_encryption: ssl
mailer_auth_mode: login
mailer_host: smtp.gmail.com
mailer_user: myemail#gmail.com
mailer_password: mypass
secret: mysecret
config.yml (relevant snippet, please lmk if need other portions)
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }
DefaultController
$form = $this->createFormBuilder()
->add('from', EmailType::class)
->add('message', TextareaType::class)
->add('send',SubmitType::class)
->getForm()
;
if ($form->isSubmitted()){
$data = $form->getData();
$message = \Swift_Message::newInstance()
->setSubject('Join Our Team Request')
->setFrom($data['from'])
->setTo('myemail2#gmail.com')
->setBody(
$data['message'],'text/plain'
)
;
$this->get('mailer')->send($message);
}
return $this->render('xxx.html.twig', array("title" => "...","join_team_form" =>$form->createView()));
The view and form are rendered correctly. It appears the form submits correctly, however I never receive an email. Again new to web dev, any suggestions appreciated.
EDIT: I was able to successfully configure and send an email using the command prompt and the 'php bin\console swiftmailer:email:send' command. The issue was it was using a dev_config file, silly error. However, when I still fill out the form and click the send button, no email gets sent. Only using the above command. Any insight?
EDIT2: Solution to first EDIT was running 'composer update'.... nice...
The best practice to send mails with gmail on Symfony is described in this documentation
But for this you need to have environment variables loaded in your application, and I don't know it's your case.
The below should work. Update your config.yml to
swiftmailer:
url: 'gmail://%mailer_user%:%mailer_password%#localhost?encryption=tls&auth_mode=oauth'
spool: { type: 'memory' }
Another suggestion is to check that your form is valid before sending the mail. Otherwise the send will fail if the from is null or ""
if ($form->isSubmitted() && $form->isValid()){ ... }
Let me know if it don't work
Try to use tls encryption and smtp tranport protocol.
Your config.yml will be similar to this:
# Swiftmailer Configuration
swiftmailer:
transport: 'smtp'
host: 'smtp.gmail.com'
username: 'myemail#gmail.com'
password: 'mypass'
encryption: tls
auth_mode: login
And, you must activate access to less secure applications and disable two step authentification in your gmail account.

Symfony 3.3 swiftmailer not send mail on localhost

I'm sure everything has been done properly, but the mail is not delivered. Could there be anything else I should try to do?
config.yml
swiftmailer:
transport: "%mailer_transport%"
encryption: ssl
port: 465
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
parameter.yml
mailer_transport: smtp
mailer_host: smtp.gmail.com
mailer_user: on*****#gmail.com
mailer_password: ******
controller.php
echo "mail gönder";
$this_is = 'this is';
$the_message = ' the message of the email';
$mailer = $this->get('mailer');
// $mailerUser = $this->>container->getParameter('mailer_user))
// echo $mailerUser;
// result on*****#gmail.com
$message = \Swift_Message::newInstance()
->setSubject('The Subject for this Message')
->setFrom($this->container->getParameter('mailer_user'))
->setTo('onu****#hotmail.com')
->setBody($this->renderView('mail/email.html.twig', ['this'=>$this_is, 'message'=>$the_message]))
;
$mailer->send($message);
I have tried too much of an attempt but failed. I am using Nginx server. Could this be the reason?
Thank You.
Maybe with the transport gmail like explained in Symfony's doc.
Oww.
I solved the problem, but the solution seemed a bit strange to me. I ended mail class with the die; command. The problem is solved when use return $this->redirectToRoute(...).
I think it reason in the spool parameter. Spool Email.

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.

Swiftmailer sending from console but not from PHP

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!

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