CodeIgniter and phpmailer - codeigniter

I'm using CodeIgniter 3.0.3 and PHPMailer (https://github.com/ivantcholakov/codeigniter-phpmailer).
I have a problem with the configuration because it does not send e-mails.
This is my config file:
<?php defined('BASEPATH') OR exit('No direct script access allowed.');
$config['useragent'] = 'PHPMailer'; // Mail engine switcher: 'CodeIgniter' or 'PHPMailer'
$config['protocol'] = 'smtp'; // 'mail', 'sendmail', or 'smtp'
$config['mailpath'] = '/usr/sbin/sendmail';
$config['smtp_host'] = 'mail.skins4points.pl';
$config['smtp_user'] = 'noreply#skins4points.pl';
$config['smtp_pass'] = 'mypass';
$config['smtp_port'] = 587;
$config['smtp_timeout'] = 5; // (in seconds)
$config['smtp_crypto'] = ''; // '' or 'tls' or 'ssl'
$config['smtp_debug'] = 0; // PHPMailer's SMTP debug info level: 0 = off, 1 = commands, 2 = commands and data, 3 = as 2 plus connection status, 4 = low level data output.
$config['wordwrap'] = true;
$config['wrapchars'] = 76;
$config['mailtype'] = 'html'; // 'text' or 'html'
$config['charset'] = null; // 'UTF-8', 'ISO-8859-15', ...; NULL (preferable) means config_item('charset'), i.e. the character set of the site.
$config['validate'] = true;
$config['priority'] = 3; // 1, 2, 3, 4, 5; on PHPMailer useragent NULL is a possible option, it means that X-priority header is not set at all, see https://github.com/PHPMailer/PHPMailer/issues/449
$config['crlf'] = "\n"; // "\r\n" or "\n" or "\r"
$config['newline'] = "\n"; // "\r\n" or "\n" or "\r"
$config['bcc_batch_mode'] = false;
$config['bcc_batch_size'] = 200;
$config['encoding'] = '8bit'; // The body encoding. For CodeIgniter: '8bit' or '7bit'. For PHPMailer: '8bit', '7bit', 'binary', 'base64', or 'quoted-printable'.
I am using the email account you set up with hekko.pl
Here sample configuration mail client: http://www.pomoc.hekko.pl/content/28/53/pl/konfiguracja-poczty-w-mozilla-thunderbird.html

I'd guess that smtp_crypto needs to be tls if you're talking to port 587.

Related

codeigniter email displays HTML tags in email

I have a email sending code in codeigniter. I am able to send email but the HTML displays tags in the email. I have used mailtype as HTML. But it still does not work. This is my email function.
public function email_send($to_email,$subject,$message,$attach)
{
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'dsouzaj184#gmail.com';
$config['smtp_pass'] = 'abcd#1234';
$config['charset'] = 'UTF-8';
$config['smtp_crypto'] = 'ssl';
$config['priority'] = 1;
//$config['newline'] = "\r\n";
$config['mailtype'] = 'html'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$config['crlf'] = '\r\n';
$config['newline'] = '\r\n';
$this->load->library('email',$config);
$this->email->clear(true);
//$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_header('Content-Type', 'text/html');
//$this->email->set_newline('\r\n');
$this->email->from('dsouzaj184#gmail.com', 'Global Admin');
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
// return $to_email;
if($attach != '') {
$this->email->attach($attach);
}
if($this->email->send()) {
$this->email->clear(true);
return 1;
} else {
//print_r($this->email->print_debugger());
return 0;
}
}
Add wordwrap in config array as true
$config['wordwrap'] = TRUE;
and if it wont solve your problem than change
$this->load->library('email',$config);
$this->email->clear(true);
//$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_header('Content-Type', 'text/html');
//$this->email->set_newline('\r\n');
to
$this->load->library('email');
$this->email->initialize($config);
$this->email->set_newline('\r\n');
$this->email->clear();

SMTP Mail Not Send in Codeigniter

In Config.php File I have changes
$config['protocol']='smtp';
$config['smtp_hostname'] = "ssl://smtp.zoho.com";
$config['smtp_crypto'] = 'tls';
$config['smtp_username'] = "aaaa";
$config['smtp_password'] = "xxx";
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['newline'] = "\r\n";
But MAil Not Send.
enter image description here

Email not send using Codelgniter

