Send email to create and attach PDF in CodeIgniter - codeigniter

The output email gets sent but does not arrive i my email. I have added dompdf library. When I removed the code that creates the pdf then the mail was sent.
My code:
<?php
$this->load->library('dompdf_gen');
$this->dompdf->load_html($body);
$this->dompdf->render();
$output = $this->dompdf->output(APPPATH . 'Brochure.pdf', 'F');
$email = $this->input->post('email');
$subject = "some text";
$message = $body;
$this->sendEmail($email, $subject, $message);
$config = Array(
'mailtype' => 'html'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('ul#ul.com');
$this->email->to($email);
$this->email->subject($subject);
$this->email->message($message);
$this->email->attach(APPPATH . 'Brochure.pdf');
if ($this->email->send())
{
echo 'Email send.';
}
else
{
show_error($this->email->print_debugger());
}

You must be save attachment as a file, so use file_put_contents to save pdf to a file. Set permission to 777 to accessible by public:
$this->load->library('dompdf_gen');
$this->dompdf->load_html($body);
$this->dompdf->render();
$output = $dompdf->output();
file_put_contents(APPPATH.'Brochure.pdf', $output);
chmod(APPPATH.'Brochure.pdf', 777);
$email=$this->input->post('email');
$subject="some text";
$message=$body;
$this->sendEmail($email,$subject,$message);
$config = Array(
'mailtype' => 'html' );
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('ul#ul.com');
$this->email->to($email);
$this->email->subject($subject);
$this->email->message($message);
$this->email->attach(APPPATH.'Brochure.pdf');
if($this->email->send())
{
echo 'Email send.';
}
else
{
show_error($this->email->print_debugger());
}

Use this library to convert html to pdf which I have modified for CI3.
https://github.com/shyamshingadiya/HTML2PDF-CI3
Please check below mentioned solution
//Load the library
$this->load->library('html2pdf');
//Set folder to save PDF to
$this->html2pdf->folder('./uploads/pdfs/');
//Set the filename to save/download as
$this->html2pdf->filename('new.pdf');
//Set the paper defaults
$this->html2pdf->paper('a4', 'portrait');
for Sending this particular file in email.
$this->load->library('email');
$this->email->from('your#example.com', 'Your Name');
$this->email->to('someone#example.com');
$this->email->subject('Email PDF Test');
$this->email->message('Testing the email a freshly created PDF');
$this->email->attach($path_to_pdf_file);
$this->email->send();
Let me know if it not work.

Related

This is my codiegniter contact us form code , it's correct , but i want include mail functionality in this contact us form,

this code is working perfect for saving data into databse , but i just want to include mail functionality in this code.
Please take a look.
I have tried a lot, but in vain. Any help would be nice.
<?php
class contactform extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation'));
$this->load->database();
}
function index()
{
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required');
$this->form_validation->set_rules('companyname', 'Companyname', 'trim|required');
//$this->form_validation->set_rules('industryname', 'Industryname', 'trim|required');
$this->form_validation->set_rules('country', 'Country', 'trim|required');
$this->form_validation->set_rules('message', 'Message', 'trim|required');
//run validation on post data
if ($this->form_validation->run() == FALSE)
{ //validation fails
$this->load->view('contact_form_view');
}
else
{
//insert the contact form data into database
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'subject' => $this->input->post('subject'),
'companyname' => $this->input->post('companyname'),
//'industryname' => $this->input->post('industryname'),
'country' => $this->input->post('country'),
'message' => $this->input->post('message')
);
if ($this->db->insert('contacts', $data))
{
// i just want a mailer
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">We received your message! Will get back to you shortly!!!</div>');
redirect('contactform/index');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Some Error. Please try again later!!!</div>');
redirect('contactform/index');
}
}
}
//custom callback to accept only alphabets and space input
function alpha_space_only($str)
{
if (!preg_match("/^[a-zA-Z ]+$/",$str))
{
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
this code is working perfect , to save data in to database. Thanks in advance.
My mailer attempt is this
<?php
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'user#gmail.com'; // email id
$config['smtp_pass'] = 'password'; // email password
$config['mailtype'] = 'html';
$config['wordwrap'] = TRUE;
$config['charset'] = 'iso-8859-1';
$config['newline'] = "\r\n"; //use double quotes here
$this->email->initialize($config);
?>
<?php
//get the form data
$name = $this->input->post('name');
$from_email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
//set to_email id to which you want to receive mails
$to_email = 'user#gmail.com';
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send())
{
// mail sent
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">successfully!</div>');
redirect('contactform/index');
}
else
{
//error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center"> error </div>');
redirect('contactform/index');
}
?>
I am assuming that you want to send mail from localhost.To do this you can use PHPMailer library
$this->load->library('PHPMailer/PHPMailer');
//require("class.phpmailer.php");
$mail=new PHPMailer(); //creat PHPMailer object
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; //needs login information
$mail->SMTPSecure = "tls"; //specifies tls security
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; //gmail smtp port
/******************* Set Your Credentials********************/
$mail->Username ="user#gmail.com" ; //SMTP gmail address
$mail->Password = "password"; // SMTP account password
$mail->From = "fromxyz"; //sender's gmail address
$mail->isHTML(true);
$mail->FromName = "From name";
$mail->AddAddress($email);//receiver's e-mail address
$mail->Subject = "Email Subject";//e-mail subject
$mail->Body ="Email Message";//e-mail message
$mail->WordWrap = 50;
if(!$mail->send())
{
echo "error";
}
else
{
echo "successs";
}
If this also not work try making some changes in gmail account:
Login to gmail.
Go to My Account Section.
Then Go To Sign-in & Security > Connected apps & sites
In this section make Allow less secure apps: ON
in you Controller try this type
//run validation on post data
if ($this->form_validation->run() == FALSE)
{ //validation fails
$this->load->view('contact_form_view');
}
else
{
//insert the contact form data into database
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'subject' => $this->input->post('subject'),
'companyname' => $this->input->post('companyname'),
//'industryname' => $this->input->post('industryname'),
'country' => $this->input->post('country'),
'message' => $this->input->post('message')
);
//load email libary
$this->load->library('email');
//configure email settings
$config['protocol'] = 'smtp`';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'user#gmail.com'; // email id
$config['smtp_pass'] = 'password'; // email password
$config['mailtype'] = 'html';
$config['wordwrap'] = TRUE;
$config['charset'] = 'iso-8859-1';
$config['newline'] = "\r\n"; //use double quotes here
$this->email->initialize($config);
//get the form data
$name = $this->input->post('name');
$from_email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
//set to_email id to which you want to receive mails
$to_email = 'user#gmail.com';
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send())
{
// mail sent
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">successfully!</div>');
}
else
{
//error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center"> error </div>');
}
}

