找回密碼
 立即註冊
搜索
熱搜: 活動 交友 discuz
查看: 459|回復: 3

PHPMailer

[複製鏈接]

257

主題

38

回帖

1138

積分

管理員

積分
1138
發表於 2023-5-25 10:23:07 | 顯示全部樓層 |閱讀模式
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = '[email protected]';                     //SMTP username
    $mail->Password   = 'secret';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');     //Add a recipient
    $mail->addAddress('[email protected]');               //Name is optional
    $mail->addReplyTo('[email protected]', 'Information');
    $mail->addCC('[email protected]');
    $mail->addBCC('[email protected]');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

257

主題

38

回帖

1138

積分

管理員

積分
1138
 樓主| 發表於 2023-5-25 10:23:15 | 顯示全部樓層


$mail= new PHPMailer(); //建立新物件
$mail->IsSMTP(); //設定使用SMTP方式寄信
$mail->SMTPAuth = true; //設定SMTP需要驗證
$mail->SMTPSecure = "ssl"; // Gmail的SMTP主機需要使用SSL連線
$mail->Host = "smtp.gmail.com"; //Gamil的SMTP主機
$mail->Port = 465; //Gamil的SMTP主機的埠號(Gmail為465)。
$mail->CharSet = "utf-8"; //郵件編碼
$mail->Username = " "; //Gamil帳號
$mail->Password = ""; //Gmail密碼
$mail->From = " "; //寄件者信箱
$mail->FromName = " "; //寄件者姓名
$mail->Subject =$subject; //郵件標題
$mail->Body =" "; //郵件內容
$mail->IsHTML(true); //郵件內容為html
$mail->AddAddress("$email"); //收件者郵件及名稱
$mail->AddBCC(" "); //設定 密件副本收件者
if(!$mail->Send()){

echo "Error: " . $mail->ErrorInfo;

}else{

echo "<b>您好!已收到您的留言,會盡快回覆</b>";

}

257

主題

38

回帖

1138

積分

管理員

積分
1138
 樓主| 發表於 2023-5-25 10:23:21 | 顯示全部樓層

    $mail->SetLanguage("zh", BASEPATH . "plugins/PHPMailer/language/");
    $mail->SMTPDebug = 0;   
      
    $mail->IsSMTP();                                //設定使用SMTP方式寄信
    $mail->SMTPAuth = false;                         //設定SMTP需要驗證
    $mail->SMTPSecure = false;                      // Gmail的SMTP主機需要使用SSL連線
    $mail->Host = "localhost";                 //Gamil的SMTP主機
    $mail->Port = 25;                              //Gamil的SMTP主機的埠號(Gmail為465)。
    $mail->CharSet = "utf-8";                       //郵件編碼
    $mail->Encoding = "base64";

    $mail->Username = "XXX";     //godaddy mail帳號
    $mail->Password = "XXX";      //godaddy mail密碼

257

主題

38

回帖

1138

積分

管理員

積分
1138
 樓主| 發表於 2023-9-24 13:11:16 | 顯示全部樓層
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->SMTPDebug = 2; //Alternative to above constant
$mail->isSMTP();  // tell the class to use SMTP
$mail->SMTPAuth   = true;                // enable SMTP authentication
$mail->Port       = 25;                  // set the SMTP port
$mail->Host       = "mail.yourhost.com"; // SMTP server
$mail->Username   = "[email protected]"; // SMTP account username
$mail->Password   = "your password";     // SMTP account password




Debug levels
Setting the PHPMailer->SMTPDebug property to these numbers or constants (defined in the SMTP class) results in different amounts of output:

SMTP::DEBUG_OFF (0): Disable debugging (you can also leave this out completely, 0 is the default).
SMTP::DEBUG_CLIENT (1): Output messages sent by the client.
SMTP::DEBUG_SERVER (2): as 1, plus responses received from the server (this is the most useful setting).
SMTP::DEBUG_CONNECTION (3): as 2, plus more information about the initial connection - this level can help diagnose STARTTLS failures.
SMTP::DEBUG_LOWLEVEL (4): as 3, plus even lower-level information, very verbose, don't use for debugging SMTP, only low-level problems.
You don't need to use levels above 2 unless you're having trouble connecting at all - it will just make output more verbose and more difficult to read.

Note that you will get no output until you call send(), because no SMTP conversation takes place until you do that.
您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|DoIT 科技論壇

GMT+8, 2025-6-15 10:44 , Processed in 0.013899 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回復 返回頂部 返回列表