Symfony 1.4 sfMailer: Configure TLS self-signed - php

It seems that we need to configure the mailer to use self-signed certificates in a Symfony 1.4 project and no idea where to look at
Configuration right now:
mailer:
class: sfMailer
param:
delivery_strategy: realtime
delivery_address: user#mail.com
transport:
class: Swift_SmtpTransport
param:
host: x.x.x.x
port: 25
encryption: ~
username: user#mail.com
password: pass
I was looking at this question: PHP - Swiftmailer using STARTTLS and self signed certificates
But all examples are with Laravel and it seems very new so I'm not sure if Symfony 1.4 has all these features included.
I also looked at the mailer configuration but I didn't find anything of help there.
Any help on how to configure this without touching anything from vendor folder?
EDIT:
So this is the new configuration:
mailer:
class: sfMailer
param:
delivery_strategy: realtime
delivery_address: user#mail.com
transport:
class: Swift_SmtpTransport
param:
host: x.x.x.x
port: 25
encryption: ~
username: user#mail.com
password: pass
streamOptions:
ssl:
allow_self_signed: true
verify_peer: false
local_cert: "/etc/ssl/certificate.cer"
local_pk: "/etc/ssl/certificate.key"

Yes, you should be able to add these stream options options to the param section of the transport. If I read the class sfMailer right, it should look something like this:
transport:
class: Swift_SmtpTransport
param:
host: x.x.x.x
port: 25
encryption: ~
username: user#mail.com
password: pass
streamOptions:
ssl:
allow_self_signed: true
verify_peer: false

Related

How do I inject different SwiftMailer mailers into different services in Symfony?

I have a Symfony 2.8 project that requires usage of two separate mailing services (eg. Google SMTP and Yahoo SMTP). I have configured them according to the docs:
swiftmailer:
default_mailer: mailer_google
mailers:
mailer_google:
transport: smtp
host: %mailer_google_host%
port: %mailer_google_port%
encryption: tls
username: %mailer_google_username%
password: %mailer_google_password%
mailer_yahoo:
transport: smtp
host: %mailer_yahoo_host%
port: %mailer_yahoo_port%
encryption: tls
username: %mailer_yahoo_username%
password: %mailer_yahoo_password%
The values for the %variables% are all defined in parameters.yml, which are not relevant here.
I'm trying to use a custom Mailer class which is used to define two different services, like this:
aalaap.services.mailer_google:
class: Aalaap\AppBundle\Services\Mail\Mailer
arguments:
- #mailer
- '%mailer_google_sender%'
aalaap.services.mailer_yahoo:
class: Aalaap\AppBundle\Services\Mail\Mailer
arguments:
- #mailer_yahoo
- '%mailer_yahoo_sender%'
Now, because the default mailer is set to mailer_google, in the first service definition, only specifying #mailer works fine. However, the second service can't find #mailer_yahoo.
ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php line 58:
The service "aalaap.services.mailer_yahoo" has a dependency on a non-existent service "mailer_yahoo".
I've tried it with #mailer.mailer_yahoo, but that doesn't work either. If I change the first service definition to use the specific #mailer_google instead of just the default #mailer, that doesn't work either. No luck with #swiftmailer.mailers.mailer_yahoo.
How do I inject the second (or non-default) mailer into a service?
So, this is how to setup multiple mailers accounts and handle injection with Symfony :
Setup the swiftmailer config :
swiftmailer:
default_mailer: mailer_google
mailers:
mailer_google:
transport: smtp
host: %mailer_google_host%
port: %mailer_google_port%
encryption: tls
username: %mailer_google_username%
password: %mailer_google_password%
mailer_yahoo:
transport: smtp
host: %mailer_yahoo_host%
port: %mailer_yahoo_port%
encryption: tls
username: %mailer_yahoo_username%
password: %mailer_yahoo_password%
Then inject the wanted mailer (for example, google mailer) in services this way :
aalaap.services.mailer_google:
class: Aalaap\AppBundle\Services\Mail\Mailer
arguments:
- #swiftmailer.mailer.mailer_google
- '%mailer_google_sender%'
Or (because in that case google mailer is the default mailer) :
aalaap.services.mailer_google:
class: Aalaap\AppBundle\Services\Mail\Mailer
arguments:
- #swiftmailer.mailer.default_mailer
- '%mailer_google_sender%'

Cannot email via gmail

Unable to send mail via gmail in Symfony 2.8.3 in prod on freebsd host while it works on Windows 10 with same source code in both prod & dev. [Also unable to get a log of mailing on host, which might help resolve this.]
Update: Mail can be sent using PHP's mail() function. Using Swiftmailer to send mail from a controller does not work. Changed parameters to use mailer_transport: smtp mailer_host: localhost does not enable Swiftmailer to send from a controller.
parameters.yml:
mailer_transport: gmail
mailer_host: smtp.gmail.com
mailer_user: {valid address}
mailer_password: (valid password}
config.yml:
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
encryption: ssl
auth_mode: login
config_prod.yml:
monolog:
handlers:
mail:
type: fingers_crossed
action_level: critical
handler: buffered
buffered:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: "%mailer_user%"
to_email: "%mailer_user%"
subject: Error report!
level: debug
In the absence of any other solution or finding a way to log Swiftmailer behavior I added a custom exception listener to send mail locally.

