PHPMailer动态邮件收件人插入

时间:2019-05-09 23:41:12

标签: php phpmailer

我们都知道如何在项目中实现PHPMailer,它的用法很棒,但是在输入必要的参数后,我需要更多的东西才能使一切正常。 我需要一个表格来更改将接收电子邮件的收件人的电子邮件地址,该怎么办?

代码:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';


$mail = new PHPMailer(true); // Passing `true` enables exceptions                             
try {
    //Server settings
    $mail->SMTPDebug = 0; // Enable verbose debug output                                 
    $mail->isSMTP(); //Set mailer to use SMTP                                      
    $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
    $mail->SMTPAuth = true; // Enable SMTP authentication
    $mail->Username = 'xxx@gmail.com'; // SMTP username
    $mail->Password = 'xxx'; // SMTP password
    $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587; // TCP port to connect to

    //Recipients
    $mail->setFrom('xxx', 'xxx'); // Sender email and name
    $mail->addAddress('xxx'); // Reciver email

    // if you want to send email to multiple users, then add the email addresses you which you want to send.
    //$mail->addAddress('reciver2@gmail.com');
    //$mail->addAddress('reciver3@gmail.com');

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

    //Content
    $mail->isHTML(true);// Set email format to HTML                                  
    $mail->Subject = 'Oggetto dell email'; // Subject of the email
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';

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

0 个答案:

没有答案