Email Attachment not working in codeigniter

Mail function with attachment file is not working in codeigniter, i m not receiving any mail with attachment.. before i received the mail without attachment but after i used "$_SERVER["DOCUMENT_ROOT"]."/admin/assets/image/order_complete_file/".$image_name;", i didnt receive any mail.. kindly help me.. Here my coding is
$image_name = "some_name";
$content = "some content";
$subject = "Subject Name";
$this->load->helper('email');
$this->load->library('email');
$this->email->set_newline("\r\n");
$this->email->from('abc#gmail.com');
$this->email->to('xxx#gmail.com');
$this->email->subject($subject);
$this->email->message($content);
$attched_file = $_SERVER["DOCUMENT_ROOT"]."/admin/assets/image/order_complete_file/".$image_name;
$this->email->attach($attched_file);
$this->email->send();
You should use config parameter for email library, and set 'mailtype' to 'html'(Default Value is 'text'). If it still fails, you should use the print_debugger() function to see why.
$config = Array(
'mailtype' => 'html'
);
$image_name = "some_name";
$content = "some content";
$subject = "Subject Name";
$this->load->library('email');
$this->email->initialize($config);
$this->email->from('abc#gmail.com');
$this->email->to('xxx#gmail.com');
$this->email->subject($subject);
$this->email->message($content);
$attched_file = $_SERVER["DOCUMENT_ROOT"]."/admin/assets/image/order_complete_file/".$image_name;
$this->email->attach($attched_file);
if($this->email->send()){ echo 'Email send.';}
else {show_error($this->email->print_debugger()); }

