CodeIgniter : send email with attachment

24 11 2010

You will see how easy to send email with attachments in codeigniter. Here we will send email from our gmail account. Lets check the method for sending email –

function sendMail()
{
    $config=array(
      'protocol'=>'smtp',
      'smtp_host'=>'ssl://smtp.googlemail.com',
      'smtp_port'=>465,
      'smtp_user'=>'username@gmail.com',
      'smtp_pass'=>'yourGmailPass'
    );

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

    $this->email->set_newline("\r\n");
    $this->email->from("user@gmail.com","Your Name");
    $this->email->to("someone@gmail.com");
    $this->email->subject("Test message!");
    $this->email->message("Its working!");

    $path=$_SERVER["DOCUMENT_ROOT"];
    $file=$path."/ci/attachments/info.txt";

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

    if($this->email->send())
    {
        echo "Mail send successfully!";
    }

    else
    {
        show_error($this->email->print_debugger());
    }
}