使用phpmailer发送两个带有附件的不同电子邮件

时间:2019-05-09 14:36:17

标签: php phpmailer

我有一个问题,我不知道在哪里。 我的表格将发送两个不同的邮件:

  1. 一个简单的感谢邮件,仅需发送给已编译表格并带有附件的人
  2. 第二封邮件,仅需要发送给网站所有者,该邮件将以相同的附件进行回复

我的问题是,使用phpmailer,编译表单的客户端同时接收到这两个邮件。当然,他不必看到第二封邮件。

这是我的代码:

$to = $email; 
$subject = "Princype - la tua configurazione"; 
$message =
"Buongiorno $nome, <br>
in allegato potrai trovare le tua configurazione per il tuo $taglio $style.
<br>
A breve entrerai in contatto con un nostro incaricato breve per confermare o fissare un appuntamento.
<br>
Cordialmente,
<br><br>";




// email stuff 2
$to2 = "info@info.com"; 
$subject2 = "Princype - richiesta info per $taglio $style - $id_planimetria"; 
$message2 =
"Una nuova richiesta per l'appartamento $taglio $style - $id_planimetria,
<br>
<br>
Nome: $nome <br>
Cognome: $cognome <br>
email: $email <br>
Telefono: $telefono <br>";



//first email
try {

    $mail->isSMTP();                                                    // Set mailer to use SMTP
    $mail->Host       = 'smtps.XXX.com';                               // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                           // Enable SMTP authentication
    $mail->Username   = 'info@info.com';                 // SMTP username
    $mail->Password   = 'XXXXXXX';                             // SMTP password
    $mail->SMTPSecure = 'ssl';                                          // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 000;                                            // TCP port to connect to

    //Recipients
    $mail->setFrom('info@info.com', 'info service');
    $mail->addAddress($to, sprintf('%s %s', $nome, $cognome));           // Add a recipient

    $mail->addReplyTo('info@info.com', 'info service');


    // Attachments
                  // Add attachments
    $mail->addAttachment($filepath, $filename);                            // Optional name

    // Content
    $mail->isHTML(true);                                                   // Set email format to HTML
    $mail->Subject = $subject;
    $mail->Body    = $message;
    $mail->AltBody = $message;

    $mail->send();

    //second mail
    $mail->setFrom('info@info.com', 'info service');
    $mail->addAddress('info@info.com', 'info service');           // Add a recipient
    $mail->addReplyTo($to, sprintf('%s %s', $nome, $cognome));


    $mail->Subject = $subject2;
    $mail->Body    = $message2;
    $mail->AltBody = $message2;

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

1 个答案:

答案 0 :(得分:0)

函数addAddress会将电子邮件地址附加到已知的收件人列表中。如果您不希望有多个接收器,则应在添加更多接收器之前清除接收器列表,这可以通过以下代码段完成,

$mail->clearAddresses();