用 CodeIgniter 寄信

果然也是有人寫好啦,首先把這包 codeigniter-phpmailer 打包回去,然後依據裡面的東西分別放到對應的資料夾。

之後把 application/config/email.php 裡的設定改一下

$config['smtp_host'] = 'smtp.gmail.com'; //如果是 gmail 就改這樣
$config['smtp_user'] = '[email protected]'; //你收發的 email
$config['smtp_pass'] = 'yourpassword'; //email 的密碼
$config['smtp_port'] = 465; //這是 gmail 的 port

然後再照他的範例,在要寄信的地方把下面的地方改一下就好了。

  1.  $this->load->library('email');   
  2.   
  3.             $subject = 'This is a test';   
  4.             $message = '<p>This message has been sent for testing purposes.</p>';   
  5.   
  6.             // Get full html:   
  7.             $body =   
  8. '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  9. <html xmlns="http://www.w3.org/1999/xhtml">   
  10. <head>   
  11.     <meta http-equiv="Content-Type" content="text/html; charset='.strtolower(config_item('charset')).'" />   
  12.     <title>'.html_escape($subject).'</title>   
  13.     <style type="text/css">   
  14.         body {   
  15.             font-family: Arial, Verdana, Helvetica, sans-serif;   
  16.             font-size: 16px;   
  17.         }   
  18.     </style>   
  19. </head>   
  20. <body>   
  21. '.$message.'  
  22. </body>   
  23. </html>';   
  24.             // Also, for getting full html you may use the following internal method:   
  25.             //$body = $this->email->full_html($subject, $message);   
  26.   
  27.             $result = $this->email   
  28.                 ->from('[email protected]')   
  29.                 ->reply_to('[email protected]')    // Optional, an account where a human being reads.   
  30.                 ->to('[email protected]')   
  31.                 ->subject($subject)   
  32.                 ->message($body)   
  33.                 ->send();  

留言