问题
In a php file i need to send 2 different emails to 2 different id's. It did not work when i used two variables like this shown below.
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();
But if i use the same variable twice i will work as shown below
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();
But the problem is it is sending the attachment to both the email ids. Please help me how do i not send the attachment to second id.
回答1:
unset($mail->attachment)
won't work as attachment
is a protected variable. Instead use:
$email->clearAttachments();
回答2:
Before execute /* Second Email */
You can try:
unset($mail->attachment)
来源:https://stackoverflow.com/questions/19136350/how-do-i-remove-attachment-form-phpmailer-when-sending-a-different-mail-for-seco