Sending Email in Symfony 3 in dev mode - symfony

In need help learning how to send email in development mode using the Symfony framework with the Swift_mailer library.
Here is my config_dev.yml file:
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }
And my config.yml file:
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }
And my parameters.yml file:
....
mailer_transport: gmail
mailer_host: 127.0.0.1
mailer_user: 'my_gamil_here'
mailer_password: 'my_password_here'
Then in my controller I have the following:
public function indexAction(\Swift_Mailer $mailer)
{
$message = (new \Swift_Message('Hello Email'))
->setFrom('westtexascentral555#gmail.com')
->setTo('kaleyaw#yahoo.com')
->setBody(
$this->renderView(
// app/Resources/views/Emails/registration.html.twig
'Emails/registration.html.twig',
array('name' => 'James')
),
'text/html'
);
$mailer->send($message);
return new Response('Email sent');
}
Can Someone tell me what I'm going wrong. Nothing gets sent to the email account.

Maybe it's because you specified mailer host as localhost. Check this symfony docs http://symfony.com/doc/current/email/gmail.html. And pay attention to this part:
The gmail transport is simply a shortcut that uses the smtp transport
and sets these options:
encryption ssl
auth_mode login
host smtp.gmail.com

Make sure, that mailer_user is set only to your gmail username (without host part). Let's say if I have email myemail#gmail.com, my username would be myemail.
Are you using 2-step verification for your gmail account? If so, additionally you need to generate app password for your account, as specified in docs:
If your Gmail account uses 2-Step-Verification, you must generate an App password and use it as the value of the mailer_password parameter. You must also ensure that you allow less secure apps to access your Gmail account.
Shortly, you can follow here, login, select app (in this case Mail) and the device you want to use mailer with, press Generate and you will see modal with your app password, similar to this snub udpz xcvt jrdp. Copy this password, and use it as your gmail password (without spaces).
If this doesn't help, also check logs, usually if there is a problem with smtp server, swiftmailer prints error to the logs.

'mailer_transport' is the protocole to send mails.
Try to use 'smtp' instead 'gmail' :)

Related

Can't send E-Mails with symfony and swiftmailer using SMTP Server