I want to send an email to the user who is verified by the Admin. When the admin clicks on verify button in DB the status of a user is changed and email sends to a user, status can change but the email not send.
AdminController:
if($this->AdminModel->updateUser($id , $status)){
$user_message = "hello";
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'kshahroz699#gmail.com';
$config['smtp_pass'] = 'password';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE;
$this->email->initialize($config);
$this->email->from('kshahroz699#gmail.com', 'Model Hunt Inquiry');
$this->email->to($email);
$this->email->subject('Model Hunt Inquiry Form');
$this->email->message($user_message);
if($this->email->send()) {
echo " Successfully send";
}else{
echo "Not Send";
}
AdminModel:
function updateUser($id, $data){
$this->db->where('id',$id);
return $this->db->update('mh_users',$data);
}
Route:
$route['verifyUserEmail/(:any)/(:any)'] = 'Admin/verifyEmail/$1/$2';
Admin Model
function updateUser($id, $data){
$this->db->where('id',$id);
return $this->db->update('mh_users',$data);
}
My Controller
$this->load->library('email');
if($this->AdminModel->updateUser($id , $status)){
$from = 'no-reply#test.com';
$to = 'test#gmail.com';
$message1 = 'Hello Test';
$config = array();
$config['useragent'] = "CodeIgniter";
$config['mailpath'] = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
$config['protocol'] = "smtp";
$config['smtp_host'] = "localhost";
$config['smtp_port'] = "25";
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$this->email->from($from, 'Test');
$this->email->to($to);
$this->email->subject('Test');
$this->email->message($message1);
$this->email->set_mailtype('html');
if($this->email->send()){
$message=array("1","Mail Sent Successfully");
}else{
$message=array("0",$this->db->_error_message());
}
}
$this->session->set_flashdata('message', $message);

Send mpdf output as attachment email file with PHPMailer in Codeigniter

I have pdf file created with mpdf library and I want to send the output file as attachment in email with PHPMailer. I have tried this code but still not working. The attachment is missing but the email is successfully sent.
This is my code in controller :
public function pdf_file()
{
$this->load->library('m_pdf');
$this->data['title']="MY PDF TITLE 1.";
$this->data['description']="Test PDF File";
$html=$this->load->view('frontend/pdf_output',$this->data, true);
$pdfFilePath = "the_pdf_output.pdf";
$pdf = $this->m_pdf->load();
$pdf->SetProtection(array('copy'), '123456', '123456', 128);
$pdf->SetVisibility('screenonly');
$pdf->WriteHTML($html,2);
//$pdf->Output($pdfFilePath, "D");
$content = $pdf->Output('', 'S');
$content = chunk_split(base64_encode($content));
$subject = "PDF File";
$message = $this->load->view('frontend/pdf_output',$this->data,TRUE);
$from = "aaa#yahoo.com";
$to = 'xxx#gmail.com';
$cc = null;
$attachment = $content;
$sender = "Administrator";
$site_url = site_url();
if ($site_url == 'http://localhost/mysite/')
{
$this->load->view('frontend/email',$data);
}
else
{
$this->booking_model->send_email($subject,$message,$from,$to,$cc,$attachment,$sender);
}
}
This is my PHPMailer configuration :
function send_email($subject,$message,$from,$to,$cc,$attachment,$sender)
{
$this->load->library('PHPMailerAutoload');
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Host = "mail.host.com";
$mail->IsHTML(true);
$mail->Port = 26;
$mail->SMTPAuth = true;
//$mail->SMTPSecure = 'ssl';
$mail->Username = "aaa#domain.com";
$mail->Password = "123456";
$mail->setFrom($from, $sender);
if (count($cc) > 1)
{
for ($i=1; $i <= count($cc); $i++)
{
$mail->addCC($cc[$i], $sender);
}
}
if (!empty($attachment))
{
$mail->addAttachment($attachment);
}
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHTML($message);
//send the message, check for errors
if (!$mail->send()) {
// failed
} else {
// success
}
}
Anyone know to send the pdf attachment on email ? Please help.

Connect multiple database in controller or model Codeigniter

I want connect over database
because dbHost, dbUser, dbPass... is Variable should i want connect it in controller or model.
In controller file or model file, What should I write?
In config.php file set as follow:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = 'db1';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['db2']['hostname'] = 'localhost';
$db['db2']['username'] = '';
$db['db2']['password'] = '';
$db['db2']['database'] = 'db2';
$db['db2']['dbdriver'] = 'mysql';
$db['db2']['dbprefix'] = '';
$db['db2']['pconnect'] = TRUE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = '';
$db['db2']['char_set'] = 'utf8';
$db['db2']['dbcollat'] = 'utf8_general_ci';
$db['db2']['swap_pre'] = '';
$db['db2']['autoinit'] = TRUE;
$db['db2']['stricton'] = FALSE;
Or where you want to use directly do as follow:
$db2 = $this->load->database("database_name);
in active record:
$this->db2->select("*");
You can do this without adding to any file
1)We need to create DB Driver String from your given parameters
dbdriver://username:password#hostname/database
//Controller function
function connnectExternalDB()
{
$dbdriver = $this->input->get_post('dbdriver'); //mysqli or mysql
$username = $this->input->get_post('username'); // DB user name
$password = $this->input->get_post('password'); // DB password
$hostname = $this->input->get_post('hostname'); // DB host url
$database = $this->input->get_post('database'); // DB name
$dsn_string = "$dbdriver://$username:$password#$hostname/$database";
// Following will overwrite your current DB connection
$this->load->database($dsn_string, FALSE, TRUE); // 2nd parameter will return db object,3rd parameter will return active record enable for this db connection;
$this->load->model("example_model");
}
2) You can do the same without overwriting the current db connection like this
function connnectExternalDB()
{
$dbdriver = $this->input->get_post('dbdriver'); //mysqli or mysql
$username = $this->input->get_post('username'); // DB user name
$password = $this->input->get_post('password'); // DB password
$hostname = $this->input->get_post('hostname'); // DB host url
$database = $this->input->get_post('database'); // DB name
$dsn_string = "$dbdriver://$username:$password#$hostname/$database";
// Following will overwrite your current DB connection
$DB_OBJ = $this->load->database($dsn_string, TRUE, TRUE); // 2nd parameter will return db object,3rd parameter will return active record enable for this db connection;
$query = "select * from user";
$user_list = $DB_OBJ->query($query)->result_array();
}
It's in database.php in applications/config/ directory not config.php

Resources