Send Email with Attachment using SMTP – PHP Codeigniter

In this post, you will learn how to send emails in PHP using SMTP. Like PHPMailer, there are a few more good email sending libraries in PHP

Add ‘email‘ library

application/config/autoload.php

$autoload[‘libraries’] = array(’email’);

Controller Page Code

public function sendEmailAmd()
{
    $msg='Message';
    $sub='Subject';
    $dat=date('d/M/Y');

    $config = Array('protocol' => 'sendmail',
            'smtp_host' => 'mail.XXXXX.in',
            'smtp_port' => 25,
            'smtp_user' => 'info@XXXX.in',
            'smtp_pass' => 'infoXXXXXX',
            'smtp_timeout' => '4',
            'mailtype' => 'html',
            'charset' => 'iso-8859-1');


            $this->load->library('email', $config);
            $this->email->set_newline("\r\n");

            $subject=$sub;


            $message='Abdul Mohammad Message';

            $filePath=$_SERVER['DOCUMENT_ROOT'].'/Chief/assets/sample.pdf';

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


            $this->email->from('senderemail', 'Message');
            $s=$this->email->to('receiveremail','Message');
            $this->email->subject($subject);
            $this->email->message($message);



            if($this->email->send()) {
            echo 'Mail Sent';

            } else {
            echo 'NOT Sent';
            print_r($this->email->print_debugger());
            }
}

Leave a Reply

Your email address will not be published. Required fields are marked *