Sending mail with swiftMailer SMTP and Debian - symfony

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

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)

Sending Email in Symfony 3 in dev mode

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' :)

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.

Configuration to send Email with Symfony herberged on OVH

I am trying to send email with my Symfony Application. My goal is to send the emails either with a gmail account or from the server's email account.
After some research I succed to send emails from my local server wamp. The configuration is the following :
swiftmailer:
transport: %mailer_transport%
encryption: %mailer_encryption%
auth_mode: %mailer_auth_mode%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
spool: { type: memory }
mailer_transport: smtp
mailer_encryption: ssl
mailer_auth_mode: login
mailer_host: smtp.gmail.com
mailer_transport: gmail
mailer_user: myaccount
mailer_password: mypassword
I tried this configuration on my OVH server but it doesn't work. I looked in the OVH server logs but didn't find any error message.
I also tried some configuration with my server email account from my local server WAMP without success.
Here is one example :
swiftmailer:
transport: %mailer_transport%
auth_mode: %mailer_auth_mode%
host: %mailer_host%
port: %mailer_port%
username: %mailer_user%
password: %mailer_password%
spool: { type: memory }
mailer_transport: smtp
mailer_auth_mode: login
mailer_host: smtp.mydomain.be
mailer_port: 587
mailer_user: admin#mydomain.be
mailer_password: mypassword2
The solution is to set your mailer_transport parameter to 'mail' so SwiftMailer will use the default PHP mail() function.
Otherwise, you could specify the transport directly from your controller:
// Mail() transport
$transport = \Swift_MailTransport::newInstance();
// Message
$message = \Swift_Message::newInstance()
->setFrom("me#domain.org", "My Name")
->setTo(array(
"user#domain.org" => "User Name"
))
->setSubject("Solution for sending e-mail from OVH")
->setBody("...", 'text/plain')
;
// My instance of mailer
$mailer = \Swift_Mailer::newInstance($transport)
->send($message);
Worked for me on an OVH plan with shared e-mail.

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