Email Attachments Joining together - codeigniter

I am building a system in which you can send email at a desired date and time, there is an interface which captures this data and through a cron the email is sent.
This is all working as expected but I am having an issue with the attachments.
When the cron runs and sends the email it seems as if there is more than 1 email going out, the 2nd email adds the attachments of the previous email sent to itself, and this carries on. I have tried unsetting the value of the array which holds the file names but still no luck.
Email A -- (Has 3 Attachments / Sends 3 Attachments)
Email B -- (Has 2 Attachments / Sends 5 Attachments)
Email C -- (Has 2 Attachments / Sends 7 Attachments)
This is strange because this is my array output after each loop:
Successful
Array
(
[id] => 38
[company_id] => 225
[message_sender] => Favour Sanctuary
[message_subject] => Delayed Email Testing
[recipients] => xxxxxxxx#gmail.com
[message_body] => I hope you get this and the pictures
[message_type_id] =>
[time_sent] => 2019-05-08 14:17:27
[attach_file] => ["instagram8.png","networking.jpg"]
[date] => 2019-05-08
[time] => 13:19:00
[sent] => 0
)
Successful
Array
(
[id] => 39
[company_id] => 225
[message_sender] => Verdana State
[message_subject] => Attachment Mail
[recipients] => xxxxxxxx#gmail.com
[message_body] => This time you will get it
[message_type_id] =>
[time_sent] => 2019-05-08 14:33:16
[attach_file] => ["checkout.gif","kokoseller_new_logo.jpg","data_empty.png"]
[date] => 2019-05-08
[time] => 14:35:00
[sent] => 0
)
Successful
Array
(
[id] => 40
[company_id] => 225
[message_sender] => Kwame Eugene
[message_subject] => Memories of FFx
[recipients] => xxxxxxxx#gmail.com
[message_body] => Lets hope this goes well
[message_type_id] =>
[time_sent] => 2019-05-08 16:31:34
[attach_file] => ["evidence.pdf","kokoelec.jpg"]
[date] => 2019-05-08
[time] => 16:35:00
[sent] => 0
)
Here is my Cron Function:
function delayed_email(){
$check_date = date('Y-m-d');
$check_time = date('H:i:s');
$events = $this->db->query('SELECT * from record_mail where `date` IS NOT NULL and `time` IS NOT NULL and (`sent` = 0 OR `sent` IS NULL)')->result_array();
foreach($events as $e){
if($e['date']==$check_date && $e['time'] <= $check_time){
$company_id = $e['company_id'];
$subject = $e['message_subject'];
$message = $e['message_body'];
$sender = $e['message_sender'];
$recipients = $e['recipients'];
$attach_files = json_decode($e['attach_file'], TRUE);
$send = regular_email($recipients,$message,$subject,$attach_files,$company_id,$sender);
unset($attach_files);
$attach_files = array();
}
if ($send == TRUE){
echo 'Successful';
echo '<pre>';
print_r($e);
echo '</pre>';
//$params =array('sent'=>1);
//$this->db->where('id', $e['id']);
//$this->db->update('record_mail',$params);
}else{
echo 'failed';
}
}
}
Here is my send email Helper:
function regular_email($recipients, $message=NULL, $subject=NULL, $attach_files= NULL, $company_id=NULL, $sender=NULL){
$CI =& get_instance();
$CI->load->database();
$CI->load->library('email');
$CI->load->helper('url');
$to = $recipients ? $recipients : 'xxxxxx#gmail.com';
$subject = $subject ? $subject: 'Correspondence';
$body = $message;
$sender = $sender ?: company_name($company_id);
$CI->email->from('xxxxxxx#gmail.com' , $sender);
$CI->email->reply_to('xxxxxxx#gmail.com');
$CI->email->to($to);
$CI->email->subject($subject);
$CI->email->message($body);
if(is_array($attach_files)) {
foreach($attach_files as $k => $v){
$attach = base_url('uploads/email_attach_file/') . '/' . $v;
$CI->email->attach($attach);
}
} else {
//ATTACH ONE
$attach = base_url('uploads/email_attach_file/') . '/' . $attach_files;
$CI->email->attach($attach);
}
$result = $CI->email->send();
if($result){
return TRUE;
} else {
return FALSE;
};
/*if(!$CI->email->send()){
return $CI->email->print_debugger();
} */
}

Since you tagged this with PHPMailer, I assume that PHPMailer is being used behind the scenes. When you call $CI->email->attach($attach);, it retains the list of files you've attached, i.e. it always adds attachments, it doesn't set them. Notice that the number of attachments always increases by the number of files you've added each time, so the first one gets 3, you add 2, making 5, then add another 2, making 7. It's not your input arrays that are at fault, it's the internal list of attachments held within your mail instance ($CI->email).
If PHPMailer is inside there, you need to call the clearAttachments() method to clear them - I'd assume that CI has an equivalent or a wrapper for it - refer to their docs.