not recieving a confirmation email with fosuserbundle

i'm trying to do my first symfony project and i am using the fosuserbundle. It's working perfectly i'm just having a problem with the email confirmation, i'm not receiving anything.
This is my parameters.yml:
parameters:
database_host: 127.0.0.1
database_port: null
database_name: mydatabase
database_user: root
database_password: null
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: mymail#gmail.com
mailer_password: mypass
secret: 8c73d1f9bc7f50ce500d4a98a4627ffaa42dc905
this is myconfig.yml:
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
port: "585"
spool: { type: memory }
fos_user:
db_driver: orm
firewall_name: main
user_class: OC\UserBundle\Entity\User
service:
mailer: fos_user.mailer.twig_swift
registration:
confirmation:
enabled: true
from_email:
address: mymail#gmail.com
sender_name: myname
service:
mailer: fos_user.mailer.twig_swift
please can you help? because i searched on the web and tried but since it's my first project none of the solutions are working with me.
Try with these slight changes in your config.yml:
port: 585
auth_mod: login
The value for the port doesn't need to be enclosed in quotes. I'm not sure if this really makes a difference, but you should follow the conventions.
Since you're using smtp as mailer_transport you have also to specify the authentication mode. Check the documentation for more details:
http://symfony.com/doc/current/reference/configuration/swiftmailer.html
Eventually you'll need another value for the authentication mode.
EDIT (2016-02-19):
Instead of using smtp as mailer_transport you can use the shortcut gmail. Please see this article from the Symfony Cookbook:
http://symfony.com/doc/current/cookbook/email/gmail.html
Then you can omit the line:
auth_mod: login
This shortcut will set the host to smtp.gmail.com. In your configuration you have the host set to 127.0.0.1. This could eventually be the reason why it works with the web_profiler, but not with the real e-mail address.

symfony swiftmailer: mail not sent in prod-environment

I have a symfony 2.8 app, with fosuserbundle implemented. In the dev environment, the registration confirm mails are sent, but not in the prod environment. There is no log message and the smpt configuration is correct.
sf version: 2.8.1
swiftmailer-bundle version: current (compatible with sf version; 2.* in composer json)
fosuserbundle: 1.3.5 (* in composer)
dev-config:
imports:
- { resource: config.yml }
framework:
router:
resource: "%kernel.root_dir%/config/routing_dev.yml"
#strict_requirements: true
profiler: { only_exceptions: true }
web_profiler:
toolbar: true
intercept_redirects: true
assetic:
use_controller: true
Has anyone the same problem?
edit
I just tried a mail with the following console command in prod env: php sf --env=prod swiftmailer:email:send
Terminal output:
#!/usr/bin/env php
From: test#antondachauer.de
To: mail#antondachauer.de
Subject: test
Body: test
Sent 1 emails
But the mail has not been sent. In the dev env, it works also in terminal.
I tried all the solutions mentioned here,
which is "gethostbyname" and "change smtp of Gmail" but it did not work.
After a few days of digging, I found a solution, you need to change the encryption to tls, and add ssl in stream_options in config. It's not recommended according to the security standpoint, but it works in my case.
parameters.yml
mailer_prod_encryption: tls
config.yml
swiftmailer:
transport: "%mailer_dev_transport%"
host: "%mailer_dev_host%"
username: "%mailer_dev_user%"
password: "%mailer_dev_password%"
encryption: "%mailer_dev_encryption%"
auth_mode: "%mailer_dev_auth_mode%"
spool: { type: memory }
port: 25
stream_options:
ssl:
allow_self_signed: true
verify_peer: false

Disable swiftmailer spool in Symfony per environment

I having trouble send bulk emails with symfony2 via console command. I want to disable the mail spooling complete for the env the console is running in but keep it working for the default env. So I have this in config.yml:
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
port: %mailer_port%
username: %mailer_user%
password: %mailer_password%
spool: %mailer_spool_type%
and then each env has it's on parameters like parameters_prod.yml:
parameters:
mailer_transport: smtp
mailer_host: smtp.someserver.net
mailer_port: 587
mailer_user: "someuser"
mailer_password: "somepassword"
mailer_spool_type: { type:memory }
then for the console env I use parameters_cron.yml:
parameters:
mailer_spool_type: ~
The problem is using ~ or null doesn't actually turn off the spool but it uses the default values of {cache: file, path: %kernel.cache_dir%/swiftmailer/spool }
Is there a way to disable the spool just by changing the parameters or is there a better way?
Apparently the only way to disable email spooling is to completely leave out the spool entry from your configuration file. Once you include the spool entry in the main configuration file, there is no way to disable spooling for a specific environment.
That said, you can accomplish the opposite of what you propose: you can disable email spooling by default and enable it in specific environments.
Leave it out in config.yml:
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
port: %mailer_port%
username: %mailer_user%
password: %mailer_password%
And include it in config_prod.yml:
swiftmailer:
spool: %mailer_spool_type%

Categories