How do i remove attachment form phpmailer when sending a different mail for second time in php

僤鯓⒐⒋嵵緔 提交于 2019-12-11 09:06:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!