📜  CodeIgniter-发送电子邮件

📅  最后修改于: 2020-10-26 05:31:44             🧑  作者: Mango


使用CodeIgniter发送电子邮件要容易得多。您还可以在CodeIgniter中配置有关电子邮件的首选项。 CodeIgniter提供以下用于发送电子邮件的功能-

  • 多种协议-邮件,Sendmail和SMTP
  • SMTP的TLS和SSL加密
  • 多个收件人
  • 抄送和密件抄送
  • HTML或纯文本电子邮件
  • 附件
  • 自动换行
  • 优先事项
  • 密件抄送批处理模式,可将大型电子邮件列表拆分为小型密件抄送。
  • 电子邮件调试工具

电子邮件类具有以下功能,以简化发送电子邮件的工作。

S.N. Syntax Parameters Return Return Type
1 from($from[, $name = ”[, $return_path = NULL]])

$from (string) − “From” e-mail address

$name (string) − “From” display name

$return_path (string) − Optional email address to redirect undelivered e-mail to

CI_Email instance (method chaining) CI_Email
2 reply_to($replyto[, $name = ”])

$replyto (string) − E-mail address for replies

$name (string) − Display name for the reply-to e-mail address

CI_Email instance (method chaining) CI_Email
2 to($to)

$to (mixed) − Comma-delimited string or an array of e-mail addresses

CI_Email instance (method chaining) CI_Email
3 cc($cc)

$cc (mixed) − Comma-delimited string or an array of e-mail addresses

CI_Email instance (method chaining) CI_Email
4 bcc($bcc[, $limit = ”])

$bcc (mixed) − Comma-delimited string or an array of e-mail addresses

$limit (int) − Maximum number of e-mails to send per batch

CI_Email instance (method chaining) CI_Email
5 subject($subject)

$subject (string) − E-mail subject line

CI_Email instance (method chaining) CI_Email
6 message($body)

$body (string) − E-mail message body

CI_Email instance (method chaining) CI_Email
7 set_alt_message($str)

$str (string) − Alternative e-mail message body

CI_Email instance (method chaining) CI_Email
8 set_header($header, $value)

$header (string) − Header name

$value (string) − Header value

CI_Email instance (method chaining) CI_Email
9 clear([$clear_attachments = FALSE])

$clear_attachments (bool) – Whether or not to clear attachments

CI_Email instance (method chaining) CI_Email
10 send([$auto_clear = TRUE])

$auto_clear (bool) − Whether to clear message data automatically

CI_Email instance (method chaining) CI_Email
11 attach($filename[, $disposition = ”[, $newname = NULL[, $mime = ”]]])

$filename (string) − File name

$disposition (string) − ‘disposition’ of the attachment. Most email clients make their own decision regardless of the MIME specification used here. iana

$newname (string) − Custom file name to use in the e-mail

$mime (string) − MIME type to use (useful for buffered data)

CI_Email instance (method chaining) CI_Email
12 attachment_cid($filename)

$filename (string) − Existing attachment filename

Attachment Content-ID or FALSE if not found string

发送邮件

要使用CodeIgniter发送电子邮件,首先必须使用以下命令加载电子邮件库-

$this->load->library('email');

加载库后,只需执行以下功能即可设置必要的元素以发送电子邮件。 from()函数用于设置-从何处发送电子邮件, to()函数用于-向谁发送电子邮件。 subject()message()函数用于设置电子邮件的主题和消息。

$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()函数以发送电子邮件。

$this->email->send();

创建一个控制器文件Email_controller.php并将其保存在application / controller / Email_controller.php中

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中

 
      CodeIgniter Email Example 
   
    
    
      session->flashdata('email_sent'); 
         echo form_open('/Email_controller/send_mail'); 
      ?> 
        
       
       
        
       
   
    

application / config / routes.php中的route.php文件中进行更改,并在文件末尾添加以下行。

$route['email'] = 'Email_Controller';

通过访问以下链接执行以上示例。用您网站的URL替换yoursite.com。

http://yoursite.com/index.php/email