Userfrosting ExtendedMailMessage not clearing attachments - php

I am using the ExtendedMailMessage class to try and send an email with an attachments to my clients. The issue is I am sending the email inside a foreach loop and for some reason even though I am defining a new attachment the new attachment is just being appended to an array and each client gets multiple attachments instead of one.
$extendedMailer = $this->ci->extendedMailer;
foreach ($emails as $email) {
$attachment = new MailAttachment(
base64_decode($base64String), "report.pdf"
);
try {
$message = new ExtendedTwigMailMessage($this->ci->view, 'mail/pdf-reports.html.twig');
$message->from($config['address_book.admin'])
->addEmailRecipient(new EmailRecipient($email, 'Client'))
->setFromEmail($config['address_book.admin'])
->setReplyEmail($config['address_book.admin'])
->addAttachment($attachment);
$extendedMailer->sendDistinct($message);
}
catch(\Exception $ex) {
var_dump($email);
}
}
The first client will receive 1 attachment and then the 2nd will receive 2 attachments, 3rd will receive 3 attachments etc...
How do I just send 1 attachement with each email instead of appending it to the old attachments

Since PHPMailer instance is reused for each email, anything set on PHPMailer is carried on to the next email. When the Mailer's send or sendDistinct methods are called, only the recipients are explicitly cleared. So in your case, you might need to explicitly clear the attachement from $message between each email (depending how your addAttachment is implemented).
Another solution might be to move the addAttachment call outside the loop, assuming each email as the same attachement :
$extendedMailer = $this->ci->extendedMailer;
$attachment = new MailAttachment(
base64_decode($base64String), "report.pdf"
);
$message = new ExtendedTwigMailMessage($this->ci->view, 'mail/pdf-reports.html.twig');
$message->from($config['address_book.admin'])
->setFromEmail($config['address_book.admin'])
->setReplyEmail($config['address_book.admin'])
->addAttachment($attachment);
foreach ($emails as $email) {
try {
$message->addEmailRecipient(new EmailRecipient($email, 'Client'));
$extendedMailer->sendDistinct($message);
}
catch(\Exception $ex) {
var_dump($email);
}
}
Note that the following fix will be introduced in UserFrosting V5 send/sendDistinct to avoid this issue :
// Clone phpMailer so we don't have to reset it after sending.
$phpMailer = clone $this->phpMailer;

Related

Unable to move mails to sent folder of IMAP using Laravel

I am sending emails from my laravel website using SMTP. When i send email to the user then i want to copy that mail to IMAP sent folder too.
Here is my code when i am sending mail to user:
$mail = Mail::to($this->receiver)
->send(new ComplaintMail($this->sender->user_email,$this->subject,$this->complaint,$this->answers,$this->sender));
$path = "{mypath.com:993/imap/ssl}Sent";
$imapStream = imap_open($path,$this->sender->user_email,$this->sender->email_password);
$result = imap_append($imapStream,$path,$mail->getSentMIMEMessage());
imap_close($imapStream);
Also I tried using imap_mail_move() method like so:
$mail = Mail::to($this->receiver)
->send(new ComplaintMail($this->sender->user_email,$this->subject,$this->complaint,$this->answers,$this->sender));
$path = "{mypath.com:993/imap/ssl}Sent";
$imapStream = imap_open($path,$this->sender->user_email,$this->sender->email_password);
imap_mail_move($imapStream,$mail,$path);
imap_close($imapStream);
Both ways it didn't worked out
In ComplaintMail class, build function looks like :
public function build()
{
return $this->from($this->sender)
->subject($this->subject)
->markdown('emails.complaint');
}

Sending bulk email using laravel queue