how to send email using codeigniter

today i have sending email using code igniter but not work properly .so please any one help me.
this is my controller page code here;
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {enter code here
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
// $this->load->library('pagination');
$this->load->library('session','form_validation');
}
function sendMail()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'siva#gmail.com', // here goes your mail
'smtp_pass' => 'mygamilpassword', // here goes your mail password
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->email->initialize($config);
$message = 'hiiiiii';
$this->load->library('email', $config);
$this->email->set_newline("rn");
$this->email->from('siva#gmail.com'); // here goes your mail
$this->email->to('sam#gmail.com');// here goes your mail
$this->email->subject('Resume from JobsBuddy');
$this->email->message($message);
if($this->email->send())
{
echo 'Email sent.';
}
else
{
show_error($this->email->print_debugger());
}
} $this->sendMail;
}
and in my localhost open php.extension->php.openssl enable and also the port number change for 465, and smtp:smtp.gmail.com this are all enable it.
but not working help me.?
Try this code. This is the working code:
function sendMail()
{
$config['protocol'] = "smtp";
$config['smtp_host'] = "ssl://smtp.googlemail.com";
$config['smtp_port'] = "465";
$config['smtp_user'] = "siva#gmail.com";
$config['smtp_pass'] = "mygamilpassword";
$message = "Your email body text goes here";
$config['mailtype'] = "html";
$ci = & get_instance();
$ci->load->library('email', $config);
$ci->email->set_newline("\r\n");
$ci->email->from("siva#gmail.com");
$ci->email->to("siva#gmail.com");
$ci->email->subject("Resume from JobsBuddy");
$ci->email->message($message);
$ci->email->send();
echo $this->email->print_debugger();
}
Try this code. This is the working code and allow-less-secure-apps-access-gmail-account setting open
public function send_mail() {
ini_set('SMTP', "server.com");
ini_set('smtp_port', "25");
ini_set('sendmail_from', "yourmail#gmail.com");
$this->load->library('email');
$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'] = 'yourmail#gmail.com';
$config['smtp_pass'] = 'yourpassword';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$from_email = "frommailid#gmail.com";
$to_email = "tomailid#gmail.com";
//Load email library
$this->load->library('email');
$this->email->from($from_email, 'Identification');
$this->email->to($to_email);
$this->email->subject('Send Email Codeigniter');
$this->email->message('The email send using codeigniter library');
//Send mail
if($this->email->send())
$this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
else
show_error($this->email->print_debugger());
$this->session->set_flashdata("email_sent","You have encountered an error");
$this->load->view('contact_email_form');
}

Uploading an image with Code Igniter?

