Sending multiple mail issue with laravel 5 - php

I have users database, and I want to send email to all my one form So I am trying like this:
$d=Mail::send('admin.email_template', $data, function ($message) use ($data,$emailIds) {
$message->from('dinesh224401#gmail.com', 'Dinesh Laravel');
$message->to($emailIds)->subject($data['subject']);
});
where $emailIds have 'dinesh.rkgit#rediffmail.com', 'anu.rkgit#rediffmail.com'
but I am getting this error:
Address in mailbox given ['abc1#rediffmail.com', 'abc#rediffmail.com'] does not comply with RFC 2822, 3.6.2.
if I use directly emails in mail function like this:
$message->to('abc1#rediffmail.com', 'abc#rediffmail.com')
then it works,
Updated:
I am making this string from array as:
$aa=implode("', '",array('dinesh.rkgit#rediffmail.com', 'anu.rkgit#rediffmail.com'));
//print_r("'".$aa."'");
$emailIds="['".$aa."']"; //I have used [] here but it did not work also
echo $emailIds
//output ['dinesh.rkgit#rediffmail.com', 'anu.rkgit#rediffmail.com']
I do not know what is problem, Thanks in advance.

Because, $emailIds = 'dinesh.rkgit#rediffmail.com', 'anu.rkgit#rediffmail.com'; is a string, comma separaed.
the to method of Mail requires an array.
Try this:
$emailIds = ['dinesh.rkgit#rediffmail.com', 'anu.rkgit#rediffmail.com'];
$d=Mail::send('admin.email_template', $data, function ($message) use ($data,$emailIds) {
$message->from('dinesh224401#gmail.com', 'Dinesh Laravel');
$message->to($emailIds)->subject($data['subject']);
});

Related

PhpMailer, how do I write the variable value correctly for multiple addCC email address?

