PHP กับการเขียนโปรแกรมส่ง Email ด้วย CodeIgniter - email library

โดย SONGCHAI SAETERN

PHP Sending email via SMTP server


จากบทความการตั้งค่าเพื่อเขียนโปรแกรม ส่งอีเมล์ด้วย PHP วิธีเปิดใช้งาน Authen Account ของ Gmail  ซึ่งเป็นการส่งผ่าน PHPMailer แต่สำหรับ CodeIgniter 3 สามารถเรียกใช้งานไลบรารี่ email ได้เลย ซึ่งมีการเขียนแนะนำไว้ดังนี้

ที่มา : https://stackoverflow.com/questions/37492272/how-to-use-phpmailer-in-codeigniter

และก็ลองค้นหาบทความเพิ่มเติมเกี่ยวกับการเขียนโปรแกรมส่งอีเมลด้วย PHP CodeIgntier ผ่าน Gmail ก็ได้ตัวอย่างโค้ดดังนี้

//Load email library$this->load->library('email');
//SMTP & mail configuration$config = array(
    'protocol'  => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'user@gmail.com',
    'smtp_pass' => 'gmail_password',
    'mailtype'  => 'html',
    'charset'   => 'utf-8');$this->email->initialize($config);$this->email->set_mailtype("html");$this->email->set_newline("\r\n");
//Email content$htmlContent '<h1>Sending email via SMTP server</h1>';$htmlContent .= '<p>This email has sent via SMTP server from CodeIgniter application.</p>';
$this->email->to('recipient@example.com');$this->email->from('sender@example.com','MyWebsite');$this->email->subject('How to send email via SMTP server in CodeIgniter');$this->email->message($htmlContent);
//Send email$this->email->send();

ซึ่งไลบรารี่ email ใน CodeIgntier นี้ ยังสามารถปรับค่า config เพื่อส่งอีเมลโดยใช้ระบบอีเมลของเซิร์ฟเวอร์ หรือเว็บโฮสติ้งก็ได้ด้วยเช่นกัน


และการนำไปใช้งานจริงๆ จะมีการเรียกใช้มากกว่า 1 ครั้ง หรือมากกว่า 1 หน้าแน่นอน
ดังนั้น สร้างเป็นฟังก์ชั่นแยกออกไปเหมือนตัวอย่างแรกด้านบนนั้นจะดีกว่า

public function send_email($to, $subject, $message) {
    $config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'mail.gmx.com',
        'smtp_port' => 587, //465,
        'smtp_user' => 'myself@gmx.com',
        'smtp_pass' => 'PASSWORD',
        'smtp_crypto' => 'tls',
        'smtp_timeout' => '20',
        'mailtype'  => 'html', 
        'charset'   => 'iso-8859-1'
    );
    $config['newline'] = "\r\n";
    $config['crlf'] = "\r\n";
    $this->CI->load->library('email', $config);
    $this->CI->email->from('myself@gmx.com', 'Admin');
    $this->CI->email->to($to);
    $this->CI->email->subject($subject);
    $this->CI->email->message($message);

    //$this->email->send();
    if ( ! $this->CI->email->send()) {
        return false;
    }
    return true;
}


กรณีส่งแล้ว Error : fsockopen(): unable to connect to ssl://smtp.gmail.com:465 ดูที่บทความนี้นะครับ   http://phpcodemania.blogspot.com/2018/02/codeigniter-mail-fsockopen-unable-to-connect-to-gmail.html


:: อ้างอิง ::

การส่งอีเมล์ด้วย PHP โดยมีการเรียกใช้งาน Authen Account ของ Gmail
http://phpcodemania.blogspot.com/2015/11/php-authen-account-gmail.html

Sending Email via SMTP Server in CodeIgniter
https://www.codexworld.com/codeigniter-send-email-gmail-smtp-server/

How to use PHPMailer in codeigniter?
https://stackoverflow.com/questions/37492272/how-to-use-phpmailer-in-codeigniter


PHP CI MANIA PHP Code Generator 
โปรแกรมช่วยสร้างโค้ด ลดเวลาการเขียนโปรแกรม เขียนโปรแกรมง่ายและสะดวกขึ้น
สนใจสั่งซื้อราคาสุดคุ้ม >> http://fastcoding.phpcodemania.com/