Ad Code

Ticker

6/recent/ticker-posts

How to send email with attachment in codeigniter

Send Email with attachment

There are a set of configured function for sending mails from codeigniter with attachment of any files, there are the $config array which include the configuration of SMTP mail.

There are use gmail smtp configuration.

function sendMail()
  {
        $config=array(
          'mailpath' => '/usr/sbin/sendmail',
          'protocol'=>'smtp',
          'smtp_host'=>'ssl://smtp.gmail.com',
          'smtp_port'=>465,
          'smtp_user'=>'yourmail@gmail.com',
          'smtp_pass'=>'mailpassword',
          'mailtype'  => 'html', 
          'charset'   => 'iso-8859-1'
        );

        $this->load->library("email",$config);

        $this->email->set_newline("\r\n");
        $this->email->from('From Mail', 'From name');
        $this->email->to('user@usermail.in');
        $this->email->subject('Email Attachment Test');
        $this->email->message('Testing the attachment email class.'); 
        
        $path=$_SERVER["DOCUMENT_ROOT"];
        $file=$path."/example.csv";

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

        if($this->email->send())
        {
            echo "Mail send successfully with attachement!";
        }else{
            show_error($this->email->print_debugger());
        }
    }

There are made some changes like attachment file path:-

$path=$_SERVER["DOCUMENT_ROOT"];
$file=$path."/exam.csv";

If you are use to send mail using html form with file input then use the $_FILES in php and use temp file path for attach in the mail function.

And also other configuration variable:-

From Mail:- this is a mail which is define as a sender mail.
From Name:- sender name.
To('user@usermail.in'):- There is a recipient email address.
And Subject and message.

In message section you can also use normal text or html files, if use send html mail then you must use in configuration setting like this:-

'mailtype'  => 'html',

this is define a mail type, and this is also accept normal text and attached files in mail.

Post a Comment

4 Comments

Ad Code