How to Loop recipient in email Codeigniter - php

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.

Related

How to stop PHP Mailer from appending all email address on every email?

I've been trying many different ways to prevent PHP Mailer from appending every email address coming from my DB to every single email.
I feel it doesn't look right for every user to receive an email from my website and being able to see a few other thousand emails appended to that same email as well.
Here's my code, I already tried to many diferent things:
$query_select = "SELECT * FROM users";
$query = mysqli_query($connection, $query_select);
if($select_to == 'all'){
while($row = mysqli_fetch_assoc($query)) {
$user_email = $row['user_email'];
$send_to = $user_email;
$mail->setFrom('example#gmail.com', 'My website');
$mail->addAddress($send_to);
$mail->isHTML(true);
$mail->Subject = $subject_email;
$mail->Body = $content_email;
$mail->AltBody = $content_email;
}
} else {
if($select_to == 'single'){
$send_to = $to_single_email;
}
}
if($mail->send()){
$confirm = 'Sent.';
$send = array(
'confirm'=>$confirm
);
echo json_encode($send);
} else {
$confirm = 'There was en error while sending this email.';
$send = array(
'confirm'=>$confirm
);
echo json_encode($send);
}
$mail->send() will send to all addresses via a single message. Instead you need to send one message per recipient.
The while() loop would be refactored similar to:
while($row = mysqli_fetch_assoc($query)) {
$user_email = $row['user_email'];
$send_to = $user_email;
$mail->setFrom('example#gmail.com', 'My website');
$mail->addAddress($send_to);
$mail->isHTML(true);
$mail->Subject = $subject_email;
$mail->Body = $content_email;
$mail->AltBody = $content_email;
$mail->send();
$mail->clearAllRecipients();
}
The call to clearAllRecipients() will clear out the previous addresses that would otherwise accumulate via addAddress()
You'll also need to add the error checking within the loop for each call of send()

Passing data from one view to email template view on checkbox checked in code-igniter

I'm using a code-igniter to develop an web application. I'm trying to create a dynamic email template which loads data dynamically when I'm check a checkbox from other view.
Here is my scenario:
If I'm checking a checkbox for HMO_001, then all data that will loaded in the table for HMO_001 will pass to my email template, and when I'm click on Send button the mail will send to particular landlord.
Now I'm able to send an email to multiple landlords at a time on button click when I'm check multiple checkbox, but the email is hard coded. I'm trying to make it dynamic, but no success.
Here is my Code:
Controller to load table data:
public function index()
{
# code...
$user = $this->ion_auth->user()->row();
$data['username'] = $user->username;
$data['user_id'] = $user->id;
$landlord = $this->input->post('landlord_id');
$property = $this->input->post('property_id');
$certificate = $this->input->post('certificate_id');
if( $landlord =='') {
$landlord = 0;
}
if($property == ''){
$property = 0;
}
if($certificate == ''){
$certificate = 0;
}
$data['landlords'] = $this->c->landlordList();
//$data['properties'] = $this->c->propertyList();
$data['certificates'] = $this->c->certificateList();
//$data['certified'] = $this->c->certificate_List();
$data['certified'] = $this->c->getcertificateByLandlordProperty($landlord, $property, $certificate);
// print_r($data['landlords']);
// print_r($data['properties']);
// echo "<pre>";
// print_r($data['certificates']);
// echo "</pre>";
// exit();
$data['title'] = 'Certificate List';
$this->load->view('template/header', $data);
$this->load->view('certificate/certificate_list', $data);
$this->load->view('template/footer');
}
Controller to send mail:
public function sendMail()
{
# code...
//$data['certified'] = $this->c->getcertificateByLandlordProperty($landlord, $property, $certificate);
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['sendMail'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['sendMail'] as $selected){
//echo $selected."</br>";
}
}
}
$email_to = $this->input->post('sendMail[]');
<!-- echo "<pre>";
print_r($email_to);
echo "</pre>";
exit(); -->
$body = $this->load->view('emailTemplate/certTemplate', $data);
$this->load->library('email');
$config = array();
$config['protocol'] = 'mail';
$config['smtp_port'] = 587;
$config['smtp_host'] = 'smtp.office365.com';
$config['smtp_user'] = 'info#admin.com';
$config['smtp_pass'] = '**********';
$config['smtp_crypto'] = 'STARTTLS';
$config['newline'] = "\r\n"; //REQUIRED! Notice the double quotes!
$config['crlf'] = "\r\n";
$config['mailtype'] = "html";
$this->email->initialize($config);
$this->email->from('info#admin.com', 'Administrator');
$this->email->to($email_to);
$this->email->subject('Your Subject');
$this->email->message('$body');
$sent = $this->email->send();
if ($sent)
{
echo 'OK, mail send successfully';
} else {
echo $this->email->print_debugger();
}
}
Any kind of help is welcome, Thanks in advance.
To make a customized email message template, you could do like it this :
1. Move each recipient part to foreach loops that you've made prevously, this way you know each of email to recipient that is not being sent.
2. Add a third TRUE parameter on message $body (to return data as a string & not sending it to browser).
3. Add clear() method so each recipient only receive their customized message (to prevent showing all recipients on each recipient).
public function sendMail()
{
# code...
//$data['certified'] = $this->c->getcertificateByLandlordProperty($landlord, $property, $certificate);
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['sendMail'])){
$this->load->library('email');
$config = array();
$config['protocol'] = 'mail';
$config['smtp_port'] = 587;
$config['smtp_host'] = 'smtp.office365.com';
$config['smtp_user'] = 'info#admin.com';
$config['smtp_pass'] = '**********';
$config['smtp_crypto'] = 'STARTTLS';
$config['newline'] = "\r\n"; //REQUIRED! Notice the double quotes!
$config['crlf'] = "\r\n";
$config['mailtype'] = "html";
$this->email->initialize($config);
// Loop to store and display values of individual checked checkbox.
foreach($_POST['sendMail'] as $selected){
$data['email_to'] = $selected;
$body = $this->load->view('emailTemplate/certTemplate', $data, TRUE); // the TRUE parameter will return data as a string instead of sending it to browser (access recipient email on certTemplate with $email_to variable)
$this->email->clear();
$this->email->from('info#admin.com', 'Administrator');
$this->email->to($email_to);
$this->email->subject('Your Subject');
$this->email->message($body); // remove quotes from $body
$sent = $this->email->send();
if ($sent)
{
echo 'OK, mail send successfully';
} else {
echo $this->email->print_debugger();
}
}
}
}
}
Hope it helps..

