如何在php中第二次发送不同的邮件时删除附件表单phpmailer

use*_*437 1 php email attachment phpmailer

在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();
Run Code Online (Sandbox Code Playgroud)

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

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();
Run Code Online (Sandbox Code Playgroud)

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

小智 6

unset($mail->attachment)将无法正常工作,attachment是一个受保护的变量.而是使用:

$email->clearAttachments();
Run Code Online (Sandbox Code Playgroud)


小智 -1

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

你可以试试:

unset($mail->attachment)
Run Code Online (Sandbox Code Playgroud)