i got this error when i'm trying to send multiple email, and i also want to pass a data to mail view.
here is my controller:
$get_no_inven = Permohonan::find($id_per)->no_inventaris;
$get_users = DB::table('users')->where('hak_akses','=','deputi')->get(['email']);
$recipients = [ $get_users ];
$subject = 'Testing email no 2';
$meta = 'meta';
foreach($recipients as $recipient) {
// here you declare variables accesable in view file
$dataToPassToEmailView = [];
// **key** of this table is variable **name in view**
$dataToPassToEmailView['no_inventaris'] = $get_no_inven;
Mail::send('mailkedua', $dataToPassToEmailView, function($message) use ($subject, $recipient, $meta) {
$message->to($recipient, 'Deputi Manager')->subject($subject);
$message->from('app.staging#nutrifood.co.id','Kalibrasi Online');
});
}
and here is my mail view:
<p>Dear Deputi Manager,</p>
<p>Berikut kami informasikan terdapat hasil kalibrasi terbaru alat dengan nomor inventaris {{$no_inventaris}}
Mohon bantuannya untuk melakukan approval, silahkan akses "Link CALON" http://baf-staging-x2:3030/lihat_permohonan_deputi/</p>
<p>Terima kasih,</p>
<p>CALON</p>
i don't understand where i'm doing wrong, thankyou!
You don't need to wrap the result of your query in an array. Also if you only want the email field from these records you could use pluck to get a list of them:
$recipients = DB::table('users')->where('hak_akses','=','deputi')->pluck('email');
foreach ($recipients as $recipient) {
...
}
This would make $recipient a string for the email address.
Related
Im working with library:
https://github.com/ddeboer/imap
This is my part function:
$mailbox = $connection->getMailbox('INBOX');
// Loop through each email
foreach ($mailbox as $message)
{
// Check if the message already exists in the database
$this->db->where('message_number', $message->getNumber());
$query = $this->db->get('contacts');
// If the message doesn't exist in the database
if ($query->num_rows() == 0)
{
$subject = $message->getSubject();
// Determine the type of the message (new or reply)
if (preg_match('/^re:/i', $subject)) {
$message_type = "reply";
$this->helpdesk_model->insert_new_ticket($message, $message_type);
// Insert the attachments into the database
$attachments = $message->getAttachments();
$message_number = $message->getNumber();
foreach ($attachments as $attachment)
{
$this->helpdesk_model->insert_attachment_to_ticket($attachment, $message_number);
}
} else {
$this->load->model('email_model');
$message_type = "new";
$this->helpdesk_model->insert_new_ticket($message, $message_type);
$this->email_model->send_email_new_ticket_created($message);
// Insert the attachments into the database
$attachments = $message->getAttachments();
$message_number = $message->getNumber();
foreach ($attachments as $attachment)
{
$this->helpdesk_model->insert_attachment_to_ticket($attachment, $message_number);
}
}
}
}
Base on above function I read emails from INBOX and insert to Codeigniter3 database. Currently I add preg_match function, and in match re: in title then I insert this message as reply.
But how to add logic and get any parent ID original message + and each reply for this message?
I try get: message_number but every message have diffrent number, I also try get message_id but for all return for me 0 and im not sure how to get parent ID for all messages in one ticket.
This drive me crazy all the time...
I tried read all the stuff regarding SwiftMailer for Laravel including fixes from other errors base on subject, but I do not get how I am still getting error passing array of email address.
Is there a way to determine how the CC display my email addresses? Whenever I print_r it, it display “name#domain.com; name2#domain.com “.
// current logged id
$user_id = $id;
// match all current logged id to all assets
// if investment type is present, get property id
$asset = ClientPropertyManagement::where('assets.client_id', '=', $user_id)->get();
foreach( $asset as $assets ) {
if( $assets->investment_type == "TIC" ) {
$properties[] = $assets->property_id;
}
}
// if property id is present, connect to property table
// if connected and match id, get property manager emails
if( !empty($properties) ){
$property = DB::table('property')->whereIn('id', $properties)->get();
foreach( $property as $p ){
$contact[] = $p->property_mgmt_contact;
}
}
// if property managers are present, filter
// name#domain.com; name2#domain.com
if( !empty($contact) ) {
$property_contact = implode(', ', array_filter(array_unique($contact)));
}
print_r($property_contact);
// output: "name#domain.com; name2#domain.com "
//send email to owner and other property manager
Mail::send('_dash.emails.profile', $contacts, function ($message) {
$ownerEmail = "name#gmail.com";
$ownerName = "WebMail Information";
$recipient_name = Request::get('firstname') .' '. Request::get('lastname');
$recipient_email = explode(';', Input::get('email'));
$message->from($recipient_email[0],$recipient_name);
$message->cc($property_contact); // send property all managers
$message->to($ownerEmail,$ownerName)->subject("Profile Update");
});
Note: After I dump dd($property_contact) the value was null... how come? Even if I paste the code inside Mail::send still empty.
I forgot to add use ($variable) inside my Mail::send to pass my variable outside Mail::send.
//send email to owner and other property manager
Mail::send('_dash.emails.profile', $contacts, function ($message) use ($property_contact) {
$ownerEmail = "name#gmail.com";
$ownerName = "WebMail Information";
$recipient_name = Request::get('firstname') .' '. Request::get('lastname');
$recipient_email = explode(';', Input::get('email'));
$message->from($recipient_email[0],$recipient_name);
$message->cc($property_contact); // send property all managers
$message->to($ownerEmail,$ownerName)->subject("Profile Update");
});
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.
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}}
The following program is intended to match incoming email aliases with those in the database, and forward the email to the right address, like Craigslist does.
I am now getting this error:
Error: [1] You must provide at least one recipient email address.
in anon-email.php at line number: sending the email
Here is the code:
$mailboxinfo = imap_mailboxmsginfo($connection);
$messageCount = $mailboxinfo->Nmsgs; //Number of emails in the inbox
for ($MID = 1; $MID <= $messageCount; $MID++)
{
$EmailHeaders = imap_headerinfo($connection, $MID); //Save all of the header information
$Body = imap_qprint(imap_fetchbody($connection, $MID, 1)); //The body of the email to be forwarded
$MessageSentToAllArray = $EmailHeaders->to; //Grab the “TO” header
$MessageSentToAllObject = $MessageSentToAllArray[0];
$MessageSentToMailbox = $MessageSentToAllObject->mailbox ."#". $MessageSentToAllObject->host; //Everything before and after the “#” of the recipient
$MessageSentFromAllArray = $EmailHeaders->from; //Grab the “FROM” header
$MessageSentFromAllObject = $MessageSentFromAllArray[0];
$MessageSentFromMailbox = $MessageSentFromAllObject->mailbox ."#". $MessageSentFromAllObject->host; //Everything before and after the “#” of the sender
$MessageSentFromName = $MessageSentFromAllObject->personal; //The name of the person who sent the email
$toArray = searchRecipient($MessageSentToMailbox); //Find the correct person to send the email to
if($toArray == FALSE) //If the alias they entered doesn’t exist…
{
$bounceback = 'Sorry the email in your message does not appear to be correct';
/* Send a bounceback email */
$mail = new PHPMailer(); // defaults to using php “mail()”
$mail -> ContentType = 'text/plain'; //Plain email
$mail -> IsHTML(false); //No HTML
$the_body = wordWrap($bounceback, 70); //Word wrap to 70 characters for formatting
$from_email_address = 'name#domain.com';
$mail->AddReplyTo($from_email_address, "domain.Com");
$mail->SetFrom($from_email_address, "domain.Com");
$address = $MessageSentFromMailbox; //Who we’re sending the email to
$mail->AddAddress($address, $MessageSentFromName);
$mail->Subject = 'Request'; //Subject of the email
$mail->Body = $the_body;
if(!$mail->Send()) //If the mail fails, send to customError
{
customError(1, $mail->ErrorInfo, "anon-email.php", "sending the email");
}
}
else //If the candidate address exists, forward on the email
{
$mail = new PHPMailer(); // defaults to using php “mail()”
$mail -> ContentType = 'text/plain'; //Plain E-mail
$mail -> IsHTML(FALSE); //No HTML
$the_body = wordwrap($Body, 70); //Wordwrap for proper email formatting
$from_email_address = "$MessageSentFromMailbox";
$mail->AddReplyTo($from_email_address);
$mail->SetFrom($from_email_address);
$address = $toArray[1]; //Who we’re sending the email to
$mail->AddAddress($address, $toArray[0]); //The name of the person we’re sending to
$mail->Subject = $EmailHeaders->subject; //Subject of the email
$mail->Body = ($the_body);
if(!$mail->Send()) //If mail fails, go to the custom error
{
customError(1, $mail->ErrorInfo, "anon-email.php", "sending the email");
}
}
/* Mark the email for deletion after processing */
imap_delete($connection, $MID);
}
imap_expunge($connection); // Expunge processes all of the emails marked to be deleted
imap_close($connection);
function searchRecipient() // function to search the database for the real email
{
global $MessageSentToMailbox; // bring in the alias email
$email_addr = mysql_query("SELECT email FROM tbl WHERE source='$MessageSentToMailbox'"); // temp store of the real email
$row = mysql_fetch_array($email_addr); //making temp store of data for use in program
if(empty($row['email']))
{
return FALSE;
}
else /* Else, return find the person's name and return both in an array */
{
$results = mysql_query("SELECT * FROM tbl WHERE email = '$email_addr'"); // temp store of both queries from this function
$row = mysql_fetch_array($results, $email_addr); //making temp store of data for use in program
$name = $row['author']; // taking the author data and naming its variable
return array($name, $email_addr); // this is the name and the real email address to be used in function call
}
}
function customError($errno, $errstr, $file, $line)
{
error_log("Error: [$errno] $errstr in $file at line number: $line",1, "name#domain.com","From: name#domain.com.com");
die();
}
Here is the first thing I would try:
It would appear that your function searchRecipient isn't being passed a parameter. Rather than use the global keyword, I would define it in your function call. Also, mysql_fetch_array does not pass back an associative array, which is what you are using in your next step. I would change that to mysql_fetch_assoc (it's the same thing essentially). There are also a few other minor syntax corrections in this function. Here are my proposed changes to that function. I think this should fix your problem. Or at least get you moving forward.
function searchRecipient($MessageSentToMailbox) // function to search the database for the real email
{
$email_addr = mysql_query("SELECT email FROM tbl WHERE source='$MessageSentToMailbox'"); // temp store of the real email
$row = mysql_fetch_assoc($email_addr); //making temp store of data for use in program
if(empty($row['email']))
{
return FALSE;
}
else /* Else, return find the person's name and return both in an array */
{
$email_addr = $row['email'];
$results = mysql_query("SELECT * FROM tbl WHERE email = '$email_addr'"); // temp store of both queries from this function
$row = mysql_fetch_assoc($results); //making temp store of data for use in program
$name = $row['author']; // taking the author data and naming its variable
return array($name, $email_addr); // this is the name and the real email address to be used in function call
}
}
You could also combine this into one query and make it a little easier. Here is that solution.
function searchRecipient($MessageSentToMailbox)
{
$results = mysql_query("SELECT email, author FROM tbl WHERE source='$MessageSentToMailbox'");
$row = mysql_fetch_assoc($results);
if(empty($row['email']) || empty($row['author'])) return false;
return array($row['email'], $row['author']);
}