Email sending error in codeigniter

I am working on a project that needs to send email.I use this as my controller
$from_email = $this->input->post('email');
$to_email = "info#test.com";
$name = $this->input->post('name');
$phno = $this->input->post('phno');
$from = $this->input->post('start');
$to = $this->input->post('end');
$message = $this->input->post('message');
$bodyContent="<table>
<tr><td>Name:</td><td>$name</td></tr>
<tr><td>Phno:</td><td>$phno</td></tr>
<tr><td>From Date:</td><td>$from</td><td>To Date:</td><td>$to</td></tr>
<tr><td>Message:</td><td>$message</td></tr>
</table>";
//Load email library
$this->load->library('email');
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject('Enquiry From Test TOURS');
$this->email->message($bodyContent);
//Send mail
if($this->email->send()) {
$this->session->set_flashdata("email_sent","Email sent successfully.");
} else {
$this->session->set_flashdata("email_sent","Error in sending Email.");
}
redirect('test_view/index');
And I am getting the output in the mail as like this
<table>
<tr><td>Name:</td><td>wer</td></tr>
<tr><td>Phno:</td><td>wer</td></tr>
<tr><td>From Date:</td><td>10/18/2017</td><td>To
Date:</td><td>10/25/2017</td></tr>
<tr><td>Message:</td><td>wer</td></tr>
</table>
I don't know why i am getting output in the mail like this , can anyone sortout this error please.
use mailtype configuration 'html' in Email configuration file
$config['mailtype'] = 'html';
from user guide
You need to set the type as html :
$this->email->set_mailtype("html");

Email not sending to one particular domian

We are using amazon server and I am using PHP, Codeigniter framework.
Below is the script which I am using for sending emails.
It is working when I send an email to Gmail domain email id (abc#gmail.com) but it is not working on some specific domain name email id (abc#xyzdmainname.com).
public function sendMail() {
$to = 'abc#xyzdomain.com';
$fromEmail = 'pqr#gmail.com';
$fromName = 'pqr';
$subject = 'Testing';
$message = 'Testing of email';
$newFile = ''
$this->CI->load->library('email');
$this->CI->email->mailtype = 'html';
$this->CI->email->from($fromEmail, $fromName);
$this->CI->email->to($to);
$this->CI->email->subject($subject);
$this->CI->email->message($message);
if (!empty($newFile)) {
$this->CI->email->attach($newFile);
}
$result = $this->CI->email->send();
$message = '';
if ($result) {
return 1;
} else {
echo $this->CI->email->print_debugger();
return 0;
};
Please let me help what-what case may be here for fail sending email on a particular domain.
and How I can overcome this issue.

PHP script: send-to another e-mail adres with SMTP

Currently I am using the following script. This script will send an email to email1, but not always.
When somebody adds the second email2, the email only goes to number 2.
I want to change the script to sent to both emails (1 and 2) when email2 exists.
Tried a lot of things, but can't get it right.
// Email
if(!$row->email2){
$email = $row->email1;
} else{
$email = $row->email2;
// Tried this below and a lot more
if(!$row->email2){
$email = $row->email1;
} else{
$email = $row->email1;
$email = $row->email2;
// E-mail to
if(!is_array($email)){
$mail->ClearAddresses();
$mail->AddAddress($email);
$mail->Send();
} else{
foreach($email as $email){
$mail->ClearAddresses();
$mail->AddAddress($email);
$mail->Send();
$emails = array();
if($email = $row->email1) {
$emails[] = $email;
}
if($email = $row->email2) {
$emails[] = $email;
}
foreach($email as $email){
$mail->ClearAddresses();
$mail->AddAddress($email);
$mail->Send();
}

Categories