when I try to send an e-Mail I get the error:
Exception occurred while flushing email queue: Connection could not be
established with host w00d8ef1.kasserver.com [ #0]
.env
MAILER_URL=smtp://w00d8ef1.kasserver.com:25?encryption=ssl&auth_mode=login&username=myusername&password=mypassword
swiftmailer.yaml
swiftmailer:
url: '%env(MAILER_URL)%'
spool: { type: 'memory' }
Controller:
$message = (new Swift_Message('Test'))
->setFrom('info#lama.resper.de')
->setTo($user->getEmail())
->setBody(
$this->renderView(
'pdf/usercard.html.twig',
['user' => $user]
),
'text/html'
)
;
$mailer->send($message);
Hope someone can help me. The Mailserver is running.
Are you sure that you are using the right port number for smtp? Generally, it runs on 465 or 587. So, try to change the port number in your MAILER_URL env variable.
symfony 4.3
try .env file loks like
MAILER_URL=smtp://YOURDOMAIN.COM:465?encryption=ssl&auth_mode=login&username=USEREMAIL#DOMAIN.COM&password=YOURPASSWORD
in config/packages/dev/swiftmailer.yaml (for dev env)
insert:
swiftmailer:
transport: smtp
username: user#domain.pl
password: 'yourpasswor{$##'
host: mail.yourdomain.pl
port: 465
encryption: ssl
auth-mode: login
spool: { type: 'memory' }
stream_options:
ssl:
allow_self_signed: true
verify_peer: false
verify_peer_name: false
use
php bin/console debug:config swiftmailer
to checkout your config
same for production yaml file (but ssl shoud not be self signed)

i cant send email symfony3 neither to confirm registration or for a simple email

Im developping a web application using symfony3 , i'm trying to send confirmation emails using swiftmailer , i want to use confirmation mails and resetting also send direct emails between the users of my app.
Unfortunately i didn't managed to do it yet.
Here is my configs files also the code that i use to send direct mails
Here is my parameter.yml
mailer_transport: gmail
mailer_host: smtp.gmail.com
mailer_auth_mode: login
mailer_user: ***********
mailer_password: ********
mailer_encryption: ssl
Here is my code config.yml
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
encryption: ssl
username: '%mailer_user%'
auth_mode: login
password: '%mailer_password%'
spool: { type: memory }
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User
registration:
confirmation:
enabled: true
from_email:
address: *****e#gmail.com
sender_name: ........
from_email:
address: ******#gmail.com
sender_name: ......
my code to send email
$mailer = $this->get('mailer');
$message = $mailer->createMessage()
->setSubject("Objet")
->setFrom(array('................' => "...."))
->setTo('..........)
->setBody("Hi");
$mailer->send($message);
Here is my parameters file:
mailer_transport: gmail
mailer_host: gmail
mailer_user: ************
mailer_password: **********
And here is my config file section for swiftmailer:
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
I use dual verification in Gmail so I generated a application specific password,
this is where you can see how to set one.
This is how I send emails from my controller:
private function sendConfirmationEmail($user)
{
$token = $user->getEmailConfirmationToken();
$subject = 'Email Confirmation';
$label = 'Confirm Email';
$email = $user->getEmail();
$href = "www.mysymfonyproject.com/app_dev.php/confirm?token=" . $token;
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom('myemail#gmail.com')
->setTo( $email )
->setBody(
$this->renderView(
'Emails/confirm.html.twig',
array('href' => $href)
),
'text/html'
);
$this->get('mailer')->send($message);
return true;
}
Also, I see something slightly odd in your config file, it seems you set your "auth_mode" and "encryption" twice both in your parameters.yml and your config.yml.
Your config.yml file includes your parameters.yml file which is like your parameters.yml get copy-pasted to the config file and in the config file you have mailer settings where some values are pointers to pre set values and other are just values, %mailer_password% points to mailer_password parameter value which gets included from the parameters file. It's nothing complicated it's just YML and inclusion. Since you already defined all parameters in the parameters.yml all you need to do in config.yml is to point to them and you can do so by changing your config.yml section for mailer to look like this:
transport: "%mailer_transport%"
host: "%mailer_host%"
encryption: "%mailer_encryption%"
username: "%mailer_user%"
auth_mode: "%mailer_auth_mode%"
password: "%mailer_password%"
spool: { type: memory }
Or just copy my application settings and give it a try. A YML developers greeting "Good Luck :)"
PS. YML has to have proper indentation and the suggested edit of your config file is probably not indented correctly so indent it manually.

Symfony 3.1 swiftmailer email doesn't send

I wanted to send a test email by using Symfony 3.1 and SwiftMailer, but it doesn't work. I was reading other solutions, but it still doesn't work.
Config.yml:
swiftmailer:
transport: %mailer_transport%
encryption: %mailer_encryption%
auth_mode: %mailer_auth_mode%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
spool: { type: memory }
Parameters.yml
mailer_transport: smtp
mailer_encryption: ssl
mailer_auth_mode: login
mailer_host: smtp.gmail.com
mailer_user: user#gmail.com
mailer_password: mypass
Parameters.yml v.2 I was trying:
mailer_transport: gmail
mailer_encryption: ssl
mailer_auth_mode: login
mailer_host: smtp.gmail.com
mailer_user: user#gmail.com
mailer_password: mypass
Controller:
public function contactAction(Request $request)
{
$name = $request->request->get('name');
$surname = $request->request->get('surname');
$email = $request->request->get('email');
$subject = $request->request->get('subject');
$message = $request->request->get('message');
$data = "";
if($request->request->get('contact_submit')){
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($email)
->setTo('myemail#gmail.com')
->setBody($message);
$this->get('mailer')->send($message);
$data = "Thank you: $name";
}
return $this->render('przedszkole/contact.html.twig', array('data' => $data));
}
So after click Submit, my view change and show me: Thank you $name, but i don't get any email :/
I change security lvl of my gmail e-mail like someone tell in other solutions, but it doesn't help for me :/
I also uncomment swiftmailer: delivery_adress in config_dev.yml.
I will be grateful for any help :/
How about you use (only) the following YML config:
mailer_transport: gmail
mailer_user: user#gmail.com
mailer_password: mypass
I read that there are default settings that you don't need to set for gmail and maybe because you are setting, it has some effect. I'm not sure if this will fix the problem, but you can try it.
I guess the problem might be with emails going into spool queue. This I had seen with someone else as well. You can explicitly disable it using:
spool:
enabled: false
I got a chance to debug your code and all I get is you need to change these two lines
->setFrom($email)
->setTo('myemail#gmail.com')
as
->setFrom('myemail#gmail.com')
->setTo($email)
setFrom() will contain your email ID and setTo() will have the recipients ID.

Sending mail with swiftMailer SMTP and Debian

I have a symfony app runing on a Debian 7 VM. I'm trying to send mails using swiftMailer. All seems to be fine when running the code but no email is received. What am I missing?
Controller :
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('send#example.com')
->setTo('name.lastname#gmail.com')
->setBody('hello');
$mailer = $this->get('mailer');
if (!$mailer->send($message, $failures)) {
echo "Failures:";
print_r($failures);
return "ko";
} else {
return 'email sent successfully';
}
This is always returning "email sent successfully".
config.yml :
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
#spool: { type: memory }
parameters.yml :
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
php.ini :
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = 127.0.0.1
; http://php.net/smtp-port
smtp_port = 25
In order to send emails you have to setup a mail server like postfix.
You can also check the following thread about sending emails from localhost:
How to send email from localhost using PHP on Linux
Always think about logs if you don't know what is happening, you can use the logging option to true, to get some feedback, hopefully you get more informations about the issue http://symfony.com/doc/master/reference/configuration/swiftmailer.html#logging

swiftmailer setFrom() doesnt work

i use Symfony2, and i need to send Email from my application.
in my config.yml:
swiftmailer:
transport: smtp
encryption: ssl
auth_mode: login
host: smtp.gmail.com
in my config_dev.yml:
swiftmailer:
transport: gmail
When i add the Email and the password it works but i don't want to send from static Email
in my Controller i have this:
$message = \Swift_Message::newInstance()
->setSubject('demande de conge ')
->setFrom($col->getEmailCollaborateur())
->setTo($form->get("emailcdp")->getData())
->setBody($this->render('acmeBundle:Conge:demandeCongeNormal.html.twig', array('conge' => $conge, 'id'=> $this->getUser()->getId())))
;
$this->get('mailer')->send($message);
i have this exception :
Expected response code 250 but got code "530", with message "530-5.5.1
Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 o10sm16197wia.0 - gsmtp
You have an indentation error in your yaml file, there is no link between your description & your error message !
Try to add 2 spaces before "transport", in your config_dev.yml.
Take care too, gmail requires an authentication !
# Swiftmailer Configuration
swiftmailer:
transport: "transport_value"
username: "username_value"
password: "passsssword_value"
Edit :
In addition, with gmail you can't send an email using an different mail than the linked account

Resources