發佈日期:
分類:
如何使用PHP‧利用Gmail伺服器‧發送電郵
01. 要在PHP中發送電郵,最簡單就是使用現成有的PHPMailer。
02. 只需要按要求,設定以下參數便可以:
$mail->Host - Gmail伺服器名稱; $mail->Port = Gmail伺服器埠位; $mail->Username = Gmail帳戶; $mail->Password = Gmail密碼;
$mail->From - 寄件者電郵; $mail->AddAddress - 收件者電郵及姓名 $mail->AddAttachment - 附檔; $mail->Subject - 電郵目錄; $mail->Body - 郵件內容;
03. 以下為一個例子,可按要求再作更改。
$content = "<p>This is a testing email.</p>"; if ($content != "") { require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsSMTP(); $mail->Host = "ssl://smtp.gmail.com"; $mail->Port = "465"; $mail->SMTPAuth = true; $mail->Username = "user@gmail.com"; $mail->Password = "password"; $mail->From = "user@gmail.com"; $mail->AddAddress("user1@hotmail.com", "user1"); $mail->AddAddress("user2@hotmail.com", "user2"); $mail->AddAddress("user3@hotmail.com", "user3"); $mail->WordWrap = 999; $mail->AddAttachment("/attachment/file1.log"); $mail->AddAttachment("/attachment/file2.log"); $mail->IsHTML(true); $mail->Subject = "Email from Gmail"; $mail->Body = $content; if(!$mail->Send()) { echo "<p class=\\"content_text\\">Message could not be sent.</p>"; echo "<p class=\\"content_text\\">Mailer Error: " . $mail->ErrorInfo . "</p>"; exit; } echo "<p class=\\"content_text\\">Message has been sent.</p>"; } else { echo "<p class=\\"content_text\\">No error found.</p>"; } ?>
發佈留言