Codeigniter Send Separate Emails Simultaneously - php

Using Codeigniter 3, I have a web form that sends an email to the site admin once a form is submitted - this works as expected.
I am using email templates, once the form is submitted template 1 is sent (to the site admin). I would now like to simultaneously send template 2 (to the submitter's email address).
The emails will contain the same content apart from the email intro text, and subject - details below;
Email Template 1 - to admin;
'Hi, a new item has been requested on the site, ...'
Email Template 2 - to submitter;
'Hi, Here is the item you have requested on the site, ...'
My current code is as follows;
public function sendRequest() {
$this->load->library('email');
$from_email = $this->input->post('email');
$to_email = 'admin#example.com';
$subject = 'New Item Request - Admin Copy';
$name = $this->input->post('name');
$comment = $this->input->post('comment');
$this->email->from($from_email);
$this->email->to($to_email);
$this->email->subject($subject);
$data = array(
'name' => $name,
'from' => $from_email,
'comment' => $comment,
);
// send email template 1
$this->email->message($this->load->view('template/email/item_request_template', $data, true));
// send email template 2 to submitter - how?
// change $subject to 'New Item Request - User Copy';
if($this->email->send()) {
// send the $data to my email template
$data = array(
'item_request_name' => $this->input->post('name'),
'item_request_email' => $this->input->post('email'),
'item_request_comment' => $this->input->post('comment'),
);
}
}
Is there a more efficient way of doing this?

You simply have to repeat all the steps required for sending an email in the first place. Only difference is that for the second call you need to call, and reset all the required config options.
public function sendRequest() {
$this->load->library('email');
$from_email = $this->input->post('email');
$to_email = 'admin#example.com';
$subject = 'New Item Request - Admin Copy';
$name = $this->input->post('name');
$comment = $this->input->post('comment');
$this->email->from($from_email);
$this->email->to($to_email);
$this->email->subject($subject);
$data = array(
'name' => $name,
'from' => $from_email,
'comment' => $comment,
);
// send email template 1
$this->email->message($this->load->view('template/email/item_request_template', $data, true));
// send email template 2 to submitter - how?
// change $subject to 'New Item Request - User Copy';
if($this->email->send()) {
$this->email->clear(TRUE); // Pass TRUE as an argument if you are sending attachments
$this->email->from($from_email); // Update for second email
$this->email->to($to_email); // Update for second email
$this->email->subject($subject); // Update for second email
// send the $data to my email template
$data = array(
'item_request_name' => $this->input->post('name'),
'item_request_email' => $this->input->post('email'),
'item_request_comment' => $this->input->post('comment'),
);
$this->email->message($this->load->view('template/email/item_request_template_2', $data, true));
$this->email->send();
}
}

Related

How to Loop recipient in email Codeigniter

Have tried send email to multiple recipients but only 1 data is sent:
function reminder(){
$recipients= $this->user_model->view();
var_dump($recipients[0]->email);
$emaill = $recipients->email;
$recipientsmail= $emaill.',';
$email = $recipientsmail;
$judul = 'Test Email';
$deskripsi = 'TESt Email';
$config = [...]; //config for email is OK
$this->load->library('email', $config);
$this->email->from('tes');
$this->email->to($email);
$this->email->subject($judul);
$this->email->message($deskripsi);
$this->email->send();
return TRUE;
}
is something wrong in my code?
Please Help Me
That's the way I use to send multiple emails in codeigniter. Instead of put all the email directions in a variable ($email), use a foreach to loop the array and follow the details in the code:
function reminder(){
$recipients= $this->user_model->view();
var_dump($recipients[0]->email);
$judul = 'Test Email';
$deskripsi = 'This is a test';
$emailuser = 'user123#gmial.com';//for example
$nameuser = 'name of the user';
$config = [...]; //config for email is OK
$this->load->library("email");
foreach ($recipients as $value) {
$this->email->initialize($config);
$this->email->from($emailuser, $nameuser);
$this->email->to($value->email);
$this->email->subject($judul);
$this->email->message($deskripsi);
if($this->email->send()){
$this->session->set_flashdata("email_sent","Email sent successfully.");
}else{
$this->session->set_flashdata("email_sent","Error in sending Email.");
}
}
return TRUE;
}
And with this you could send more than one email. I hope it helps you.

How can I catch the information of a callback?

The code i am using to send messages works fine. I have configured the web-hooks correctly as the clickatell server is giving a 200 response which means my system received the callback but it is not catching the information of the callback. I am still new to this so What could i possibly be doing wrong.
clickatell library
CONTROLER--Send callback result via email(I am receiving the email but has no callback results)
public function callback(){
$this->load->library('clickatell_rest'); //Load Clickatell library
$this->clickatell_rest->parseStatusCallback(function ($result){
$callbackRunDate = date('Y-m-d H:i:s');
$from_email = 'admin#aaa.co.za';
$to = 'sebakets#gmail.com';
$message = $callbackRunDate.' '.print_r($result);
$subject = 'Callback Results';
$this->email->set_mailtype('html');
$this->email->from($from_email, 'Clickatell');
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
});
}
Clickatell Library function
public static function parseStatusCallback($callback, $file = STDIN){
$body = file_get_contents($file);
$body = json_decode($body, true);
$keys = [
'apiKey',
'messageId',
'requestId',
'clientMessageId',
'to',
'from',
'status',
'statusDescription',
'timestamp'
];
if (!array_diff($keys, array_keys($body))) {
$callback($body);
}
return;
}

Adding subject line for sending an email in codeigniter php

Applying for a job need to send an email with some of the details like name,phonenumber,email,current ctc etcc..Email is sending correctly but the problem is while sending an email in subject line i need to include name email ctc...for this i have done this but it is not accepting in this format.
$name = $this->input->post('fullname');
$email = $this->input->post('email');
$phone = $this->input->post('mobilenumber');
$currentemploymentstatus = $this->input->post('current_employment_status');
//set to_email id to which you want to receive mails
$to_email = 'yyyy#gmail.com';
$config=Array(
'protocol'=> 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com', //smtp host name
'smtp_port' => '465', //smtp port number
'smtp_user' => 'xxxxxx#gmail.com',
'smtp_pass' => 'PASSWORD123', //$from_email password
'mailtype' =>'html',
'newline' =>"\r\n",
'crlf' =>"\r\n",
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = array();
$message[] = 'Fullname : '.trim($name).' ';
$message[] = 'Email : '.trim($email).' ';
$message[] = 'Mobile : '.trim($phone).' ';
$message[] = 'Current Employment Status : '.trim($currentemploymentstatus).' ';
//$message = implode(PHP_EOL, $message);
$message = implode('<br>', $message);
//send mail
$this->load->library('email',$config);
$this->email->from($email);
$this->email->to($to_email);
//$list = array();
$this->email->subject($name|$email);
$this->email->message($message);
$this->email->set_newline("\r\n");
$this->email->set_mailtype("html");
if ($this->email->send())
{
$this->flash->success('Thank you for applying to this post we will get back to you soon!</div>');
redirect('apply');
}
else
{
$this->flash->success('There is error in sending mail! Please try again later');
redirect('apply');
}
}
Just Solved by adding this code.
$subject = $name .' | '.$currentemploymentstatus .' | '.$expectedctc .' | '.$primaryskills;
$this->email->subject($subject);
For your problem try changing "|" to .'|'
To clean up your code and to make your life easier:
If you are using CI, check out my repository on setting up an email_model to simplify the code you are using. Instead of loading the config stuff for the library all you will need to do is hit the model to send to email queue and hit the model a second time to process the queue
https://github.com/ddell003/Email_model

Email is not going to private domain

I am trying to send an email to all the staff members once particular action triggers. Everything works fine. However, I have noticed that email is not going to those who have our domain name email address. Other than that, I have tried to check that the one who use gmail, yahoo or hotmail receives the email. Most interesting thing is that, they can see the list of all email addresses including those who do not receive it. I am just wondering, what could be the reason for it ? Is there any restriction for sending any emails to private domains ? I do not reckon. I am using the codeigniter email library and following is my code.
$config = Array(
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = $this->load->view('request/template', $d, true);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('no-reply#austecservices.com.au','Austec Services Pty Ltd'); // change it to yours
$this->email->to('xxxxx#austecservices.com.au, xxxx#yahoo.com');// change it to
$this->email->bcc('xxxxx#gmail.com');
$this->email->subject($company[0]->title.": ".$this->userSession->given_name." ".$this->userSession->surname." raised an ad-hoc request for ".$site_name);
$this->email->message($message);
if(!$this->email->send()) {
show_error($this->email->print_debugger());
}
Any suggestion ?
please try this code:
$mail['fordetail'] = array(
'name'=> $name,
'email'=> $email,
'content'=>$content,
);
$txtEmail = $this->load->view('email',$mail,true);
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->from($this->config->item('team'));
$this->email->to($cusdata->email);
$this->email->subject($subject);
$this->email->message($txtEmail);
$this->email->send();

PHPMailer send more values

I am profun in PHP, so i need help with this one:
I added new variables to PHPmailer form, however How can i manage to send them to email?
this is the code I think I must modify:
$mail = new PHPMailer();
$mail->From = $conf['email_sent_from'];
$mail->FromName = $conf['email_sent_from_name'];
$mail->Subject = $conf['email_subject'];
$mail->WordWrap = 50; // some nice default value
$mail->Body = $values['message']; <-- this code
$mail->AddReplyTo( $values['email'] );
$mail->AddAddress( $conf['email_to'] );
Those are all values:
$values = array(
'webname' => $_POST['webname'],
'business' => $_POST['business'],
'name' => $_POST['name'],
'email' => $_POST['email'],
'phone' => $_POST['phone'],
'message' => $_POST['message'],
);
I want webname, business, name, email, phone, & message to be in the body. How can i do this?
AND is there a way of HTML customization? like adding < br >
cheap and dirty with minimum formatting
$mail->Body = implode('<br>',$values);
a little more formatting, make sure html email is used
$body="<table>";
foreach ($values as $k=>$v){
$body.='<tr><td>'.$k.'</td><td>'.$v.'</td></tr>';
}
$body.="</table>";
$mail->IsHTML(true); // its html mail baby
$mail->Body =$body;

Categories