Codeigniter sends an email with an attached file

I am trying to send an email to codeigniter with an attached file.

I always receive a letter successfully. However, I never get with the attached file. Below is the code and is highly appreciated for all comments.

$ci = get_instance(); $ci->load->library('email'); $config['protocol'] = "smtp"; $config['smtp_host'] = "ssl://smtp.gmail.com"; $config['smtp_port'] = "465"; $config['smtp_user'] = " test@gmail.com "; $config['smtp_pass'] = "test"; $config['charset'] = "utf-8"; $config['mailtype'] = "html"; $config['newline'] = "\r\n"; $ci->email->initialize($config); $ci->email->from(' test@test.com ', 'Test Email'); $list = array(' test2@gmail.com '); $ci->email->to($list); $this->email->reply_to(' my-email@gmail.com ', 'Explendid Videos'); $ci->email->subject('This is an email test'); $ci->email->message('It is working. Great!'); $ci->email->attach( '/test/myfile.pdf'); $ci->email->send(); 
+10
php email codeigniter email-attachments
source share
8 answers

$ this-> email-> attach ()

Allows you to send an attachment. Put the path / file name in the first parameter. Note. Use the file path, not the URL. For several applications, the function is used several times. For example:

 public function setemail() { $email=" xyz@gmail.com "; $subject="some text"; $message="some text"; $this->sendEmail($email,$subject,$message); } public function sendEmail($email,$subject,$message) { $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://smtp.googlemail.com', 'smtp_port' => 465, 'smtp_user' => ' abc@gmail.com ', 'smtp_pass' => 'passwrd', 'mailtype' => 'html', 'charset' => 'iso-8859-1', 'wordwrap' => TRUE ); $this->load->library('email', $config); $this->email->set_newline("\r\n"); $this->email->from(' abc@gmail.com '); $this->email->to($email); $this->email->subject($subject); $this->email->message($message); $this->email->attach('C:\Users\xyz\Desktop\images\abc.png'); if($this->email->send()) { echo 'Email send.'; } else { show_error($this->email->print_debugger()); } } 
+21
source share

I have this problem before, the problem is with the path file, so I change the path file to


$attched_file= $_SERVER["DOCUMENT_ROOT"]."/uploads/".$file_name; $this->email->attach($attched_file);


And it works great with me

+3
source share

With Codeigniter 3.1.0, I had the same problem. Missing "\ r \ n" seems to be:

 Content-Type: application/pdf; name="test.pdf"<br> Content-Disposition: attachment;<br> Content-Transfer-Encoding: base64<br> JVBERi0xLjYNJeLjz9MNCjQzNyAwIG9iag08PC9MaW5lYXJpemVkIDEvTCA3OTUyMTYvTyA0Mzkv<br> RSA2ODEwODcvTiA0L1QgNzk0ODA3L0ggWyA1NjQgMjYxXT4+DWVuZG9iag0gICAgICAgICAgICAg<br> 

it should be:

 Content-Type: application/pdf; name="test.pdf"<br> Content-Disposition: attachment;<br> Content-Transfer-Encoding: base64<br> <br> JVBERi0xLjYNJeLjz9MNCjQzNyAwIG9iag08PC9MaW5lYXJpemVkIDEvTCA3OTUyMTYvTyA0Mzkv<br> RSA2ODEwODcvTiA0L1QgNzk0ODA3L0ggWyA1NjQgMjYxXT4+DWVuZG9iag0gICAgICAgICAgICAg<br> 

I changed line 725 in system / libraries / Email from

  'content' => chunk_split(base64_encode($file_content)),<br> 

for image

 'content' => "\r\n" . chunk_split(base64_encode($file_content)),<br> 

This works for me, but not the perfect solution.

+3
source share

Try putting the full path in $ ci-> email-> attach ();

In windows it looks like

 $ci->email->attach('d:/www/website/test/myfile.pdf'); 

This method worked well for me in the past.

+2
source share

use path helper

 $this->load->helper('path'); $path = set_realpath('./images/'); 

by email

 $this->email->attach($path . $your_file); 
0
source share

here i use phpmailer to send mail
here the full code is mentioned below

 $this->load->library('My_phpmailer'); $mail = new PHPMailer(); $mailBody = "test mail comes here2"; $body = $mailBody; $mail->IsSMTP(); // telling the class to use SMTP $mail->Host = "ssl://smtp.gmail.com"; // SMTP server $mail->SMTPDebug = 1;// enables SMTP debug information (for testing) // 1 = errors and messages // 2 = messages only $mail->SMTPAuth = true;// enable SMTP authentication $mail->Host = "ssl://smtp.gmail.com"; // sets the SMTP server $mail->Port = 465;// set the SMTP port for the GMAIL server $mail->Username = " YourAccountIdComesHere@gmail.com "; // SMTP account username $mail->Password = "PasswordComesHere";// SMTP account password $mail->SetFrom(' SetFromId@gmail.com ', 'From Name Here'); $mail->AddReplyTo(" SetReplyTo@gmail.com ", "Reply To Name Here"); $mail->Subject = "Mail send by php mailer"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test $mail->MsgHTML($body); $mail->AddAttachment($cdnStorage . '/' . $fileName); $address =' WhomeToSendMailId@gmail.com '; $mail->AddAddress($address, "John Doe"); if (!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } 
0
source share

Is there any “size limit” when we attach a file using smtp gmail? For example, when I attach a file smaller than 80 KB, it works, but with a file size of up to 80 KB, the file attachment is not sent, but the email is still sent

0
source share
  <?php class Email extends CI_Controller { public Function index(); { $config = Array( 'protocol' => 'smtp', 'smpt_host' => 'ssl://googlemail.com', 'smtp_port' => 465, 'smtp_user' => ' example@gmail.com ', 'smtp_pass' => 'yourpass' ); $this->load->library('email', $config); $this->email->set_newline("\r\n"); $this->email->from(' example@gmail.com '); $this->email->to(' example@gmail.com '); $this->email->subject('This is a test email sending'); $this->email->message('This is some message, you can type your own'); if($this->email->send() { echo "Your email has been sent"; }else{ show_error($this->email->print_debugger()); } } ?> 
-one
source share

All Articles