با سلام خدمت کاربران گرامی سایت پی وی لرن و کاربرانی که دوره آموزش codeigniter را دنبال می کنند. در جلسه قبل با نحوه آپلود فایل در codeigniter آشنا شدیم. در این جلسه نیز به نحوه ارسال ایمیل در codeigniter می پردازیم.
مراحل ارسال ایمیل در codeigniter بسیار ساده است؛ علاوه بر آن می توان تنظیمات مربوط به مراحل ارسال ایمیل در codeigniter را در قسمت پیکربندی این فریم ورک انجام داد.
کلاس ایمیل (Email class) دارای ویژگی های زیر است که کار ارسال ایمیل را ساده می کند:
Return Type | Return | Parameters | Syntax | S.N. |
CI_Email | CI_Email instance (method chaining) | $from (string) − “From” e-mail address $name (string) − “From” display name $return_path (string) − Optional email address to redirect undelivered e-mail to | from($from[, $name = ”[, $return_path = NULL]]) | ۱ |
CI_Email | CI_Email instance (method chaining) | $replyto (string) − E-mail address for replies $name (string) − Display name for the reply-to e- mail address | reply_to($replyto[, $name = ”]) | ۲ |
CI_Email | CI_Email instance (method chaining) | $cc (mixed) − Comma- delimited string or an array of e-mail addresses | to($to) | ۳ |
CI_Email | CI_Email instance (method chaining) | $bcc (mixed) − Comma- delimited string or an array of e-mail addresses $limit (int) − Maximum number of e-mails to send per batch | bcc($bcc[, $limit = ”]) | ۴ |
CI_Email | CI_Email instance (method chaining) | $subject (string) − E- mail subject line | subject($subject) | ۵ |
CI_Email | CI_Email instance (method chaining) | $body (string) − E-mail message body | message($body) | ۶ |
CI_Email | CI_Email instance (method chaining) | $str (string) − Alternative e-mail message body | set_alt_message($str) | ۷ |
CI_Email | CI_Email instance (method chaining) | $header (string) − Header name $value (string) − Header value | set_header($header, $value) | ۸ |
CI_Email | CI_Email instance (method chaining) | $clear_attachments(bool) – Whether or not to clear attachments | clear([$clear_attachments = FALSE]) | ۹ |
CI_Email | CI_Email instance (method chaining) | $auto_clear (bool) − Whether to clear message data automatically | send([$auto_clear = TRUE] | ۱۰ |
CI_Email | CI_Email instance (method chaining) | $filename (string) − File name $disposition (string) − $newname (string) − Custom $mime (string) − MIME type to | attach($filename[, $disposition = ”[, $newname = NULL[, $mime = ”]]]) | ۱۱ |
string | Attachment Content- ID or FALSE if not found | attachment_cid($filename) | attachment_cid($filename) | ۱۲ |
برای ارسال ایمیل در codeigniter ابتدا لازم است که کتابخانه ایمیل (email library)را در codeigniter به وسیله کد زیر فعال کنید:
1 | $this->load->library('email'); |
بعد از بارگذاری کتابخانه ایمیل، به راحتی با فعال کردن کارکرد های زیر، عناصر ضروری را در ارسال ایمیل تنظیم نمایید.
از کارکرد from() برای تنظیم محل ارسال ایمیل، از کارکرد to() برای تنظیم محل دریافت ایمیل، از کارکرد subject() و message() نیز برای تنظیم محتوا و پیام استفاده می شوند.
1 2 3 4 5 | $this->email->from('your@example.com', 'Your Name'); $this->email->to('someone@example.com'); $this->email->subject('Email Test'); $this->email->message('Testing the email class.'); |
در مرحله بعد، کارکرد send() را همان طور که در کد زیر می بینید فعال نمایید:
1 | $this->email->send(); |
یک فایل کنترل کننده با عنوان Email_controller.php بسازید و در application/controller/Email_controller.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 | <?php class Email_controller extends CI_Controller { function __construct() { parent::__construct(); $this->load->library('session'); $this->load->helper('form'); } public function index() { $this->load->helper('form'); $this->load->view('email_form'); } public function send_mail() { $from_email = "your@example.com"; $to_email = $this->input->post('email'); //Load email library $this->load->library('email'); $this->email->from($from_email, 'Your Name'); $this->email->to($to_email); $this->email->subject('Email Test'); $this->email->message('Testing the email class.'); //Send mail if($this->email->send()) $this->session->set_flashdata("email_sent","Email sent successfully."); else $this->session->set_flashdata("email_sent","Error in sending Email."); $this->load->view('email_form'); } } ?> |
یک فایل نمایش با عنوان email_form.php بسازید و در application/views/email_form.php ذخیره نمایید.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>CodeIgniter Email Example</title> </head> <body> <?php echo $this->session->flashdata('email_sent'); echo form_open('/Email_controller/send_mail'); ?> <input type = "email" name = "email" required /> <input type = "submit" value = "SEND MAIL"> <?php echo form_close(); ?> </body> </html> |
این تغییرات را در فایل routes.php در application/config/routes.php ایجاد کنید و خط زیر را به آخر فایل اضافه کنید.
1 | $route['email'] = 'Email_Controller'; |
مثال بالا را با مراجعه به لینک زیر اجرا کنید. yoursite.com را با URL سایت خود جایگزین نمایید.
در این جلسه از آموزش codeigniter ، به آموزش نحوه ارسال ایمیل در codeigniter پرداختیم و یاد گرفتیم که چطور به ارسال ایمیل از طریق فریم ورک کدایگنایتر بپردازیم. در جلسه بعد از این سلسله جلسات آموزشی به اعتبار سنجی فرم در codeigniter خواهیم پرداخت.
با پی وی لرن همراه باشید.