2

在一个 php 文件中,我需要向 2 个不同的 id 发送 2 封不同的电子邮件。当我使用如下所示的两个变量时,它不起作用。

require 'PHPmailer/class.phpmailer.php';

/* First Email*/

$email = new PHPMailer();
$email->From      = 'admin@mywebsite.com';
$email->FromName  = 'My Webisite';
$email->Subject   = 'Subject of first email';
$email->Body      = 'Body of the message to first person';
$email->AddAddress( 'to first person' );

$file_to_attach = 'path of the file';       
$email->AddAttachment( $file_to_attach, '' );

$email->Send();

/* Second Email*/

require 'PHPmailer/class.phpmailer.php';
$confirm = new PHPMailer();
$confirm-> From      = 'noreply@mywebsite.com';
$confirm-> FromName  = 'Admin @ MyWebsite';
$confirm-> Subject   = 'Subject of second email';
$confirm-> Body      = 'Body of second email';
$confirm-> AddAddress('Email ID of second person');

$confirm->Send();

但是,如果我两次使用相同的变量,我将如下所示工作

require 'PHPmailer/class.phpmailer.php';

/* First Email*/

$email = new PHPMailer();
/* Same as above*/
$file_to_attach = 'path of the file';       
$email->AddAttachment( $file_to_attach, '' );

$email->Send();

/* Second Email*/

$email-> From      = 'noreply@mywebsite.com';
$email-> FromName  = 'Admin @ MyWebsite';
$email-> Subject   = 'Subject of second email';
$email-> Body      = 'Body of second email';
$email-> AddAddress('Email ID of second person');

$email->Send();

但问题是它将附件发送到两个电子邮件 ID。请帮助我如何不将附件发送到第二个 ID。

4

2 回答 2

9

unset($mail->attachment)不能像attachment保护的变量一样工作。而是使用:

$email->clearAttachments();
于 2014-02-17T11:35:34.130 回答
-1

执行前 /* 第二封电子邮件 */

你可以试试:

unset($mail->attachment)
于 2014-01-14T08:15:45.587 回答