有些情況下,客人 PHP 網頁程式會透過 PHPMailer 作發送電郵。

基本 PHPMailer 例子設定如下 –
(需要更改 Host, Username, Password , and your email content)

<?php

#
# Based on PHPMailer v6.0.5
# ref.: https://github.com/PHPMailer/PHPMailer
#

require “PHPMailer/src/PHPMailer.php”;
require “PHPMailer/src/Exception.php”;
require “PHPMailer/src/SMTP.php”;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true); // Passing `true` enables exceptions

try {
//Server settings
# $mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ‘mail.yyyy–zzzz.com.local’; // Specify SMTP server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ‘xxxx@yyyy–zzzz.com.local’; // SMTP username
$mail->Password = ‘xxxxyyyy’; // SMTP password
$mail->SMTPSecure = ‘tls’; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to

$mail->SMTPOptions = array(
‘ssl’ => array(
‘verify_peer’ => false,
‘verify_peer_name’ => false,
‘allow_self_signed’ => true
)
);

//Recipients
$mail->setFrom(‘xxxx@yyyy–zzzz.com.local’, ‘Test Sender’);
$mail->addAddress(‘peter@some–email.com’, ‘Test receipt’); // Add a recipient
# $mail->addAddress(‘ellen@example.com’); // Name is optional
# $mail->addReplyTo(‘info@example.com’, ‘Information’);
# $mail->addCC(‘cc@example.com’);
# $mail->addBCC(‘bcc@example.com’);

 

//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 587’;
$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;
}

Pin It on Pinterest

Share This