Codeigniter Send Email with Attach File

Codeigniter send email with attach file

$this->email->attach()

Enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL. For multiple attachments use the function multiple 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());
}

}

send email with attachment in CodeIgniter

You can send attach file using

$this->email->attach($atch);

method in codeigniter. in this below code i'm sending mail using SMTP with
Attached File.

its Working perfectly.
You only need to specify base_url to define attachment file path

Controller

$email_config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'yourmail@gmail.com', // change it to yours
'smtp_pass' => 'mypasswords', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);

// Defined Attachment file using variable
$atch=base_url().'html/images/logo.png';

$this->load->library('email', $email_config);

$this->email->set_newline("\r\n");
$this->email->from('test@gmail.com', 'Rudra'); // email From
$this->email->to('mailtosend@gmail.com'); // eMail to send
$this->email->subject('Mail with Attachment'); // Subject
$this->email->message("This is mail with Attachment file using CodeIgniter."); // Message
$this->email->attach($atch); // Attachment file defined using variable

$maill=$this->email->send(); // send mail

if($maill>0)
{
echo 'Email sent.'; // success
}
else
{
show_error($this->email->print_debugger());
}

codeigniter email with attachment

this code solved for me:

        $aConfig['upload_path']      = './uploads/';
$aConfig['allowed_types'] = 'pdf';
$aConfig['max_size'] = '3000';
$config['encrypt_name'] = true;
$this->load->library('upload', $aConfig);

foreach ($_FILES as $key => $file)
{
if ($file['error'] == 0)
{
$this->email->attach($file['tmp_name'], '', $file['name']);
}
}

Codeigniter 3 email attachment from form

You have to add attachment file path in attachment argument

Replace Your post email controller as per below

public function postconEmail(){
$data = $this->input->post();
$this->load->library('email');
$config = array();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'mail.example.com';
$config['smtp_user'] = 'user@example.com';
$config['smtp_pass'] = 'password';
$config['smtp_port'] = 'xxx';
$this->email->initialize($config);

$this->email->set_newline("\r\n");

$this->email->from($data['from_email']);
$this->email->to('info@example.com');
$this->email->subject($data['subject']);
$this->email->message($data['message']);

$resume_tmp_path = $_FILES['resume']['tmp_name'].'/'.$_FILES['resume']['name'];

$this->email->attach($resume_tmp_path);
if ($this->email->send()) {
$this->session->set_flashdata('success','Email Sent');
redirect(base_url());
} else{
show_error($this->email->print_debugger());
}
}

If This is not working then you refer this question it says You cannot attach file without upload your Server so first you have to upload file in your server and then pass $this->email->attach(youy file path); so your code definetely work.

Refer This Question :
https://stackoverflow.com/a/3628203/3377733

Code Igniter - attach email

You can not directly attach a file from the upload field of your form to an email. You can only attach files to your email from your server, so you need to upload the file from the form with CIs upload library: $this->upload->do_upload() to your server into some directory. the upload library needs to be configured, which file types are allowed etc. if the upload was successful, the do_upload function returns extensive data about where the file is stored. you can use the 'full_path' index from the array to attach this file to the email. then send the mail. after that you may delete the file from your server. Here are some code fragments that might help.

$this->load->library('upload');

if($_FILES['upload']['size'] > 0) { // upload is the name of the file field in the form

$aConfig['upload_path'] = '/someUploadDir/';
$aConfig['allowed_types'] = 'doc|docx|pdf|jpg|png';
$aConfig['max_size'] = '3000';
$aConfig['max_width'] = '1280';
$aConfig['max_height'] = '1024';

$this->upload->initialize($aConfig);

if($this->upload->do_upload('upload'))
{
$ret = $this->upload->data();
} else {
...
}

$pathToUploadedFile = $ret['full_path'];
$this->email->attach($pathToUploadedFile);
...
$this->email->send();
...
}
...

Hope this helped...

Attach a file to an email in codeigniter

You can attach multiple files like this

$this->email->attach('/path/to/photo1.jpg');
$this->email->attach('/path/to/photo2.jpg');
$this->email->attach('/path/to/photo3.jpg');


Related Topics



Leave a reply



Submit