Send email with multiple messages to different users in sendgrid - php

We are using sendgrid for email,
we have tried,
$email = new SendGrid\Email();
$emails = array("foo#bar.com", "another#another.com", "other#other.com");
$email->setTos($emails);
$email->setHtml(array($message1,$message1));
$sendgrid->send($email);
How to set different - different $email->setHtml(array($message1,$message1)) at a time.

As per I understand from your question you want to send different messages to different email ids. Which can be achieve by
$email = new SendGrid\Email();
$emails = array("foo#bar.com", "another#another.com", "other#other.com");
$message = array("message1","message2","message3"); //create a array of messages according to email ids
$i =0 ;
foreach ($emails as $value) {
$email->setTos($value);
$email->setHtml($message[$i]);
$sendgrid->send($email);
$i++;
}

As your question is confusing, I will assume you want to send different email messages to all users in the list. So:
$email = new SendGrid\Email();
$emails = array("foo#bar.com", "another#another.com", "other#other.com");
$messages = array("message1", "message2");
foreach ($messages as $msg) { // Grab every message...
$email->setTos($emails); // for everyone...
$email->setHtml($msg); // set it as the body...
$sendgrid->send($email); // and send it.
}

Related

reading inbox with IMAP is not very fast

I am using IMAP to read my last 3 unseen emails.
I am using php. The email provider is fastmail.
I use the following approach.
I get the emails where $d is yesterday.
$s = 'SUBJECT "New email from " SINCE "'.$d.'" UNSEEN';
$emails = imap_search($this->conn,$s);
$emails is an array of email ids.
I loop over them like so:
foreach ($emails as $index => $i) {
$message = imap_body($this->conn, $i) . '<br>';
$overview = imap_fetch_overview($this->conn, $i, 0);
$structure = imap_fetchstructure($this->conn, $i);
if (!empty($overview[0])) {
$subject = $overview[0]->subject;
}
//from this point i get some html and i just process it.
$mes = imap_body($this->conn, $i);
}
How can i make the whole process faster? Any ideas?
I am not an expert in IMAP.
Is there any way that i can just get the text body, and not the rest of the email?
Thanks in advance!

Trying to get emails from database using Yiimail

