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());
}
}
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...
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
Codeigniter email with attachment not working
To send your attachment use absolute path instead of relative and make sure your attachment is accessable for public.
absolute path example
c:/test/test/cv_uploads/cv.doc
in your code try with
$file_name= FCPATH.'/cv_uploads/'.$up_data['file_name'];
or
$file_name= $_SERVER["DOCUMENT_ROOT"].'/cv_uploads/'.$up_data['file_name'];
and attach file with this function
$this->email->attach($file_name);
attach a file from directory and send to email with codeigniter
I'm not really clear on where you are running into a problem. 'that variable' will be the files you uploaded, yes? Create an array of the filepaths as they get uploaded and when the upload is done cycle through each for email attachment.
Have you checked other answers on the site? Maybe take a look here. But CI's documentation clearly states that you can use:
$this->email->attach('/path/to/that_file.jpg');
multiple times.
Update:
You can either try using the onUploadSuccess function in uploadify to append each file name to something that you can use later...
'onUploadSuccess' : function(file, data, response) {
alert('The file name is ' + file.name);
...
OR from within uploadify.php. From there you can store what you need for attaching after.
In your case I'd stick with modifying the uploadify.php. You'll have to give it a shot and post some code if you are stuck, but there are plenty of places to get some ideas like here and here
Related Topics
Shell_Exec() Timeout Management & Exec()
Can Mongodb and Its Drivers Preserve the Ordering of Document Elements
Use of Undefined Constant Stdin - Assumed 'Stdin' in C:\Wamp\Www\Study\Sayhello.PHP on Line 5
PHP File_Get_Contents() Behaves Differently to Browser
Getting PHP to Run a Python Script
Using PHP, How to Insert Text Without Overwriting to the Beginning of a Text File
How to Validate Non-English (Utf-8) Encoded Email Address in JavaScript and PHP
Php: How to Check If the Client Is Local
Attach Image to Facebook Event (PHP Sdk, Rest or Graph API)
Why Is My Database Backup Script Not Working in PHP
What Does the "&" Sign Mean in PHP
How to Call Entity Manager in a Constructor
How to Use Facebook Graph API to Retrieve Fan Photos Uploaded to Wall of Fan Page