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.
Related
Here I use swift_message to send messages with Symfony, and I've set my swift_mailer. But in the setFrom (""), I entered the email address in hard, which allows me to send the mails, but I would like to know if there is another way to inject this address, so that it n does not appear in my code.
Parameters.yml
parameters:
database_driver: pdo_mysql
database_host: localhost
database_port: 3306
database_name: doctrax
database_user: root
database_password: null
mailer_transport: gmail
mailer_host: smtp.gmail.com
mailer_user: minitest#gmail.com
mailer_password: "785468961A;"
secret: ThisTokenIsNotSoSecretChangeIt
mailer_auth_mode: login
mailer_encryption: ssl
mailer_port: 465
Controller
$mailer = $this->get('mailer');
$message = (new \Swift_Message('Email de Confirmaton'))
->setFrom("minitest#gmail.com")
->setTo($patient->getUser()->getUsername())
->setBody(
$this->renderView(
// app/Resources/views/Emails/registration.html.twig
'Emails/confirmation.html.twig',
array('name' => 'mam')
),
'text/html'
);
$mailer->send($message);
Now I would like to know if there is another way to inject the email address of the sender, and that his email does not appear in the code at the setFrom.
Thanks
As described in the doc here, you could add the sender_address as example:
mailer_user: [USERNAME]
mailer_password: [PASSWORD]
mailer_port: 465
mailer_encryption: ssl
mailer_sender_address: noreply#mydomain.com
Hope this help
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.
I want to send emails using symfony, but the swiftmailer does not send out any emails. I even dont get error reportings or anything else. Thats my code:
$mail = \Swift_Message::newInstance()
->setSubject('Subject')
->setTo('test#example.com') #this is replaced by real email of course
->setFrom('test#example.com')
->setBody('Testbody');
$this->get('mailer')->send($mail);
Thats the config:
swiftmailer:
default_mailer: mailer
mailers:
mailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
#spool: { type: memory }
I even tried to set the host to an address that does not exist, but I dont get any error from swiftmailer or symfony.
I tried to find the files for the lib, but there is no Swift_Message or newInstance anywhere in the symfony files, strange
use this code
$message = \Swift_Message::newInstance()
->setSubject('Validation de votre commande')
->setFrom('test#example.com')
->setTo('test#example.com')
->setBody('Testbody');
->setCharset('utf-8')
->setContentType('text/html')
->setBody('test');
$this->get('mailer')->send($message);
in app/config/parametres.yml :
mailer_transport: gmail
mailer_host: smtp.gmail.com
mailer_user: your mail
mailer_password: your password mail
in app/config/config.yml :
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
I had seen someone with the same problem. For him the problem was with the spool queue. It is solved by either disabling spool explicitly:
spool:
enabled:false
or flushing the queue in the code:
$this->get('mailer')->send($mail);
$spool = $this->get('mailer')->getTransport()->getSpool();
$spool->flushQueue($this->get('mailer')->getTransport());
Hope it works!
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.
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