1.登入google後,找到 google帳戶,在"安全性"下會看到如下圖
![]() |
未啟用兩步驟驗證的畫面 |
2.選擇上圖中的"兩步驟驗證"後,按下"開始使用"後,依照步驟操作完成,會得到一組16位數的密碼,此密碼會用於步驟三中的
- $mail->Password = '密碼';
程式碼配置與發信程式撰寫(在沒有 composer 的環境下)
一、程式碼下載
1.到 github 上下載程式碼:PHPMailer,並解壓縮
![]() |
解壓縮後資料夾內容 |
二、發信程式碼撰寫
- <?php
- try {
- /* Set the mail encode. */
- CharSet = 'UTF-8';
- /* Set the mail sender. */
- $mail->setFrom('寄件人@gmail.com', '寄件人名稱');
- /* Add a recipient. */
- $mail->addAddress('收件人郵件地址', '收件人名稱');
- /* Set the subject. */
- $mail->Subject = '主旨';
- /* Set the mail message body. */
- $mail->Body = '信件內容';
- /* SMTP parameters. */
- /* Tells PHPMailer to use SMTP. */
- $mail->isSMTP();
- /* SMTP server address. */
- $mail->Host = 'smtp.gmail.com';
- /* Use SMTP authentication. */
- $mail->SMTPAuth = TRUE;
- /* Set the encryption system. */
- $mail->SMTPSecure = 'tls';
- /* SMTP authentication username. */
- $mail->Username = '寄件人@gmail.com';
- /* SMTP authentication password. google 兩步驟認證的密碼*/
- $mail->Password = '密碼';
- /* Set the SMTP port. */
- $mail->Port = 587;
- /* Finally send the mail. */
- $mail->send();
- echo "成功發送信件!";
- }
- catch (Exception $e)
- {
- echo "發送失敗!";
- /* PHPMailer exception. */
- echo $e->errorMessage();
- }
- catch (\Exception $e)
- {
- echo "發送失敗!";
- /* PHP exception (note the backslash to select the global namespace Exception class). */
- echo $e->getMessage();
- }
- ?>
沒有留言:
張貼留言