I'm trying to set a site up so I can upload images using a web form in Code Igniter(CI). I'm not getting any errors but the file is not being saved either. I wanted to know if those that were successful in uploading images could help explain what might be the issue?
View:
<?php
echo form_open_multipart('admin/galleryUpload') . "\n";
echo "<div class='span-8'><span class='text'>Image:</span>" . form_upload('uploadImg') . "</div>";
foreach ($gallery as $picture)
{
$order[] = $picture->order;
}
$order[] = count($order) + 1;
echo "<div class='span-6 last'><span>Image Order #:</span>" . form_dropdown('order', $order) . "</div><div class='span-14'> </div>";
$conf = array('name' => 'alt_text', 'size' => '75');
echo "<div class='span-14 last'><span>Discription:</span>" . form_input($conf) . "<br /></div>";
echo form_hidden('propertyID', "$propertyID");
echo form_submit('upload', 'Upload');
echo form_close();
?>
Controller:
class Admin extends Controller
{
function galleryUpload()
{
if (! $this->session->userdata('is_admin'))
{
redirect('admin/index');
}
else
{
$this->load->model('admin_model');
$this->admin_model->imgUpload();
}
}
}
Model:
class Admin_model extends Model
{
function imgUpload()
{
$id = $this->input->post('propertyID');
$order = $this->input->post('order');
$alt_text = $this->input->post('alt_text');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
//'upload_path' => '../' . $this->imgPath($id),
'upload_path' => '../img/galleries/temp/',
'max_size' => '5000', // 5MB files max
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->imgPath($id) . '/thumbs',
'maintain_ratio' => TRUE,
'width' => '60'
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
From the user guide:
By default the upload routine expects the file to come from a form
field called userfile, and the form must be a multipart type
So you either have to change the name of the form field to userfile:
form_upload('userfile')
or pass the name of your form field to do_upload:
$this->upload->do_upload('uploadImg');

How to send an email with content from a View in codeigniter

I want to send an email to user from my application with the content of the email loaded from a view . This is the code i've tried out till now:
$toemail = "user#email.id";
$subject = "Mail Subject is here";
$mesg = $this->load->view('template/email');
$this->load->library('email');
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->to($toemail);
$this->email->from($fromemail, "Title");
$this->email->subject($subject);
$this->email->message($mesg);
$mail = $this->email->send();
You need to call $this->load->library('email'); within the controller as well for the email in CI to work.
Also , in your code : $fromemail is not initialized.
You need to have SMTP support on your server.
$config should be declared as an array before assigning values and keys.
Working Code:
$this->load->library('email');
$fromemail="ad#c.com";
$toemail = "user#email.id";
$subject = "Mail Subject is here";
$data=array();
// $mesg = $this->load->view('template/email',$data,true);
// or
$mesg = $this->load->view('template/email','',true);
$config=array(
'charset'=>'utf-8',
'wordwrap'=> TRUE,
'mailtype' => 'html'
);
$this->email->initialize($config);
$this->email->to($toemail);
$this->email->from($fromemail, "Title");
$this->email->subject($subject);
$this->email->message($mesg);
$mail = $this->email->send();
Edit:
$mesg = $this->load->view('template/email',true); should be having the true as pointed out by lycanian. By setting it as true , it doesn't send data to the output stream but it will return as a string.
Edit:
$this->load->view(); need a second parameter with data or empty like $mesg = $this->load->view(view,data,true);, if not it wont work
This line $mesg = $this->load->view('template/email',true); should be like this
$mesg = $this->load->view('template/email','',true);
with the single quotes before the value true, and it will work perfectly
Email template send In codeigniter we need to put and meta tag before sending email
$this->data['data'] = $data;
$message = $this->load->view('emailer/create-account', $this->data, TRUE);
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');
$this->email->from($email, $name);
$this->email->to('emailaddres#mail.com');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
U will try it!! it's working for mine after many errors facing
$subject = 'New message.';
$config = Array(
'protocol' => 'sendmail',
'smtp_host' => 'Your smtp host',
'smtp_port' => 465,
'smtp_user' => 'webmail',
'smtp_pass' => 'webmail pass',
'smtp_timeout' => '4',
'mailtype' => 'html',
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');
$this->email->from('from mail address', 'Company name ');
$data = array(
'message'=> $this->input->post('message')
);
$this->email->to($toEmail);
$this->email->subject($subject);
$body = $this->load->view('email/sendmail.php',$data,TRUE);
$this->email->message($body);
$this->email->send();

Resources