A little background,
I'm creating a simple function that emails all users whenever the admin of a blog creates a new announcement. I want to gather all emails using an sql query and inputting them all inside the message body or perhaps looping sending the emails one at a time (though that may seem like it would take longer).
So far this is my code:
public function emailAll()
{
$this->set_mail_settings();
$message = new YiiMailMessage;
$request = Yii::app()->db->createCommand("
SELECT email FROM persons WHERE party_id
")->queryRow();
$message->subject = 'Star Cruises - Login Information';
$message->addTo('sendTo#someone.com');
$message->from = Yii::app()->params['adminEmail'];
Yii::app()->mail->send($message);
exit();
}
private function set_mail_settings()
{
$sysParam = SystemParameters::model()->getSystemParameters(
array("smtp_host", "smtp_port", 'smtp_user','smtp_password')
);
Yii::app()->mail->transportOptions['username'] = $sysParam['smtp_user'];
Yii::app()->mail->transportOptions['password'] = $sysParam['smtp_password'];
Yii::app()->mail->transportOptions['host'] = $sysParam['smtp_host'];
Yii::app()->mail->transportOptions['port'] = $sysParam['smtp_port'];
}
The emailAll() function is called whenever an email is used.
My problem is the $request. I don't know how I would gather all the emails and putting them into the $message->addTo();
UPDATE: I was doing it fine until I reached this point:
public function emailAll()
{
$this->set_mail_settings();
$message = new YiiMailMessage;
$emails = Yii::app()->db->createCommand("SELECT group_concat(email) as em FROM persons")->queryRow();
$email_ids = explode(",",$emails["em"]);
$message->setBcc($email_ids);
$message->setBody('Sample');
$message->subject = 'New Announcement!';
//$message->addTo('blah#blah.com');
$message->from = Yii::app()->params['adminEmail'];
Yii::app()->mail->send($message);
}
private function set_mail_settings()
{
$sysParam = SystemParameters::model()->getSystemParameters(
array("smtp_host", "smtp_port", 'smtp_user','smtp_password')
);
Yii::app()->mail->transportOptions['username'] = $sysParam['smtp_user'];
Yii::app()->mail->transportOptions['password'] = $sysParam['smtp_password'];
Yii::app()->mail->transportOptions['host'] = $sysParam['smtp_host'];
Yii::app()->mail->transportOptions['port'] = $sysParam['smtp_port'];
}
Then I got an error on this line from YiiMail.php:
189 $msg = 'Sending email to '.implode(', ', array_keys($message->to))."\n".
And it stated that : array_keys() expects parameter 1 to be array, null given.
I understand what it wants, but I don't understand WHY this error occurred?
You can get all email ids with use of below query and use BCC instead of To for security reason.
$emails = Yii::app()->db->createCommand("SELECT group_concat(email) as em FROM persons WHERE party_id = $party_id")->queryRow();
$email_ids = explode(",",$emails["em"]);
$message->setBcc($email_ids); // use bcc to hide email ids from other users
$message->addTo("noreply#yourdomain.com"); //as To is required, set some dummy id or your own id.

Programmatically send emails pass customer vars

I'm working on the custom e-mail sending script which will get customers details and pass them into e-mail template.
foreach ($emails as $email) {
//send feedback
$customer = Mage::getModel("customer/customer");
$customer->loadByEmail($email);
$emailTemplate = Mage::getModel('core/email_template')->loadByCode('e-mail templaet code');
$emailTemplateVariables = array();
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
$emailTemplate->setSenderName('Sender name');
$emailTemplate->setSenderEmail('sender#example.com');
$emailTemplate->setTemplateSubject("email subject");
$emailTemplate->send('mail#example.com','Some name Some surname', $emailTemplateVariables);
}
Is there any way so I can pass all vars from $customer to $emailTemplateVariables ?
And those vars will be used as {{var customer.email}} in the e-mail template.
You just need to create an array with a key value pair like below:
array("email"=>"xxx#example.com")
and assign this array to your
$emailTemplateVariables['customer']=array("email"=>"xxx#example.com") variable.
Now you will be able to get the customer email value on template{{var customer.email}}

Find E-Mails with an exact e-mail address on an IMAP server using PHP

I want to connect to an IMAP-server and find all E-Mails that were sent to abc#server.tld. I tried:
$mbox = imap_open("{imap.server.tld/norsh}", "imap#server.tld", "5ecure3");
$result = imap_search($mbox, "TO \"abc#server.tld\"", SE_UID);
but this also listed e-Mails that were sent e.g. to 123abc#server.tld. Is it somehow possible to do a search for exact matches?
Short answer: you can't. I didn't find anything in RFC 2060 - Internet Message Access Protocol - Version 4rev1 saying that it can be done.
But, there is a workaround. First fetch all emails that contain abc#server.tld, then iterate through the results and select only the exact matches.
$searchEmail = "abc#server.tld";
$emails = imap_search($mbox, "TO $searchEmail");
$exactMatches = array();
foreach ($emails as $email) {
// get email headers
$info = imap_headerinfo($mbox, $email);
// fetch all emails in the TO: header
$toAddresses = array();
foreach ($info->to as $to) {
$toAddresses[] = $to->mailbox . '#' . $to->host;
}
// is there a match?
if (in_array($searchEmail, $toAddresses)) {
$exactMatches[] = $email;
}
}
Now you have all emails matching abc#server.tld in $exactMatches

Retrieve the 3 most recent email using imap and php

I'm trying to figure out how to get the latest 3 emails (SEEN and UNSEEN) using imap and php. It need to be ressource-efficient since the mailbox as 1 000 emails inside. Getting all header may need too much ressources I think.
I just need the sender, the subject and the date...
Any idea? Thanks for any syggestion/help/explaination/hint...
I did it like that:
$mbox = imap_open("{imap.myconnection.com:993/imap/ssl}INBOX", "username", "password");
// get information about the current mailbox (INBOX in this case)
$mboxCheck = imap_check($mbox);
// get the total amount of messages
$totalMessages = $mboxCheck->Nmsgs;
// select how many messages you want to see
$showMessages = 5;
// get those messages
$result = array_reverse(imap_fetch_overview($mbox,($totalMessages-$showMessages+1).":".$totalMessages));
// iterate trough those messages
foreach ($result as $mail) {
print_r($mail);
// if you want the mail body as well, do it like that. Note: the '1.1' is the section, if a email is a multi-part message in MIME format, you'll get plain text with 1.1
$mailBody = imap_fetchbody($mbox, $mail->msgno, '1.1');
// but if the email is not a multi-part message, you get the plain text in '1'
if(trim($mailBody)=="") {
$mailBody = imap_fetchbody($mbox, $mail->msgno, '1');
}
// just an example output to view it - this fit for me very nice
echo nl2br(htmlentities(quoted_printable_decode($mailBody)));
}
imap_close($mbox);
PHP-Ref IMAP: http://php.net/manual/en/ref.imap.php
Regards
Dominic
What about
imap_search($res, 'RECENT');
?
http://php.net/manual/en/function.imap-search.php
$msgnos = imap_search($mbox, "UNSEEN", SE_UID);
$i=0;
foreach($msgnos as $msgUID) {
$msgNo = imap_msgno($mbox, $msgUID);
$head = imap_headerinfo($mbox, $msgNo);
$mail[$i][] = $msgUID;
$mail[$i][] = $head->Recent;
$mail[$i][] = $head->Unseen;
$mail[$i][] = $head->from[0]->mailbox."#".$head->from[0]->host;
$mail[$i][] = utf8_decode(imap_utf8($head->subject));
$mail[$i][] = $head->udate;
}
return $mail;
imap_close($mbox);
Will do the job.

Categories