We are trying to send bulk email (100k) with PHP Laravel framework. Which way is the correct way to send bulk email with Laravel queue?
Case 1.
//controller
public function runQueue(){
dispatch(new ShootEmailJob());
}
//job
public function handle(){
$emails = EmailList::get(['email']);
foreach($emails as $email){
Mail::to($email)->send();
}
}
Case 2.
//controller
public function runQueue(){
$emailList = EmailList::get(['email']);
foreach($emailList as $emailAddress){
dispatch(new ShootEmailJob($emailAddress->email));
}
}
//job
public function handle(){
Mail::to($emailAddress)->send(new ShootMail($emailAddress));
}
Which one is the correct approach case 1 or case 2?
The first approach will first fetch all emails and then send them one by one in one "instance" of a job that is run as a background process if you queue it.
The second approach will run n "instances" of jobs, one for each email on the background process.
So performance-wise option 1 is the better approach. You could also wrap it in a try - catch block in case of exceptions so that the job does not fail if one of the emails fails, e.g.:
try {
$emails = EmailList::get(['email']);
foreach($emails as $email){
Mail::to($email)->send();
}
} catch (\Exception $e) {
// Log error
// Flag email for retry
continue;
}

Return error message to a database field: Yii2 and Swiftmailer

I program in PHP using the Yii2 framework and Swiftmailer.
I am trying to find a way to update a database field with error messages. When a user is trying to create an account, and the email he declares is not a valid one, the field FailMesg should be updated with the error message.
I have tried two different ways to do it but none seems to work (the database field remains NULL):
1st ( Include variable in send() method)
$message = Swift_Message::newInstance()
->………..
$transport = ………………
$mailer = Swift_Mailer::newInstance($transport);
if($mailer->send($message, $failures)){
……..
else
……..
$user->FailMesg = $failures;
........
$user->save();
2nd (Use the Logger plugin)
$message = Swift_Message::newInstance()
->………..
$transport = ………………
$mailer = Swift_Mailer::newInstance($transport);
$logger = new \Swift_Plugins_Loggers_ArrayLogger();
$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));
if($mailer->send($message)){
……..
else
……..
$user->FailMesg = $logger->dump();
........
$user->save();
Am I missing something here? Any suggestions will be highly appreciated.
$failures is an array, not a string. From the swiftmailer docs:
If the variable name does not yet exist, it will be initialized as an empty array and then failures will be added to that array. If the variable already exists it will be type-cast to an array and failures will be added to it.
You have to change it into a string before saving e.g.:
$user->FailMesg = implode("|", $failures);

Is it possible to define the mail's subject inside of the blade template?

When sending a mail using Mail::queue / Mail::send you have to pass a mail template and the subject separately.
Is there a way to manage the subject in the mail templates (better for multi-languages).
I.e. as the first line in the template
mail.blade.php
This is the subject
Hello User,
foobar
It's not that hard:
Mail::queue($template, $data, function (Message $message) use ($toUser, $sendingName, $sendingAddress) {
// take subject from first line of the template
$body = $message->getSwiftMessage()->getBody();
$bodyLines = explode("\n", $body);
if (count($bodyLines) == 0) {
Log::warning('Empty mail');
return;
}
$subject = $bodyLines[0];
unset($bodyLines[0]);
// send
$message->getSwiftMessage()->setBody(implode("\n", $bodyLines));
....

Replace multiple placeholders with PHP?

I have a function that sends out site emails (using phpmailer), what I want to do is basically for php to replace all the placheholders in the email.tpl file with content that I feed it. The problem for me is I don't want to be repeating code hence why I created a function (below).
Without a php function I would do the following in a script
// email template file
$email_template = "email.tpl";
// Get contact form template from file
$message = file_get_contents($email_template);
// Replace place holders in email template
$message = str_replace("[{USERNAME}]", $username, $message);
$message = str_replace("[{EMAIL}]", $email, $message);
Now I know how to do the rest but I am stuck on the str_replace(), as shown above I have multiple str_replace() functions to replace the placeholders in the email template. What I would like is to add the str_replace() to my function (below) and get it to find all instances of [\] in the email template I give it and replace it with the placeholders values that I will give it like this: str_replace("[\]", 'replace_with', $email_body)
The problem is I don't know how I would pass multiple placeholders and their replacement values into my function and get the str_replace("[{\}]", 'replace_with', $email_body) to process all the placeholders I give it and replace with there corresponding values.
Because I want to use the function in multiple places and to avoid duplicating code, on some scripts I may pass the function 5 placeholders and there values and another script may need to pass 10 placeholders and there values to the function to use in email template.
I'm not sure if I will need to use an an array on the script(s) that will use the function and a for loop in the function perhaps to get my php function to take in xx placeholders and xx values from a script and to loop through the placeholders and replace them with there values.
Here's my function that I referred to above. I commented the script which may explain much easier.
// WILL NEED TO PASS PERHAPS AN ARRAY OF MY PLACEHOLDERS AND THERE VALUES FROM x SCRIPT
// INTO THE FUNCTION ?
function phpmailer($to_email, $email_subject, $email_body, $email_tpl) {
// include php mailer class
require_once("class.phpmailer.php");
// send to email (receipent)
global $to_email;
// add the body for mail
global $email_subject;
// email message body
global $email_body;
// email template
global $email_tpl;
// get email template
$message = file_get_contents($email_tpl);
// replace email template placeholders with content from x script
// FIND ALL INSTANCES OF [{}] IN EMAIL TEMPLATE THAT I FEED THE FUNCTION
// WITH AND REPLACE IT WITH THERE CORRESPOING VALUES.
// NOT SURE IF I NEED A FOR LOOP HERE PERHAPS TO LOOP THROUGH ALL
// PLACEHOLDERS I FEED THE FUNCTION WITH AND REPLACE WITH THERE CORRESPONDING VALUES
$email_body = str_replace("[{\}]", 'replace', $email_body);
// create object of PHPMailer
$mail = new PHPMailer();
// inform class to use smtp
$mail->IsSMTP();
// enable smtp authentication
$mail->SMTPAuth = SMTP_AUTH;
// host of the smtp server
$mail->Host = SMTP_HOST;
// port of the smtp server
$mail->Port = SMTP_PORT;
// smtp user name
$mail->Username = SMTP_USER;
// smtp user password
$mail->Password = SMTP_PASS;
// mail charset
$mail->CharSet = MAIL_CHARSET;
// set from email address
$mail->SetFrom(FROM_EMAIL);
// to address
$mail->AddAddress($to_email);
// email subject
$mail->Subject = $email_subject;
// html message body
$mail->MsgHTML($email_body);
// plain text message body (no html)
$mail->AltBody(strip_tags($email_body));
// finally send the mail
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent Successfully!";
}
}
Simple, see strtr­Docs:
$vars = array(
"[{USERNAME}]" => $username,
"[{EMAIL}]" => $email,
);
$message = strtr($message, $vars);
Add as many (or as less) replacement-pairs as you like. But I suggest, you process the template before you call the phpmailer function, so things are kept apart: templating and mail sending:
class MessageTemplateFile
{
/**
* #var string
*/
private $file;
/**
* #var string[] varname => string value
*/
private $vars;
public function __construct($file, array $vars = array())
{
$this->file = (string)$file;
$this->setVars($vars);
}
public function setVars(array $vars)
{
$this->vars = $vars;
}
public function getTemplateText()
{
return file_get_contents($this->file);
}
public function __toString()
{
return strtr($this->getTemplateText(), $this->getReplacementPairs());
}
private function getReplacementPairs()
{
$pairs = array();
foreach ($this->vars as $name => $value)
{
$key = sprintf('[{%s}]', strtoupper($name));
$pairs[$key] = (string)$value;
}
return $pairs;
}
}
Usage can be greatly simplified then, and you can pass the whole template to any function that needs string input.
$vars = compact('username', 'message');
$message = new MessageTemplateFile('email.tpl', $vars);
The PHP solutions can be:
usage of simple %placeholder% replacement mechanisms:
str_replace,
strtr,
preg_replace
use of pure PHP templating and conditional logic (short open tags in PHP and alternative syntax for control structures)
Please, find the wide answer at Programmers.StackExchange to find out other approaches on PHP email templating.
Why dont you just make the email template a php file aswell? Then you can do something like:
Hello <?=$name?>, my name is <?=$your_name?>, today is <?=$date?>
inside the email to generate your HTML, then send the result as an email.
Seems to me like your going about it the hard way?

Categories