เนื้อหาตอนนี้ จะเป็บการประยุกต์การใช้งาน Email Class ของ Codeigniter ในการ
ส่งอีเมล โดยเราจะทำในหน้า contactus ให้ผู้ใช้สามารถติดต่อกับเราด้วยการ
ส่งอีเมล สอบถามเพิ่มเติมเข้ามาได้
ตามเป็นจริงแล้วการใช้งานในส่วนของการส่งอีเมล จะไม่ค่อยมีอะไรซับซ้อนเท่าไรนัก
แต่สำหรับการทดสอบที่เครื่อง localhost เราได้มีบทความเกี่ยวกับจำลองการส่งอีเมล
มาในบทความก่อนหน้า และเราจะใช้ในบทความนี้
การตั้งค่า mercury ใน xampp สำหรับทดสอบส่งอีเมล บน localhost
https://www.ninenik.com/content.php?arti_id=681 via @ninenik
สำหรับ Email Class ของ codeigniter นั้นสามารถรองรับทั้งการ
- ส่งผ่านโปรโตคอล Mail , Sendmail และ SMTP
- รองรับการเข้ารห้สแบบ TLS และ SSL สำหรับ SMTP
- ส่งให้ผู้รับพร้อมกันหลายคน
- รองรับ CC และ BCCs
- รองรับการส่งในรูปแบบข้อความ text ธรรมดา หรือ HTML
- รองรับการแนบไฟล์
- รองรับการตัดคำอัตโนมัติ
- รองรับการกำหนดความสำคัญของอีเมล
- รองรับ BCC Batch Mode สำหรับการส่งอีเมลจำนวนมาก
- มีระบบตรวจสอบการทำงาน
คุณสมบัติและความสามารถตามด้านบน เราสามารถหาอ่านได้ในคู่มือได้เพิ่มเติม
ก่อนที่จะใช้งาน Email Class เรามาดูไฟล์ในโปรเจ็คของเรา ประกอบด้วย
1. Contactus_model.php ไฟล์นี้ยังไม่มีเราจะสร้างไฟล์นี้ไว้ในไฟลเดอร์ apps > models
2. contactus.php เรามีไฟล์นี้อยู่แล้วในโฟลเดอร์ apps > views > pages
การทำงานคือ เราจะมีฟอร์มในหน้า contactus ให้ผู้ใช้กรอก ชื่อ อีเมล หัวข้อ และรายละเอียด
โดยจะมีการตรวจสอบฟอร์มด้วย form_validation ในที่นี้จะสมมติให้ส่งจากอีเมล
postmaster@localhost.com และส่งไปยังอีเมล demo@localhost.com
โดยจำลองว่า
postmaster@localhost.com คืออีเมลของผู้ใช้
demo@localhost.com คืออีเมลของเว็บไซต์ให้ผู้ใช้ส่งมาหา
เมื่อทำการส่งอีเมลแล้วจะให้แสดงข้อความแจ้งการส่ง หรือหากเกิดข้อผิดพลาดก็จะให้แสดง
ข้อความแจ้งการส่งผิดพลาด
เนื้อหาในตอนนี้มีการใช้งาน form validation และ library user agent
ซึ่งเคยได้กล่าวไว้แล้วในตอนต้นๆ ของโปรเจ็ค สามารถดูเพิ่มเติมได้ที่หัวข้อ
ใช้งาน Form Validation สำหรับบันทึกและแก้ไขข้อมูลใน model ตอนที่ 3
https://www.ninenik.com/content.php?arti_id=669 via @ninenik
การเรียกใช้ Email Class สำหรับส่งอีเมล
เริ่มต้นให้เราสร้างไฟล์ Contactus_model.php ไว้ในโฟลเดอร์ apps > models
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php class Contactus_model extends CI_Model { public function __construct() { parent::__construct(); $this ->load->library( 'form_validation' ); // เรียกใช้ form validation $this ->form_validation->set_error_delimiters( '<div class="bg-danger" style="padding:3px 10px;">' , '</div>' ); $this ->load->library( 'user_agent' ); /// เรียกใช้ user agent class $this ->load->library( 'email' ); // เรียกใช้ email class } // สร้างฟังก์ชั่นสำหรับส่งอีเมล โดยรับค่าตัวแปร $mailData เป้น array public function send( $mailData ){ /// BEGIN MAIL SETTINGS // $config['mailtype'] = 'html'; // $config['wordwrap'] = TRUE; // $this->email->initialize($config); ///// END MAIL SETTINGS // ส่งจากอีเมล - ชื่อ $this ->email->from( $mailData [ 'mail_email' ], $mailData [ 'mail_name' ]); $this ->email->to( 'demo@localhost.com' , 'Demo User' ); // ส่งถึง $this ->email->subject( $mailData [ 'mail_title' ]); // หัวข้อที่ส่ง $this ->email->message( $mailData [ 'mail_detail' ]); // รายละเอียด return $this ->email->send(); // คืนค่าการทำงานว่าเป็น true หรือ false } } |
ต่อไป มาดูที่ไฟล์ contactus.php ในโฟลเดอร์ apps > views > pages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | <div class = "container" > <h1><?= $title_h1 ?></h1> <?php if ( $action ==null || $action == 'send' ){?> <?php $this ->form_validation->set_rules( 'mail_name' , 'Name' , 'required' ); $this ->form_validation->set_rules( 'mail_email' , 'Email' , 'required' ); $this ->form_validation->set_rules( 'mail_title' , 'Title' , 'required' ); $this ->form_validation->set_rules( 'mail_detail' , 'Detail' , 'required' ); if ( $this ->form_validation->run() === FALSE){ // ถ้าตรวจสอบไม่ผ่าน ให้ทำงาน // echo "Error"; } else { // กรณีตรวจสอบผ่าน $mailData = array ( "mail_name" => $this ->input->post( 'mail_name' ), "mail_email" => $this ->input->post( 'mail_email' ), "mail_title" => $this ->input->post( 'mail_title' ), "mail_detail" => $this ->input->post( 'mail_detail' ) ); if ( $this ->contactus_model->send( $mailData )){ redirect( 'contactus/success' ); // } else { redirect( 'contactus/error' ); // } } if (validation_errors()){ // ถ้ามีเงื่อนไขหนึ่งใดไม่ผ่าน ให้แสดง ข้อความ error ตำแหน่งนี้ echo validation_errors(); echo "<br>" ; } ?> <?php echo form_open( 'contactus/send' ); ?> <table class = "table table-bordered" > <tr class = "active" > <td colspan= "2" >Contact Us</td> </tr> <tr> <th class = "text-right" > Name: </th> <td> <input type= "text" name= "mail_name" id= "mail_name" value= "<?=set_value('mail_name')?>" class = "form_control" style= "width:75%" > </td> </tr> <tr> <th class = "text-right" > Email: </th> <td> <input type= "text" name= "mail_email" id= "mail_email" value= "<?=set_value('mail_email')?>" class = "form_control" style= "width:75%" > </td> </tr> <tr> <th class = "text-right" > Title: </th> <td> <input type= "text" name= "mail_title" id= "mail_title" value= "<?=set_value('mail_title')?>" class = "form_control" style= "width:75%" > </td> </tr> <tr> <th class = "text-right" > Detail: </th> <td> <textarea name= "mail_detail" id= "mail_detail" rows= "7" style= "width:75%" class = "form_control" ><?=set_value( 'mail_detail' )?></textarea> </td> </tr> <tr> <th class = "text-right" > </th> <td> <input type= "submit" value= "Send Mail" name= "btn_mail" class = "btn btn-primary" > </td> </tr> </table> </form> </div> <?php } ?> <?php if ( $action == "success" ){?> <br><br> <div class = "bg-success text-center" style= "padding:10px;" > <p class = "text-success" >Send data completed</p> <a href= "<?=base_url('contactus')?>" class = "text-success" >< Back > </a> </div> <?php } ?> <?php if ( $action == "error" ){?> <?php $previous_url = $this ->agent->referrer(); ?> <br><br> <div class = "bg-danger text-center" style= "padding:10px;" > <p class = "text-danger" >Fail! please try again.</p> <a href= "<?=$previous_url?>" class = "text-danger" >< Back > </a> </div> <?php } ?> </div> |
มาดูแต่ละส่วนจากโค้ดไฟล์ contactus.php ด้านบน
จากโค้ดเราจะมีส่วนของตัวแปร $action อยู่ 4 ค่า ได้แก่ null, send, success และ error
ส่วนของ $action เท่ากับ error
ใช้สำหรับแจ้งกรณีเกิดข้อผิดพลาดในการส่งอีเมล หรือส่งอีเมลไม่ได้
1 2 3 4 5 6 7 8 9 10 11 | <?php if ( $action == "error" ){?> <?php $previous_url = $this ->agent->referrer(); ?> <br><br> <div class = "bg-danger text-center" style= "padding:10px;" > <p class = "text-danger" >Fail! please try again.</p> <a href= "<?=$previous_url?>" class = "text-danger" >< Back > </a> </div> <?php } ?> |
ส่วนของ $action เท่ากับ success
ใช้สำหรับแจ้งกรณีส่งอีเมลสำเร็จ
1 2 3 4 5 6 7 8 | <?php if ( $action == "success" ){?> <br><br> <div class = "bg-success text-center" style= "padding:10px;" > <p class = "text-success" >Send data completed</p> <a href= "<?=base_url('contactus')?>" class = "text-success" >< Back > </a> </div> <?php } ?> |
ส่วนของ $action เท่ากับ null และ send
ใช้สำหรับแสดงฟอร์มส่งอีเมล และทำงานคำสั่งส่งอีเมล แยกอธิบายแต่ละส่วนดังนี้
กำหนด rules ให้กับการตรวจสอบฟอร์มด้วย form validation ในที่นี้เช็คแค่ว่าทุกช่อง
ต้องกรอก ฟอร์มจึงจะทำการ submit ได้
1 2 3 4 | $this ->form_validation->set_rules( 'mail_name' , 'Name' , 'required' ); $this ->form_validation->set_rules( 'mail_email' , 'Email' , 'required' ); $this ->form_validation->set_rules( 'mail_title' , 'Title' , 'required' ); $this ->form_validation->set_rules( 'mail_detail' , 'Detail' , 'required' ); |
ตรวจสอบฟอร์มจาก rules ที่กำหนด ถ้าผ่าน เก็บค่าข้อมูลจากฟอร์มไว้ใน
ตัวแปรที่ชื่อ $mailData ในรุปแบบตัวแปร array จากนั้น
เรียกใช้งาน models ชื่อ contactus_model เพื่อเรียกใช้งานฟังก์ชั่น send()
ที่เราสร้างไว้สำหรับส่งอีเมล โดยส่งค่าตัวแปร array ข้อมูลจากฟอร์มเข้าไป
และถ้าทำงานสำเร็จ หรือก็คือส่งอีเมลได้ ก็จะให้ แสดงผลในส่วนของ $action
เท่ากับ success หากตรงข้าม ก็จะแสดงผลว่า error คือไม่สามารถส่งอีเมลได้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | if ( $this ->form_validation->run() === FALSE){ // ถ้าตรวจสอบไม่ผ่าน ให้ทำงาน // echo "Error"; } else { // กรณีตรวจสอบผ่าน $mailData = array ( "mail_name" => $this ->input->post( 'mail_name' ), "mail_email" => $this ->input->post( 'mail_email' ), "mail_title" => $this ->input->post( 'mail_title' ), "mail_detail" => $this ->input->post( 'mail_detail' ) ); if ( $this ->contactus_model->send( $mailData )){ redirect( 'contactus/success' ); // } else { redirect( 'contactus/error' ); // } } |
ส่วนแสดงแจ้งเตือน กรณีฟอร์มไม่ผ่านตาม rules ที่กำหนด
1 2 3 4 | if (validation_errors()){ // ถ้ามีเงื่อนไขหนึ่งใดไม่ผ่าน ให้แสดง ข้อความ error ตำแหน่งนี้ echo validation_errors(); echo "<br>" ; } |
สร้างฟอร์มด้วย fom_open() ฟังก์ชั่น เมื่อ submit ข้อมูลให้ไปที่ $action เท่ากับ send
1 | <?php echo form_open( 'contactus/send' ); ?> |
ที่เหลือก็จะเป็นในส่วนของฟอร์ม มีการกำหนดให้จำค่าข้อมูลที่กรอกไป กรณียังไม่ผ่าน rules ด้วยฟังก์ชั่น set_value()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <table class = "table table-bordered" > <tr class = "active" > <td colspan= "2" >Contact Us</td> </tr> <tr> <th class = "text-right" > Name: </th> <td> <input type= "text" name= "mail_name" id= "mail_name" value= "<?=set_value('mail_name')?>" class = "form_control" style= "width:75%" > </td> </tr> <tr> <th class = "text-right" > Email: </th> <td> <input type= "text" name= "mail_email" id= "mail_email" value= "<?=set_value('mail_email')?>" class = "form_control" style= "width:75%" > </td> </tr> <tr> <th class = "text-right" > Title: </th> <td> <input type= "text" name= "mail_title" id= "mail_title" value= "<?=set_value('mail_title')?>" class = "form_control" style= "width:75%" > </td> </tr> <tr> <th class = "text-right" > Detail: </th> <td> <textarea name= "mail_detail" id= "mail_detail" rows= "7" style= "width:75%" class = "form_control" ><?=set_value( 'mail_detail' )?></textarea> </td> </tr> <tr> <th class = "text-right" > </th> <td> <input type= "submit" value= "Send Mail" name= "btn_mail" class = "btn btn-primary" > </td> </tr> </table> |
เท่านี้เราก็สามารถส่งอีเมล ในรุปแบบข้อความ text ไปยังอีเมลที่ต้องการได้แล้ว
สามารถนำรูปแบบนี้ไปประยุกต์ เพิ่มเติมตามต้องการได้
การตั้งค่าให้ Email Class รองรับการใช้งาน SMTP
เรามาดูการตั้งค่าเพิ่มเติม สำหรับการใช้งานอีเมล สมมติเราต้องการให้ส่งผ่าน SMTP
สามารถกำหนดในไฟล์ Contactus_model.php ได้ดังต่อไปนี้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <?php class Contactus_model extends CI_Model { public function __construct() { parent::__construct(); $this ->load->library( 'form_validation' ); // เรียกใช้ form validation $this ->form_validation->set_error_delimiters( '<div class="bg-danger" style="padding:3px 10px;">' , '</div>' ); $this ->load->library( 'user_agent' ); /// เรียกใช้ user agent class $this ->load->library( 'email' ); // เรียกใช้ email class } // สร้างฟังก์ชั่นสำหรับส่งอีเมล โดยรับค่าตัวแปร $mailData เป้น array public function send( $mailData ){ /// BEGIN MAIL SETTINGS // ดู gmail port เพิ่มเติมได้ที่ https://support.google.com/a/answer/176600?hl=en $config [ 'useragent' ] = 'Ninenik.com' ; // กำหนดส่งจากอะไร เช่น ใช่ชื่อเว็บเรา $config [ 'protocol' ] = 'smtp' ; // สามารถกำหนดเป็น mail , sendmail และ smtp $config [ 'smtp_host' ] = 'smtp-relay.gmail.com' ; $config [ 'smtp_user' ] = 'gmail_account' ; $config [ 'smtp_pass' ] = 'gmal_password' ; $config [ 'smtp_port' ] = '25' ; $config [ 'smtp_crypto' ] = 'tls' ; // รูปแบบการเข้ารหัส กำหนดได้เป้น tls และ ssl $config [ 'mailtype' ] = 'html' ; // กำหนดได้เป็น text หรือ html $this ->email->initialize( $config ); ///// END MAIL SETTINGS // ส่งจากอีเมล - ชื่อ $this ->email->from( $mailData [ 'mail_email' ], $mailData [ 'mail_name' ]); $this ->email->to( 'demo@localhost.com' , 'Demo User' ); // ส่งถึง // หากต้องการส่งแบบให้มี cc หรือ bcc กำหนดตามด้านล่าง // $this->email->cc('another@another-example.com'); // $this->email->bcc('them@their-example.com'); // หากต้องการแนบไฟล์ กำหนดตามนี้ // $this->email->attach('/path/to/photo1.jpg'); // $this->email->attach('/path/to/photo2.jpg'); // $this->email->attach('/path/to/photo3.jpg'); // หรือ แนบไฟล์แบบให้แสดงในอีเมลเลย เหมาะกับรูป // $this->email->attach('image.jpg', 'inline'); // หรือ แนบไฟล์แบบกำหนด url เข้าไปตรงๆ เลย // $this->email->attach('https://www.ninenik.com/filename.pdf'); // หรือแบบอัพโหลดไฟล์ // $this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf'); $this ->email->subject( $mailData [ 'mail_title' ]); // หัวข้อที่ส่ง $this ->email->message( $mailData [ 'mail_detail' ]); // รายละเอียด return $this ->email->send(); // คืนค่าการทำงานว่าเป็น true หรือ false } } |
ดูตารางการตั้งค่าเพิ่มเติม และค่าเริ่มต้น กรณีเราไม่ได้กำหนดค่า จะเป็นไปตามค่าเริ่มต้น
Preference | Default Value | Options | Description |
---|---|---|---|
useragent | CodeIgniter | None | The “user agent”. |
protocol | mail, sendmail, or smtp | The mail sending protocol. | |
mailpath | /usr/sbin/sendmail | None | The server path to Sendmail. |
smtp_host | No Default | None | SMTP Server Address. |
smtp_user | No Default | None | SMTP Username. |
smtp_pass | No Default | None | SMTP Password. |
smtp_port | 25 | None | SMTP Port. |
smtp_timeout | 5 | None | SMTP Timeout (in seconds). |
smtp_keepalive | FALSE | TRUE or FALSE (boolean) | Enable persistent SMTP connections. |
smtp_crypto | No Default | tls or ssl | SMTP Encryption |
wordwrap | TRUE | TRUE or FALSE (boolean) | Enable word-wrap. |
wrapchars | 76 | Character count to wrap at. | |
mailtype | text | text or html | Type of mail. If you send HTML email you must send it as a complete web page. Make sure you don’t have any relative links or relative image paths otherwise they will not work. |
charset | $config['charset'] | Character set (utf-8, iso-8859-1, etc.). | |
validate | FALSE | TRUE or FALSE (boolean) | Whether to validate the email address. |
priority | 3 | 1, 2, 3, 4, 5 | Email Priority. 1 = highest. 5 = lowest. 3 = normal. |
crlf | n | “rn” or “n” or “r” | Newline character. (Use “rn” to comply with RFC 822). |
newline | n | “rn” or “n” or “r” | Newline character. (Use “rn” to comply with RFC 822). |
bcc_batch_mode | FALSE | TRUE or FALSE (boolean) | Enable BCC Batch Mode. |
bcc_batch_size | 200 | None | Number of emails in each BCC batch. |
dsn | FALSE | TRUE or FALSE (boolean) | Enable notify message from server |
ตัวอย่างการแนบไฟล์ที่อัพโหลดอย่างง่าย ใน models
ขอเพิ่มเติมตัวอย่างการแนบไฟล์แบบให้ผู้ใช้อัพโหลดไฟล์แนบไปพร้อมอีเมล
สามารถกำหนดในส่วนของการ attachment ได้ดังนี้
1 2 3 4 5 | $this ->email->attach( $mailData [ 'mail_file' ][ 'tmp_name' ], 'attachment' , $mailData [ 'mail_file' ][ 'name' ], $mailData [ 'mail_file' ][ 'type' ] ); |
โดยเราต้องส่งค่าตัวแปร $_FILES เข้ามาฟังก์ชั่นนี้ด้วย เช่น
1 2 3 4 5 6 7 | $mailData = array ( "mail_name" => $this ->input->post( 'mail_name' ), "mail_email" => $this ->input->post( 'mail_email' ), "mail_title" => $this ->input->post( 'mail_title' ), "mail_detail" => $this ->input->post( 'mail_detail' ), "mail_file" => $_FILES [ 'file_upload' ] ); |
แบบนี้เป็นต้น