Related

With \Mail::send can not set title

In Laravel 6 I send email with \Mail::send (DataGrip in .env) and I recieve the email, can not set title.
is is the same as content of the email.
I do as :
$data = array ( // That is not applied
'title' => 'Title 234',
'bodyMessage' => $message_text,
'sender_username' => $name,
'sender_email' => $email,
'site_name' => $site_name,
);
\Mail::send ( 'emails/contact_us', $data, function ($message) use($data, $donotreply_email, $contact_us_email) {
$message->from ( $donotreply_email, 'Support of ' . $data['site_name'] );
$message->to ( $contact_us_email )->subject ( $data['bodyMessage'] );
// $message->title('Message 34 Title'); // If to uncomment it it does not work too.
} );
Which is valid way ?
Thanks!
If you want to set a mail title it means mail subject. So set subject instead of title.
$message->subject('Message 34 Title');

Laravel later method fires emails randomly

I am using laravel admin, and I am trying to send emails at a specific time. I pass this datetime data into the first argument of later method
"date" => Carbon #1576722840 {#543 ▼
date: 2019-12-19 02:34:00.0 UTC (+00:00) }
However, it doesnt work as I expected. it randomly sends emails.
$form->saved(function (Form $form) {
if ($form->model()->to === '4') {
$emails = EmailAddress::all();
} else {
$emails = EmailAddress::where('user_type', $form->model()->to)->get();
}
$data = ['id' => $form->model()->id, 'title' => $form->model()->title, 'from' => $form->model()->from, 'body' => $form->model()->body, 'date' => $form->model()->schedule_date, 'is_html' => $form->model()->is_html];
foreach ($emails as $email) {
$url = URL::signedRoute('unsubscribe',['email_address_id' => $email->id]);
Mail::to($email)->later($data['date'],new MagazineMail($data,$url));
}
});
return $form;
}
As I understood it, if I want to send a mail at a specific time, I should just pass a datetime into the first argement, so I have no idea why my code does not work.
I made sure my env file is correct and set QUEUE_CONNECTION=database.

paypal order total with laravel

Anybody using the PayPal API from srmklive with there laravel project?
https://github.com/srmklive/laravel-paypal
Have installed it and I have made it work, till I got the following error.
I have been searching for hours, but can't find the solution.
What would be the problem here?
It keeps saying the following; Order total is invalid.
Array ( [TIMESTAMP] => 2018-06-27T08:06:32Z [CORRELATIONID] => c8e16efc9b6e4 [ACK] => Failure [VERSION] => 123 [BUILD] => 47483429 [L_ERRORCODE0] => 10401 [L_ERRORCODE1] => 10426 [L_ERRORCODE2] => 10431 [L_SHORTMESSAGE0] => Transaction refused because of an invalid argument. See additional error messages for details. [L_SHORTMESSAGE1] => Transaction refused because of an invalid argument. See additional error messages for details. [L_SHORTMESSAGE2] => Transaction refused because of an invalid argument. See additional error messages for details. [L_LONGMESSAGE0] => Order total is invalid. [L_LONGMESSAGE1] => Item total is invalid. [L_LONGMESSAGE2] => Item amount is invalid. [L_SEVERITYCODE0] => Error [L_SEVERITYCODE1] => Error [L_SEVERITYCODE2] => Error [paypal_link] => )
I don't see anything about the order total in the documentation?
Or am I missing something?
The code I am using right now;
`$provider = new ExpressCheckout;
$data = [];
$order_id = 1;
$data['items'] = [
[
'name' => 'Product 1',
'price' => 9.99,
'qty' => 1,
],
];
$data['return_url'] = url('/paypal/ec-checkout-success');
$data['invoice_id'] = config('paypal.invoice_prefix').'_'.$order_id;
$data['invoice_description'] = "Order #$order_id Invoice";
$data['cancel_url'] = url('/');
$total = 0;
foreach ($data['items'] as $item) {
$total += $item['price'] * $item['qty'];
}
$data['total'] = $total;
//return print_r($data);
$response = $provider->setExpressCheckout($data);
//return dd($respons);
// This will redirect user to PayPal
return print_r($response); `
Thank you in advance.
I found the solution to the above problem.
Somehow PayPal doesn't accept the 9.99 USD
When I make a round number like 10 USD it works.

codeignite trying to get property of non-object not resolved

