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

2

5

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      = '[email protected]';
$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      = '[email protected]';
$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      = '[email protected]';
$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.

Grattan answered 2/10, 2013 at 11:57 Comment(0)
K
12

unset($mail->attachment) won't work as attachment is a protected variable. Instead use:

$email->clearAttachments();
Kimber answered 17/2, 2014 at 11:35 Comment(0)
C
-1

Before execute /* Second Email */

You can try:

unset($mail->attachment)
Chauchaucer answered 14/1, 2014 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.