使用 phpmailer 发送两封带附件的不同电子邮件
Send two different email with attachement with phpmailer
我有一个问题,我不知道问题出在哪里。
我的表格将发送两封不同的邮件:
- 一封简单的感谢邮件,只需要发送给编译表格的人,并附上附件
- 仅需发送给网站所有者的第二封邮件,该邮件将使用相同的附件进行回复
我的问题是,使用 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}");
}}?>
函数addAddress
会将e-mailaddress 附加到已知的接收者列表中。如果您不想要多个接收器,您应该在添加之前清除接收器列表,这可以通过以下代码片段完成,
$mail->clearAddresses();
我有一个问题,我不知道问题出在哪里。 我的表格将发送两封不同的邮件:
- 一封简单的感谢邮件,只需要发送给编译表格的人,并附上附件
- 仅需发送给网站所有者的第二封邮件,该邮件将使用相同的附件进行回复
我的问题是,使用 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}");
}}?>
函数addAddress
会将e-mailaddress 附加到已知的接收者列表中。如果您不想要多个接收器,您应该在添加之前清除接收器列表,这可以通过以下代码片段完成,
$mail->clearAddresses();