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

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)

Related

Nonexistent parameter in Swiftmailer

In an attempt to configure both a memory & a spool mailer in a Symfony 4.3 application I followed the docs to create this configuration:
swiftmailer:
default_mailer: memory
mailers:
memory:
sender_address: 'admin#bogus.info'
transport: smtp
username: admin#bogus.info
password: 123Abcd
host: localhost
spool: { type: 'memory' }
spooler:
sender_address: 'admin#bogus.info'
transport: smtp
username: admin#bogus.info
password: 123Abcd
host: localhost
spool:
type: file
path: '%kernel.project_dir%/var/spool'
And in services.yaml:
App\Services\Emailer:
$spoolMailer: '%swiftmailer.mailer.spooler%'
$defaultMailer: '%swiftmailer.default_mailer%'
$senderAddress: '%swiftmailer.mailer.memory_mailer.sender_address%'
$projectDir: '%kernel.project_dir%'
But with those four parameters in the service the following occurs with php bin/console debug:container:
The service "App\Services\Emailer" has a dependency on a non-existent
parameter "swiftmailer.mailer.spooler"...
Why does this configuration not work?
The service "App\Services\Emailer" has a dependency on a non-existent parameter "swiftmailer.mailer.spooler"...
Surrounding parameters with the % symbol allows you to pass values to your services.
As you want to inject a service, you should prefix your parameter with the # symbol.
Also, to get the default mailer service, you have to inject #swiftmailer.mailer
EDIT: Proper way to retrieve the sender address: %swiftmailer.mailer.memory.sender_address%
Updated service definition :
App\Services\Emailer:
$spoolMailer: '#swiftmailer.mailer.spooler'
$defaultMailer: '#swiftmailer.mailer'
$senderAddress: '%swiftmailer.mailer.memory.sender_address%'
$projectDir: '%kernel.project_dir%'

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.

swiftmailer does not send emails

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!

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

Messages are spooled but not sent

I'm using symfony2 with swiftmailer to send some sample messages, but it doesn't work, messages are spooled in symfony/app/cache/swiftmailer/spool but when i do the commande php app/console swiftmailer:spool:send the messages are deleted from the spool but are not sent, the output in the console is: sent 0 emails, someone can tell me where is the problem ?here is my config.yml:
swiftmailer:
transport: smtp
username: ~
password: ~
host: localhost
port: false
encryption: ~
auth_mode: ~
spool:
type: file
path: "%kernel.cache_dir%/swiftmailer/spool"
sender_address: ~
antiflood:
threshold: 99
sleep: 0
delivery_address: ~
disable_delivery: ~
logging: "%kernel.debug%"
and here is my code:
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('test#gmail.com')
->setTo('myadress#gmail.com')
->setBody($this->renderView('ProjetAccountBundle:Main:email.txt.twig'))
;
$this->get('mailer')->send($message);
return $this->render('ProjetAccountBundle:Main:test.html.twig');
}
the problem was the use of smtp instead of mail, the correct configuration file for me was this:
swiftmailer:
transport: mail
username: ~
password: ~
swiftmailer:
spool: { type: memory }
You need to remove this.

Resources