Swiftmailer not sending emails to the delivery_addresses in dev mode - symfony

I am having trouble sending emails to the email address specified in config_dev.yml:
swiftmailer:
delivery_addresses: ['dev#example.com']
I am trying to send from a console command:
php app/console ecard:task:run send --env=dev
Console command code:
...
foreach ($emailQueue as $item) {
$transport = Swift_SmtpTransport::newInstance($item['smtp_host'])
->setPort($item['smtp_port'])
->setUserName($item['smtp_login'])
->setPassword($item['smtp_password'])
->setEncryption('ssl');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject($item['email_subject'])
->setFrom($item['email_from'], $item['email_from_name'])
->setTo($item['email_to'])
->addPart($item['email_template'], 'text/html');
$ret = $mailer->send($message, $failedRecipients);
....
}
The emails are sent to the real recipient addresses, and not the one I specified in config_dev.yml.
If I dump $this->getContainer()->getParameter('kernel.environment') in code, its 'dev'.
I don't get the $mailer this way: $this->getContainer()->get('mailer'), because I want specify/change the smtp settings in cycle - the config is stored in the database, and not in the parameters.yml. Maybe this is the problem?
(Symfony version 2.8.16, Swiftmailer 5.4.5)

If you get your mailer with:
$mailer = Swift_Mailer::newInstance($transport);
then it ignores all the parameters specified in config.yml.
In this case you must set the destination address in code, as you do with the other mailer parameters, something like:
$message = Swift_Message::newInstance()
->setSubject($item['email_subject'])
->setFrom($item['email_from'], $item['email_from_name'])
->setTo($this->getContainer()->getParameter('swiftmailer.delivery_addresses')?:$item['email_to'])
->addPart($item['email_template'], 'text/html');

Related

Symfony Mailer base64 encoding

How can we use the setEncoder() function that we used in the old swiftmailer library on the new Symfony mailer? I think setEncoder has been removed from symfony mailer. Every mail sent is automatically quoted-printable.
The transport and email I created normally work fine.
$transport = Symfony\Component\Mailer\Transport::fromDsn($env);
$mailer = new Symfony\Component\Mailer\Mailer($transport);
$message = (new Symfony\Component\Mime\Email());
$message->from("..");
...
$message->html('<p>Lorem ipsum</p>','utf-8');
$mailer->send($message);
But where should we use the base64 encoder value?
$encoder = new Symfony\Component\Mime\Encoder\Base64Encoder();

Contact form 7 not getting email

I'm using contact form 7 and facing a problem with this email id, I'm not getting any mail what we can do for that. I used SMTP also.
could you please suggest me any other option.
have you checked spam folder ? Perhaps emails are in spam for email not get in spam you can use https://wordpress.org/plugins/wp-mail-smtp/
Here is setup documentation https://wpforms.com/docs/how-to-set-up-smtp-using-the-wp-mail-smtp-plugin/.
Please check may be helpful for you.
The best option is using PhpMailer library in php. You can check if the email was sent successfully using php mailer. All you need to do is to take all the library code and add it to your FTP or install it using composer.
Here is a simple example.
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
// Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
You can get all the information on PhpMailer here Php Mailer On Github
The PhpMailer is well configured so you don't have to worry about your emails ending up in Spam folder. Also be careful on the number of mails you send periodically to single recipients.

Symfony Swiftmailer: Conditionally spool emails or send immediately

Has anyone ever set a condition on spooling emails with Swiftmailer in Symfony?
I'd like to have the option to either send my emails immediately or spool them in a file, depending on which function I'm running.
I have the email service abstracted in an own Bundle and just call the sendEmail() function in other Bundles when needed. But for some Bundles/Functions, I'd like the emails to be sent out immediately and for others, spooling is fine. I thought about using a spool parameter in my sendEmail() function, so if the parameter is set true when calling the function, the emails get spooled and if it's set to false, they get send immediately.
Or maybe a simple if condition would be sufficient?]
Any ideas, tips, experiences etc. would be awesome!
Update
in my config.yml:
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool:
type: file
path: /srv/http/test/spool
By specifying a spool option in the parameters, Swiftmailers will use an instance of Swift_Transport_SpoolTransport which will manage the spool by sending messages to the Queue instead of instead of sending them out to the world straight away. Through the Transport object, you can access the Spool instance (either a Swift_MemorySpool or a Swift_FileSpool) and force Swiftmailer to flush the queue.
// custom function to send an email
// inject \Swift_Mailer like you normally would
public function sendMessage($name, \Swift_Mailer $mailer, $bypassSpool = false)
{
$message = new \Swift_Message('Hello Email')
->setFrom(/* from */)
->setTo(/* to */)
->setBody(/* render view */);
$mailer->send($message); // pushes the message to the spool queue
if($bypassSpool) {
$spool = $mailer->getTransport->getSpool()
$spool->flushQueue(new Swift_SmtpTransport(
/* Get host, username and password from config */
));
}
}
You can configure two mailers. One with spooling and another without. And use one or the other based on mail priority.

setCC : Swift Mailer and Mandrill

I am trying to send an email with a CC email address. Unfortunately, it seems that it doesn't work.
To set the CC address, I use the function setCc() like this :
$mailLogger = new \Swift_Plugins_Loggers_ArrayLogger();
$this->mailer->registerPlugin(new
\Swift_Plugins_LoggerPlugin($mailLogger));
/** #var \Swift_Message $message */
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from_array)
->setTo(trim($to_email))
->setCc('test#test.com')
->setBody($body, 'text/html');
I receive the email on the Cc address but it doesn't appear as 'Cc'.
Also, in Mandrill dashboard, I can see that two different emails has been sent.
Is there a way to send mail with Cc with SwiftMailer and Mandrill ?
Try to use instead of:
->setCc('test#test.com')
use:
->addCc('test#test.com')

I can't get SwiftMailer to send in a Symfony2 Command

I'm trying to setup a Command to send out warning emails, but I can't get SwiftMailer to actually send them.
I read some posts about flushing the Spooler but I am still not seeing emails going to Postfix.
The emails do work in a controller, and I receive them. So I know that Postfix itself is working, and that SwiftMailer works from a Controller.
This is the execute function I have
protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getContainer();
$entityManager = $container->get('doctrine')->getManager();
$mailer = $container->get('mailer');
$transport = $container->get('swiftmailer.transport.real');
$things = $container->get('mike.stuff.service')->getThings();
foreach ($things as $thing) {
$recipients = array($thing->getPerson()->getEmail() => $thing->getPerson()->getName());
$message = sprintf('Hello %s'.PHP_EOL.PHP_EOL.'This is an alert regarding %s',
$thing->getPerson()->getName(),
$thing->getReferenceNumber());
$email = \Swift_Message::newInstance()
->setSubject('Alert')
->setFrom('noreply#mydomain.com')
->setTo($recipients)
->setBody($message);
$mailer->send($email);
}
}
// Flush the mailer transport
$spool = $mailer->getTransport()->getSpool();
$sent = $spool->flushQueue($transport);
echo 'I sent ' . $sent;
}
When I run the command the output is "I sent 1", so it looks like it has worked. But /var/log/mail.log does not have anything added to it (so of course I don't receive an email).
Commands are run in the dev environment by default, so if you have different configs for dev and prod environments, you will need to run your command in the prod environment (with --env=prod)

Resources