i don't understand how send one email for all users, i do it this in my controller :
// Init
$data = $this->request->data['Email'];
$d = array(
'subject' => $data['subject'],
'message' => $data['message']
);
// QUERY
$all = $this->Spoutnik->find('all', array(
'conditions' => array(
'Spoutnik.role >=' => '1'
),
'fields' => array('Spoutnik.email')
));
$this->set(compact('all'));
// list
$bcc = '';
foreach ($all as $user) {
$bcc .= $user['Spoutnik']['email'].',';
}
// MAIL
App::uses('CakeEmail', 'Network/Email');
$CakeEmail = new CakeEmail('default');
$website_short_name = Configure::read('website.short_name');
$CakeEmail->bcc("$bcc");
$CakeEmail->subject(''.$website_short_name.' :: '.$d['subject'].'');
$CakeEmail->viewVars(array(
'message' => (''.$d['message'].'')
));
$CakeEmail->emailFormat('html');
$CakeEmail->template('message_direct');
// final
$CakeEmail->send();
But i have error "no valid mail" , and after the liste of user's mail
what is wrong in my code ?
Couple of things I've noticed at a quick glance...
foreach ($all as $user) {
$bcc .= $user['Spoutnik']['email'].',';
}
In that code, you're adding a comma after every email, so at the end of your string you'll have a comma. Try this:
$e = 0;
foreach ($all as $user) {
if($e > 0) $bcc .= ',';
$bcc .= $user['Spoutnik']['email'];
$e++;
}
--edit-- good point Deepak, Cake's documentation suggests you give BCC an array. It's easier and more efficient to produce so do that.
Second, $CakeEmail->bcc("$bcc"); doesn't need the quotes. It should work fine with them, but I've seen Cake do some pretty weird things... Try taking them out:
$CakeEmail->bcc($bcc);
Third, you're setting all those emails to BCC which is fine, but I can't see a to address. If you want to send out to a lot of email address without them seeing each other, you still need to send the email to somewhere, even if its noreply#yourdomain.com. Add a to address in before you send:
$CakeEmail->to('noreply#yourdomain.com');
I will just use the addBcc function of CakeEmail and modify the loop:
App::uses('CakeEmail', 'Network/Email');
$CakeEmail = new CakeEmail('default');
// list
foreach ($all as $user) {
$CakeEmail->addBcc($user['Spoutnik']['email']);
}
$website_short_name = Configure::read('website.short_name');
$CakeEmail->subject(''.$website_short_name.' :: '.$d['subject'].'');
$CakeEmail->viewVars(array(
'message' => (''.$d['message'].'')
));
Try changing your $bcc block to this:
// list
$bcc = array();
foreach ($all as $user) {
$bcc[]= $user['Spoutnik']['email'];
}
Also refer to CakeEmail Documentation
Related
I'm trying to send messages to multiple recipients with different message body but I don't know if my script has any error. The problem is when I execute this below code, only one user email will receive message. If I try it again another one will get a message. I want all the emails in the array to receive the message with individual message body. And also I noticed that my script takes long to complete execution. Is there a better way to get this working as expected?
PHP
<?php
$conn_handler->prepare('
SELECT * FROM food_orders fo
INNER JOIN our_chefs oc
ON oc.chef_private_key = fo.order_chefpkey
WHERE fo.order_id = :currentorder AND fo.order_userid = :order_userid
ORDER BY fo.order_chefpkey
');
$conn_handler->bind(':currentorder', $lastOrderId);
$conn_handler->bind(':order_userid', $buyerid);
$conn_handler->execute();
$getFoodOrders = $conn_handler->getAll();
if( !isset($_SESSION['completed_'.$lastOrderId]) ) {
$creatProducts = array();
$email_list = array();
/*Here i loop on current orders*/
foreach($getFoodOrders as $row) {
//Create an array of chef emails
$email_list[$row->chef_private_key] = $row->chef_email;
//Create an array of items based on chef private key
$creatProducts[$row->order_chefpkey][] = array(
'o_name' => $row->order_foodname,
'o_pid' => $row->oder_foodid,
'o_price' => $row->order_price,
'o_currency' => $row->currency,
'o_qty' => $row->order_qty,
'o_size' => $row->order_size,
'o_img' => $row->order_image,
);
}
//Here i loop through the above chef emails
foreach($email_list as $key => $val) {
$productBuilder = null;
//Here i create html for products based on chef keys
foreach($creatProducts[$key] as $erow) {
$productBuilder .= '<div><b>Product Name:</b> '.$erow['o_name'].'<br/></div>';
}
//Here i send email to each chef with their individual products created above
$sourcejail->sendMail(
$val, //Send TO
null, //Send Bcc
null, //Send CC
null, //reply To
1, //Something
'Your have received new order ('.$lastOrderId.')', //Subject
$productBuilder //Message body
);
}
$_SESSION['completed_'.$lastOrderId] = true;
}
in there i have been create function for send an email, here my code like this :
$row=DB::table('orders')
->join("cms_members","cms_members.id","=","orders.id_cms_members")
->select("orders.*","cms_members.email as email","cms_members.name as name","cms_members.phone as phone")
->where('orders.id',$id_order)
->first();
$datas = array(
'name' => 'test',
'detail'=> 'test',
'sender' => 'adamprojo#gmail.com'
);
$customPaper = array(0,0,800,800);
$pdf = PDF::loadView('testing', $data)->setPaper($customPaper);
Mail::send('emails.temp', $datas, function($message) use($pdf)
{
$message->from("no-reply#crocodic.net","Invoice HOP Daily rent Orders");
$message->to($row->email)->subject('Invoice');
$message->attachData($pdf->output(), "invoice.pdf");
});
here my problem when ever i want to send an email i get response
Address in mailbox given does not comply with RFC 2822, 3.6.2.
but if i try to print the email its give me the email address. what should i do ?
Kindly, can someone give me any solution for mentioned problem?
This will be your code:
$row=DB::table('orders')
->join("cms_members","cms_members.id","=","orders.id_cms_members")
->select("orders.*","cms_members.email as email","cms_members.name as name","cms_members.phone as phone")
->where('orders.id',$id_order)
->first();
$datas = array(
'name' => 'test',
'detail'=> 'test',
'sender' => 'adamprojo#gmail.com'
);
$customPaper = array(0,0,800,800);
$pdf = PDF::loadView('testing', $data)->setPaper($customPaper);
Mail::send('emails.temp', $datas, function($message) use($pdf, $row)
{
$message->from("no-reply#crocodic.net","Invoice HOP Daily rent Orders");
$message->to($row->email)->subject('Invoice');
$message->attachData($pdf->output(), "invoice.pdf");
});
You need to set use($pdf, $row) by the Mail::send(), this will get the $row data that you use in the $message->to().
Hope this works!
I am having some trouble implementing the pushwoosh class http://astutech.github.io/PushWooshPHPLibrary/index.html. I have everything set up but i am getting an error with the array from the class.
This is the code i provied to the class:
<?php
require '../core/init.php';
//get values from the clientside
$sendID = $_POST['sendID'];
$memID = $_POST['memID'];
//get sender name
$qry = $users->userdata($memID);
$sendName = $qry['name'];
//get receiving token
$qry2 = $users->getTokenForPush($sendID);
$deviceToken = $qry2['token'];
//i have testet that $deviceToken actually returns the $deviceToken so thats not the problem
//this is the array that the php class requires.
$pushArray = array(
'content' => 'New message from ' . $sendName,
'devices' => $deviceToken,
);
$push->createMessage($pushArray, 'now', null);
?>
And this is the actually code for the createMessage() method
public function createMessage(array $pushes, $sendDate = 'now', $link = null,
$ios_badges = 1)
{
// Get the config settings
$config = $this->config;
// Store the message data
$data = array(
'application' => $config['application'],
'username' => $config['username'],
'password' => $config['password']
);
// Loop through each push and add them to the notifications array
foreach ($pushes as $push) {
$pushData = array(
'send_date' => $sendDate,
'content' => $push['content'],
'ios_badges' => $ios_badges
);
// If a list of devices is specified, add that to the push data
if (array_key_exists('devices', $push)) {
$pushData['devices'] = $push['devices'];
}
// If a link is specified, add that to the push data
if ($link) {
$pushData['link'] = $link;
}
$data['notifications'][] = $pushData;
}
// Send the message
$response = $this->pwCall('createMessage', $data);
// Return a value
return $response;
}
}
Is there a bright mind out there that can tell me whats wrong?
If I understand, your are trying to if your are currently reading the devices sub-array. You should try this:
foreach ($pushes as $key => $push) {
...
// If a list of devices is specified, add that to the push data
if ($key == 'devices') {
$pushData['devices'] = $push['devices'];
}
You iterate over $pushes, which is array('content' => ..., 'devices' => ...). You will first have $key = content, the $key = 'devices'.
It looks like the createMessage function expects an array of messages, but you are passing in one message directly. Try this instead:
$push->createMessage(array($pushArray), 'now', null);
The class is expecting an array of arrays; you are just providing an array.
You could do something like this
//this is the array that the php class requires.
$pushArrayData = array(
'content' => 'New message from ' . $sendName,
'devices' => $deviceToken,
);
$pushArray[] = $pushArrayData
Will you ever want to handle multiple messages? It makes a difference in how I would do it.
I have tried send email to multiple recipients. I input the recipients into an array, like this:
$recipients = array(
'email1#host.com',
'email2#host.com',
'email3#host.com'
);
And it works nice.
Now I need to get the email address form database, and when I run it, it shows error:
A PHP Error was encountered
Severity: Warning
Message: preg_match() expects parameter 2 to be string, array given
Filename: libraries/Email.php
Line Number: 795
I tried to see the array from the database, and it looks like this:
Array ( [0] => Array ( [email] => vikki2#afteroffice.com ) [1] => Array ( [email] => nanapuspita05#hotmail.com ) )
Here are the function to send email:
function send_bulk_mail() {
$from = 'email#email.com';
$to = 'email1#email.com';
$search_by = $this->input->post('search_by');
$search_field = $this->input->post('search_field');
$recipients = $this->company_model->get_email($search_by, $search_field);;
$subject = $this->input->post('subject');
$message = $this->input->post('message');
$this->load->library('email');
$this->email->from($from);
$this->email->to($to);
$this->email->bcc($recipients);
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
redirect(base_url('index.php/company'));
}
Is there any solution to change the array? Or for change the rules in libraries/email.php
This is the function in libraries/email.php:
public function clean_email($email)
{
if ( ! is_array($email))
{
if (preg_match('/\<(.*)\>/', $email, $match))
{
return $match['1'];
}
else
{
return $email;
}
}
$clean_email = array();
foreach ($email as $addy)
{
if (preg_match( '/\<(.*)\>/', $addy, $match))
{
$clean_email[] = $match['1'];
}
else
{
$clean_email[] = $addy;
}
}
return $clean_email;
}
Thank you...
The $recipients you retrived from DB is an array with each element is an array, while the $recipients you described below is an array with each element is a string. Try to make an array with each element is a string when you retrive from DB like this :
$recipient_array = $this->company_model->get_email($search_by, $search_field);
$recipients = array();
foreach($recipient_array as $key => $value) {
$recipients[] = $value['email'];
}
preg_match expects two strings and then an array. (http://php.net/manual/en/function.preg-match.php). The error seems to indicate that it's not getting a properly formatted array as the second parameter.
Based on the proper formatting of an array (http://php.net/manual/en/function.array.php) compared to your array output it seems as if you are giving it an array of arrays that enumerated with the 'email' index. (much like the 'fruits' example in the provided link).
here is the problem, couldn't find much googling, hope somebody here got the answer for this.
My PHP file is sending emails as feedback to me, and it takes 5 arguments,
whenever I send long arguments to my PHP file, it trims the end of argument 5, which is the longest one, how can I fix that?
To be more clear argument 5 is pretty much the email body.
Here is the PHP code:
<?php
include('Mail.php');
$arg1 = $argv[1]; //appName and version
$arg2 = $argv[2]; //ErrorMessage
$arg3 = $argv[3]; //ErrorData
$arg4 = $argv[4]; //ErrorSource
$arg5 = $argv[5]; //ErrorStackTrace
$arg1 = $_GET['arg1'];
$arg2 = $_GET['arg2'];
$arg3 = $_GET['arg3'];
$arg4 = $_GET['arg4'];
$arg5 = $_GET['arg5'];
$subject = $arg1 ;
$errorMessage = $arg2;
$ErrorData = $arg3;
$ErrorSource = $arg4;
$ErrorStackTrace = $arg5;
$recipients = "myemail";
$from = "errorreport#user.com" ;
$headers = array (
'From' => $from,
'To' => $recipients,
'Subject' => $subject,
);
$body = "ErrorMessage: "."\n".$errorMessage."\n"."ErrorData: "."\n".$ErrorData."\n"."ErrorSource: "."\n".$ErrorSource."\n"."ErrorStackTrace: "."\n".$ErrorStackTrace;
$mail_object =& Mail::factory('smtp',
array(
'host' => 'prwebmail',
'auth' => true,
'username' => 'user',
'password' => 'pass', ));
$mail_object->send($recipients, $headers, $body);
?>
I can see your code is referencing $_GET variables, please change your form to use a method of POST
If you are able to, shy away from GET method forms when sending large data.
find:
<form method="get">
replace with:
<form method="post">
then change:
$arg1 = $_GET['arg1'];
to:
$arg1 = $_POST['arg1'];
etc...