以下是透過gmail smtp發信的發信,並使用phpmailer:
step1:下載phpmailer
從這下載:http://sourceforge.net/projects/phpmailer/
step2:使用phpmailer
裡面的檔案只有兩個是我們要的
1.class.phpmailer
2.class.smtp
把這兩個檔案放在你要放置的目錄。
step3:寫一支發送email的php程式:
開頭先將剛剛的class.phpmailer.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 |
<?php <a name="cl-2"></a>require_once("phpmailer/class.phpmailer.php"); <a name="cl-3"></a>class Mail <a name="cl-4"></a>{ <a name="cl-5"></a> public static function send_mail($user_email, $user_name, $subject, $body) <a name="cl-6"></a> { <a name="cl-7"></a> $mail = new PHPMail(); <a name="cl-8"></a> $mail->IsSMTP(); <a name="cl-9"></a> $mail->SMTPAuth = true; <a name="cl-10"></a> $mail->SMTPSecure = "ssl"; <a name="cl-11"></a> $mail->Host = "smtp.gmail.com"; <a name="cl-12"></a> $mail->Port = 465; <a name="cl-13"></a> $mail->CharSet = "utf-8"; <a name="cl-14"></a> <a name="cl-15"></a> $mail->Username = "xxxxxx@xxx.xx"; <a name="cl-16"></a> $mail->Password = "email password"; <a name="cl-17"></a> <a name="cl-18"></a> //$mail->AddAttachment(""); <a name="cl-19"></a> <a name="cl-20"></a> $mail->Subject = $subject; <a name="cl-21"></a> $mail->From = "xxxxxx@xxx.xxx"; <a name="cl-22"></a> $mail->FromName = "xxxxxx"; <a name="cl-23"></a> $mail->Body = $body; <a name="cl-24"></a> $mail->IsHTML(true); <a name="cl-25"></a> $mail->AddAddress($user_email, $user_name); <a name="cl-26"></a> if (!$mail->Send()) { <a name="cl-27"></a> echo 'OOPs!! Fail to send email'.$mail->ErrorInfo; <a name="cl-28"></a> return $this->noview(); <a name="cl-29"></a> } else { <a name="cl-30"></a> // <a name="cl-31"></a> } <a name="cl-32"></a> } <a name="cl-33"></a>} |
之後呼叫這個涵式即可。
以上只是個範例,可依自己方式修改。