I have a PhpMailer working code as follow: (short version)
(the variable already defined before hand)
// Sender and recipient settings
$mail->setFrom($pengirim_email, $pengirim_nama);
$mail->addAddress($untuk_email, $untuk_nama);
$mail->addReplyTo($pengirim_email, $pengirim_nama);
Next, I add multiple email address for CC mail:
$mail-->addCC('aaa#gmail.com','Abdul');
$mail-->addCC('bbb#gmail.com','Borat');
It work as expected.
Now since I'm planning that the email address will come from the SQL query, so for the time being I want to know how do I have to fill the SQL 'CarbonCopy' column table with multiple email addresses - by trying to make a "hardcoded" variable value. So I try like this as the replacement for the addCC above:
$tembusan="'aaa#gmail.com','Abdul';'bbb#gmail.com','Borat'"; //not working
$CC = explode(';', $tembusan); //not working
for ($i = 0; $i < count($CC); $i++) {$mail->addCC($CC[$i]);} //not working
But it throw me an error like this :
Error in sending email. Mailer Error: Invalid address: (cc):
'aaa#gmail.com','Abdul'
So I change the $tembusan into like this:
$tembusan="aaa#gmail.com,Abdul;bbb#gmail.com,Borat"; //not working
It gives me almost the same like the error before:
Error in sending email. Mailer Error: Invalid address: (cc):
aaa#gmail.com,Abdul
Next, I try also this kind of code :
$tembusan="'aaa#gmail.com','Abdul';'bbb#gmail.com','Borat'"; //not working
$CC = explode(';', $tembusan); //not working
foreach($CC as $CCemail){$mail->AddCC($CCemail;} //not working
And it also throw the same error:
Error in sending email. Mailer Error: Invalid address: (cc):
'aaa#gmail.com','Abdul'
If I echo the last code like this foreach($CC as $CCemail){echo $CCemail. '<br/>';}, it give me a result like this :
'aaa#gmail.com','Abdul'
'bbb#gmail.com','Borat'
In my real code, I have a valid email address. The email address in the code above is just for an example.
Where did I do wrong ?
PS
btw, if I remove the "name" for the email address:
$tembusan="aaa#gmail.com;bbb#gmail.com"; //working
$CC = explode(';', $tembusan); //working
foreach($CC as $CCemail){$mail->AddCC($CCemail;} //working
it runs as expected (but in the gmail, the CC name is aaa and bbb).
Please do a further explode . try
$tembusan="aaa#gmail.com,Abdul;bbb#gmail.com,Borat";
$CC = explode(';', $tembusan);
for ($i = 0; $i < count($CC); $i++) {
$DD = explode(',', $CC[$i]);
$mail->addCC($DD[0], $DD[1]);
}
Please note that I have removed the ' characters . (you may use str_replace of PHP to eliminate these characters)

Swiftmailer rejecting valid email addresses if and only if submitted in array

I'm pulling users' phone numbers from an s2member database and appending the wireless carrier domain name required to send SMS texts via email (Swift Mailer). So to create the array of email addresses I use the following (the function cellmap below just replaces the users' wireless carrier with the correct domain to append):
$matches = array();
if (is_array ($users) && count ($users) > 0) {
foreach ($users as $user) {
$user = new WP_User ($user->ID);
$matches[] = "'" . get_user_field("cell_no", $user->ID) . cellmap(get_user_field("cell_carrier",$user->ID)) . "'";
}
}
The resulting array looks like this, for an example of 4 matched users:
Array
(
[0] => '1234567891#vtext.com'
[1] => '3216549871#vtext.com'
[2] => '9876543211#vtext.com'
[3] => '6543219877#vtext.com'
)
Then I implode to create a string to use as the "To" field for Swift Mailer:
$cell_list = implode(", ", $matches);
which results in this (numbers are made up):
'1234567891#vtext.com', '3216549871#vtext.com', '9876543211#vtext.com', '6543219877#vtext.com'
Now I pass that to Swift Mailer like so:
$outgoing_message = Swift_Message::newInstance('')
->setFrom(array('no-reply#mydomain.com' => 'Mydomain'))
->setTo($cell_list)
->setBody($message);
// Send the message
$result = $mailer->send($outgoing_message);
And I get this error (ignore the phone numbers: they're made up, but correspond to the right ones in practice):
PHP Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given ['123456789#vtext.com', '987654321#vtext.com', '5555551212#vtext.com', '321654987#vtext.com'] does not comply with RFC 2822, 3.6.2.'
I can successfully send any one of these emails individually via Swift Mailer, but when they appear as an array I always get the above error. I have tried applying trim to the individual addresses and the entire resulting string. A print_r of the array does not show any non-printable characters. I have tried various combinations of ->setTo($cell_list), ->setTo(array('$cell_list')) and anything else that might work, to no avail. I have tried replacing comma with semicolon in the list, and removing the single quotes around each address. As far as I can tell, the string of email addresses is in the exact format as shown in the Swift Mailer documentation. For the life of me I can't figure this out.
According to Swiftmailer documentation, setTo takes either a single email address or an array of email addresses as parameter.
Your code:
$cell_list = implode(", ", $matches);
$outgoing_message = Swift_Message::newInstance('')
->setFrom(array('no-reply#mydomain.com' => 'Mydomain'))
->setTo($cell_list)
->setBody($message);
Using implode() puts all the email addresses in a single string of text.
I suggest not imploding $matches:
$outgoing_message = Swift_Message::newInstance('')
->setFrom(array('no-reply#mydomain.com' => 'Mydomain'))
->setTo($matches)
->setBody($message);

Problems manipulating JSON data in PHP

I'm not that handy with JSON so here goes. I'm receiving Amazon SNS notifications for bouncing email addresses to a listener (in PHP 5.5) which does:
$post = #file_get_contents("php://input");
$object = json_decode($post, true);
This gives me:
Type => Notification
MessageId => #####
TopicArn => #####
Message => {
"notificationType":"Bounce",
"bounce": {
"bounceSubType":"General",
"bounceType":"Permanent",
"bouncedRecipients":[{"status":"5.3.0","action":"failed","diagnosticCode":"smtp; 554 delivery error: dd This user doesn't have a yahoo.com account (testuser#yahoo.com) [0] - mta1217.mail.bf1.yahoo.com","emailAddress":"testuser#yahoo.com"}],
"reportingMTA":"dsn; ######",
"timestamp":"2014-10-27T16:37:42.136Z",
"feedbackId":"######"
},
"mail": {
"timestamp":"2014-10-27T16:37:40.000Z",
"source":"myemail#mydomain.com",
"messageId":"######",
"destination":["testuser#yahoo.com"]
}
}
I was expecting an associative array all the way down but instead it's an array only at the top level and with JSON strings inside. I've tried everything I can think of, including json_decoding further parts of the array, but I'm struggling to access the data in a simple way. What I need is the "destination" email address which should be in $object['Message']['mail']['destination'][0].
Can anyone point out what I'm doing wrong here? Thanks.
It looks like $object['Message'] is also json encoded. Perhaps because it's using some generic container format for service call results. Try this
$post = #file_get_contents("php://input");
$object = json_decode($post, true);
//Message contains a json string
$object['Message'] = json_decode($object['Message'], true);
//Then access the structure using array notation
echo $object['Message']['mail']['destination'][0];

Sorting Imap mailbox by date using ImapMailbox.php

I have a customer support system which creates emails when an email is received. I used to postfix and a special configuration to get a hold of the emails to add extra features.
For example I want to include attachments that were sent from an email. The system doesnt do this , but creates an email with the subject , so I can include the attachments by matching the subjects.
I used ImapMailBox.php to read through the email contents.
It all works fine but I am getting an issue fetching the last email, so I am gettign contents from any other email with the same subject , so I need to fetch the latest email.
$mailboxP = new ImapMailbox('{127.0.0.1:143/novalidate-cert}',POSTFIX_EMAIL,POSTFIX_PASSWORD,ATTACHMENT_DIR, 'utf-8');
foreach($mailbox->searchMails('ALL') as $mailId)
$mail = $mailbox->getMail($mailId);
$mailx=(array)$mail;
$att=$mailx['attachments'];
I have tried using usort to the object $mail , with a function like this
function
mysort($a,$b) {
return strtotime($a->date)-strtotime($b->date);
}
and to the array with a function like this
function mysort($a,$b) {
return strtotime($a['date'])-strtotime($b['date']);
}
I have also tried using imap_sort to $mail and $mailx , but none of this works.
errors I am getting
imap_sort() expects parameter 1 to be resource, array given
imap_sort() expects parameter 1 to be resource, object given
usort() expects parameter 1 to be array, object given
when passing an array I get undefined index date but it defined ..
Can anyone please be kind enough to point me in the right direction.
You can add a function like this on ImapMailbox.php :
public function searchMailsSorted($imapCriteria = 'ALL') {
$this->checkConnection();
$mailsIds =imap_sort($this->mbox,SORTDATE,1,SE_UID,$imapCriteria,$this->serverEncoding);
return $mailsIds ? $mailsIds : array();
}
And then use it in your code like this:
foreach($mailbox->searchMailsSorted('ALL') as $mailId)
{
///insert code here
}
The easiest way is to use Php rsort() function.
<?php
$emailId = rsort($mailbox->searchMails('ALL');
?>

How to pass a 2 dimensional array value in a swift mailer setTo function

I am getting a 2 dimensional array value as a result after a for loop.The value is $chunk[$i][$j].And when I passed that value into setTo function,
the error showing as
Warning: preg_match() expects parameter 2 to be string, array given in H:\xampp
\htdocs\sngmarket\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Mime\Headers
\MailboxHeader.php line 350.
How do I solve this?.Here my code
$query = $em->createQuery("SELECT DISTINCT u.emailaddress FROM AcmeRegistrationBundle:userlist u");
$grp_emails[] = $query->getResult();
$chunk = array_chunk($grp_emails, 10);
$get_chunk_count = count($chunk);
for($i=0;$i<$get_chunk_count;$i++)
{
$count_inside_count = count($chunk[$i]);
for($j=0;$j<=$count_inside_count;$j++)
{
$mails=$chunk[$i][$j];
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('marketplace#socialnetgate.com')
->setTo($mails)
->setReturnPath('gowtham#ephronsystems.com')
->setBody('Hello World');
$this->get('mailer')->send($message);
return array();
}
}
I think you are overthinking this.
Have you looked at the documentation on how to send batch emails WITHOUT recipients being aware of each other? In your snippet each email contains up to 10 recipients, which may be better then sending all recipients, but still is pretty bad.
Have a look at Sending emails in batch and also at the plugins to make sure you don't reach the limit of emails you are allowed to send in a certain time frame.

Categories