I am attempting to access the result set from a model query in the view. I have the following:
Controller:
$courseId = $this->session->userdata('courseId');
//echo "Course: ".$courseId;
if(isset($courseId) && $courseId != '')
{
$result = $this->Course_model->loadBasicDetailsEdit($courseId);
$data['basicCourseDetails'] = $result;
$this->load->view('course/basicDetails', $data);
}
Model:
function loadBasicDetailsEdit($courseId)
{
$this->db->select('*');
$this->db->where('course_id', $courseId);
$this->db->from('course');
$query = $this->db->get();
if ( $query->num_rows() > 0 )
{
return $query->result();
} else {
return FALSE;
}
}
and in the view I tried to print_r() and got this:
Array ( [0] => stdClass Object ( [course_id] => 8 [title] => Photography [summary] => [description] => [price] => [member_id] => 12 [category] => [audience] => [goals] => [date] => 2013-09-26 [production] => 0 ) )
I tried to access this using $basicCourseDetails->title or $basicCourseDetails['title']
but neither are working. Any hint as to why this is happening?
Regards,
try this:
foreach($basicCourseDetails as $basic){
echo($basic->title);
}
or something like this:
echo($basicCourseDetails[0]->title);
This is an array of objects
Array ( [0] => stdClass Object ( [course_id] => 8 [title] => Photography [summary] => [description] => [price] => [member_id] => 12 [category] => [audience] => [goals] => [date] => 2013-09-26 [production] => 0 ) )
Contains one stdObject in the array, so, first objects is 0, if there were more, then second item could have index 1 and so on. To retrieve data from the first (here is only one) stdobject you may use
echo $basicCourseDetails[0]->title; // title will be printed
You can send data to the view page by this line of code which is mentioned in above question.
$result = $this->Course_model->loadBasicDetailsEdit($courseId);
$data['basicCourseDetails'] = $result;
$this->load->view('course/basicDetails', $data);
But when you will access these data in view then you need to access all data one by one by using foreachloop in the view page.
For example if you have a view page like basic_details.php inside course folder then you need to write code like this to access these data.
foreach ($basicCourseDetails as $key => $value) {
$name = $value->name;
}
The above foreachloop can be written in view page where you want to access data.

CI-Merchant with Sagepay

Has anyone had any luck using SagePay Direct with CI-Merchant? So far the only response I am getting is:
Merchant_response Object
(
[_status:protected] => failed
[_message:protected] =>
[_reference:protected] =>
[_data:protected] =>
[_redirect_url:protected] =>
[_redirect_method:protected] => GET
[_redirect_message:protected] =>
[_redirect_data:protected] =>
)
I haven't used SagePay before and need to get this working urgently. I have added my IP address to the SagePay testing area but am not testing on https as am just on my localhost at the moment.
My code looks like:
public function process_payment()
{
$settings = array (
'vendor' => 'XXX',
'test_mode' => TRUE,
'simulator_mode' => FALSE,
);
$this->merchant->initialize($settings);
//get customer details
$this->load->library('custom_cart');
$customer = $this->custom_cart->get_customer_invoice_info();
$customer_name = '';
if ( ! empty($customer['title'])) $customer_name .= $customer['title'] .' ';
$customer_name .= $customer['forename'];
$customer_name .= $customer['surname'];
$customer_street = $customer['address1'];
$customer_street2 = $customer['address2'];
if ( ! empty($customer['address3'])) $customer_street2 .= $customer['address3'];
//order details
$amt = $this->custom_cart->order_total();
$get_curr = $this->custom_cart->get_currency();
$currencycode = $get_curr['name'];
$shippingamt = $this->custom_cart->shipping_cost();
$itemamt = $this->custom_cart->order_subtotal();
$taxamt = '0';
$invnum = $this->custom_cart->customer_order_no();
$params = array(
'description'=> 'Online order',
'currency'=> $currencycode,
'transaction_id'=> $invnum,
'email'=> $customer['email_address'],
'first_name'=> $customer['forename'],
'last_name'=> $customer['surname'],
'address1'=> $customer_street,
'address2'=> $customer_street2,
'city'=> $customer['town_city'],
'postcode'=> $customer['postcode'],
'country'=> $customer['country'],
'region'=> $customer['county'],
'phone'=> $customer['phone'],
'Amount'=> $amt,
'card_no'=> $this->input->post('creditcard'),
'name'=> $customer_name,
'card_type' => $this->input->post('creditcardtype'),
'exp_month'=> $this->input->post('cardmonth'),
'exp_year' => $this->input->post('cardyear'),
'csc'=> $this->input->post('cardsecurecode')
);
$response = $this->merchant->purchase($params);
echo '<pre>';
print_r($response);
}
(all the values for the params are valid and I have checked the correct values are passed to them. Vendor name is masked here)
Finally tracked down the problem - I was passing through two digits for the year and this is where it was failing (I forgot on my previous merchant I was adding in the 20 prefix)! Thank you for your help. (If anyone has a similar problem I tracked it down by echoing a response from each function in the function trail)

Resources