Laravel send email using mail::to without viewing other recipients - php

I have an array of recipients $this->recipients and I want to send an email to all recipients without showing each other emails.
Currently, it shows all the recipients in an email.
if (count($this->recipients) > 1) {
Mail::bcc($this->recipients)
->send(new EmailNotificationMailable($this->notificationRequest));
} else {
Mail::to($this->recipients)
->send(new EmailNotificationMailable($this->notificationRequest));
}
I tried this code but when I send with Mail::bcc the To of email is empty.
Please give the working solution for this. I don't want to loop recipients array

You need to loop through the recipients collection:
if(count($this->recipients) > 1)
{
$this->recipients->each(function($recipient)
{
Mail::to(recipient)->bcc($this->recipients)->send(new EmailNotificationMailable($this->notificationRequest));
}
}else{
Mail::to($this->recipients)->send(new EmailNotificationMailable($this->notificationRequest));
}

Use something like this:
Mail::to(array_pop($this->recipients))->bcc($this->recipients)
This will set the last entry in the recipients array as the mail receiver and every other addresses will be included via BCC.

Related

Queue email receiver email address issue laravel 4.2

I am working in laravel 4.2 version and sending email for marketing purpose. But the problem is that when i send email to many users then all the users can view the email addresses of other users.
I am sending email using laravel queue method and here is my working code
$emails[] = 'someonea#gmail.com';
$emails[] = 'someoneb#gmail.com';
$emails[] = 'someonec#gmail.com';
$emails[] = 'someoned#gmail.com';
$emails[] = 'someonee#gmail.com';
if(!empty($emails)){
$data['content'] = $message;
$admin_email = UserHelper::$driver['admin_email'];
$site_title = UserHelper::$driver['site_title'];
Mail::queue('emails.market',$data,function($mail)use($emails,$subject,$data){
$mail->to($emails);
$mail->subject($subject);
$mail->from($emails);
});
}
when i receive an email then i am also able to see the email all other users in to of inbox.
Please help to resolve this issue.
Thanks in advance
You can try bcc to send the same email to other users. When you use BCC, any recipients on the Bcc line of an email are not visible to others on the email.
Mail::queue('emails.market',$data,function($mail)use($emails,$subject,$data){
$mail->to($firstEmailAddredd);
$mail->to($restAllEmailAddredd);
$mail->subject($subject);
$mail->from($emails);
});
Not tested this thing, but sure that this will help you!

Can't send BCC emails through Mandrill (via Laravel)

I am unable to send emails to users through the Mandrill plugin in Laravel using BCC. I can send emails "to" the addresses, as follows:
Mail::send('emails.coach_invite', $data, function($message) use ($coach, $emails) {
foreach ($emails as $email) {
$message->to($email);
}
$message->subject($coach->first_name.' '.$coach->last_name.' has invited you to try Nudge!');
});
This works just fine. However, if I try to BCC the same users:
Mail::send('emails.coach_invite', $data, function($message) use ($coach, $emails) {
foreach ($emails as $email) {
$message->bcc($email);
}
$message->subject($coach->first_name.' '.$coach->last_name.' has invited you to try Nudge!');
});
Nothing happens. Mandrill doesn't even acknowledge that the request came in. Any ideas why this isn't working? If it helps, here are my raw email headers:
Message-ID: <688aa904847640c9ff694521ccb85ee5#nudge-api.app>
Date: Thu, 07 Aug 2014 11:15:35 -0400
Subject: Coach McTest would like to be your Coach on Nudge!
From: Nudge Info <info#nudgeyourself.com>
Bcc: Chris Garson <chris#nudgeyourself.com>
MIME-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
I can confirm that sending emails to Bcc users doesn't really work as expected in Mandrill.
The easiest way to send what you want (single email to multiple addresses, with each addressee only seeing their own name in the delivery list), is to set the X-MC-PreserveRecipients header to false and then just send the email using the To field rather than Bcc.
This will send the email as if it were sent to each recipient individually, rather than as a group email - nobody will know who else was sent the email.
Here's how to do it in Laravel using your example:
Mail::send('emails.coach_invite', $data, function($message) use ($coach, $emails) {
foreach ($emails as $email) {
$message->to($email);
}
$message->subject($coach->first_name.' '.$coach->last_name.' has invited you to try Nudge!');
$headers = $message->getHeaders();
$headers->addTextHeader('X-MC-PreserveRecipients', 'false');
});
Note that I'm using $message->to() to address the email and then adding the X-MC-PreserveRecipients header which is set to false.
I worked for me both CC and BCC way.
Ref document: https://mandrill.zendesk.com/hc/en-us/articles/205582117-Using-SMTP-Headers-to-customize-your-messages
Search keyword: X-MC-PreserveRecipients
$headers = $message->getHeaders();
$headers->addTextHeader('X-MC-PreserveRecipients', true);
Using the X-MC-BccAddress should do the job. You can only use one bcc address though.
See https://mandrill.zendesk.com/hc/en-us/articles/205582117-Using-SMTP-Headers-to-customize-your-messages

Php loop foreach behave strange and prints out wrong html

I am trying to prepare email html and I have 2 groups of customers-
1 group just will receive email
2 group will receive same email + voucher inside..
I have prepared a short code just to illustrate the problem. Here it is:
$voucher='<div>VOUCHER IMAGE COMES HERE</div>';//only group for voucher must see it
$mail_description='Here comes my mail text';
$voucherarray=array("mail1#gmail.com","mail3#gmail.com");
$mailaray=array("mail1#gmail.com","mail2#gmail.com");
$html='';
foreach($mailaray as $email){
$html.='<div>'.$mail_description.'</div>';
if(in_array($email, $voucherarray)){
$html.=$voucher;
echo $email.'is for voucher';
}else{ $email.'is not for voucher';}
$html.='<table><tr><td>here is some other text</td></tr></table>
<div clss="footer"></div>';
mail($email,'subject',$html);
}
echo $html.'<br />';
this code prints out:
mail1#gmail.comis for voucher
Here comes my mail text
VOUCHER IMAGE COMES HERE
Here comes my mail text
here is some other text
Here comes my mail text
VOUCHER IMAGE COMES HERE
Here comes my mail text
here is some other text
What is wrong with it and why it prints out VOUCHER IMAGE COMES HERE for both mails as only one is for voucher?
Also it sends same mail to all groups users
Your need to separate between the html you send in the mail and the output you want to see for the job that sends the mail. second thing you need to empty the mail body variable after each iteration, you don't want every client to receive the mail content of the previous clients.
$voucher='<div>VOUCHER IMAGE COMES HERE</div>';//only group for voucher must see it
$mail_description='Here comes my mail text';
$voucherarray=array("mail1#gmail.com","mail3#gmail.com");
$mailaray=array("mail1#gmail.com","mail2#gmail.com");
$html='';
foreach($mailaray as $email){
$mailHtml = '';
$mailHtml.='<div>'.$mail_description.'</div>';
if(in_array($email, $voucherarray)){
$mailHtml.=$voucher;
echo $email.'is for voucher<br>';
}else{
echo $email.'is not for voucher<br>';
}
$mailHtml.='<table><tr><td>here is some other text</td></tr></table><div clss="footer"></div>';
mail($email,'subject',$mailHtml);
$html .= '<br>' . $mailHtml;
}
echo $html.'<br />';

PHP Mailer --- Reply-to: --- Return-path --- SetFrom

I am sending mail via PHP Mailer. http://phpmailer.worxware.com/
I want to be able to set the From to one emailand the REPLY-TO to another email and the RETURN-PATH to yet another.
Mainly.. I want the bounced emails to go to something like BOUNCEDemails#bademail.com
I was hoping the RETURN PATH could do this.
And if a user who gets the email I don't want them to see its from BOUNCEDemails etc.. to I want to give them an option to reply to a real email address.
I need the bounced emails tho to go to a seperate email because I don't want the REPLY TO to get many bad emails. etc..
HERE IS WHAT I HAVE: Does Not work
$mail->AddAddress('ed#RealEmail.org', 'John Doe');
$mail->AddReplyTo('replytoMe#email.com', 'Reply to email');
$mail->SetFrom('mailbox#email.com', 'From Name and Email');
$mail->AddCustomHeader('Return-path: BOUNCEDemails#bademail.com');
The code above replies to SetFrom and sends all bounces to SetFrom. Any ideas how to separate the two? Thanks
the correct way to set this (as of july 2013) is by using:
$mail->ReturnPath='bounce_here#domain.com';
the phpmailer source contains the following, which is fairly self explanatory:
if ($this->ReturnPath) {
$result .= $this->HeaderLine('Return-Path', '<'.trim($this->ReturnPath).'>');
} elseif ($this->Sender == '') {
$result .= $this->HeaderLine('Return-Path', '<'.trim($this->From).'>');
} else {
$result .= $this->HeaderLine('Return-Path', '<'.trim($this->Sender).'>');
}
You may use
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('replytoMe#email.com', 'Reply to email');
$mail->AddAddress('ed#RealEmail.org', 'John Doe');
Notice the order! AddReplyTo has to be BEFORE AddAddress!!!

Codeigniter: Email attachment of last emails not cleared while sending multiple emails in loop

My code sends multiple emails in loop with attachment,
Problem is attachments of last(previous all) emails get attached to next email.
ex. suppose 3 emails in database with 1 attachment in each(a1.pdf, a2.pdf, a3.pdf)
then,
it sends email with attachment as
email 1:
attachment :a1.pdf
email 2:
attachment :a1.pdf, a2.pdf
email 3:
attachment :a1.pdf, a2.pdf, a3.pdf
I am using codeigniter framework.
My code is (this code is called in loop)
.
.
.
$this->email->subject($item->subject);
$this->email->message($message);
$attachments='';
if(strlen($item->attachment) > 5)
{
$attachments = explode(',', $item->attachment);
foreach($attachments as $attachment)
{
if(strlen($attachment)>5)
$this->email->attach(FCPATH . 'attachments/' . $attachment);
}
}
$this->email->send();
.
.
.
You need to reset it in CodeIgniter.
At the end of the loop add:
$this->email->clear(TRUE);
This resets all email variables including the attachments, allowing you to create a new mail.
You need to use $this->email->clear(); to clean out the variables set within the loop. Read the manual.

Categories