0

I'm using PHPMailer v6.1.5 here in May 2020, and I am on shared GoDaddy servers via cPanel. I have copied all the PHPMailer files into their own directory off of my root directory. I can send one email at a time just fine. But the moment I put in a loop, sending out 4 - 10 emails, it crashes (something about unexpected 'use'). It's my belief that I cannot use Composer in my case. Any thoughts on how I can get it to work in a loop? Apologies.. but there are lots of these questions in Stackoverflow going back years.. but none seem to work in my situation.

So here is my code ...........

$path = '/home/myrootdirectory/public_html/PHPMailer/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

// load all the PHPMailer classes
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require $path . '/src/Exception.php';
require $path . '/src/PHPMailer.php';
require $path . '/src/SMTP.php';

$mail = new PHPMailer(true);
$mail->From = $fromEmail;
$mail->FromName = $fromFullName;
$mail->addAddress($toEmail, $toFullName);
$mail->addBCC("admin@mycompany.com");
if ($fromSenderCopyYN=="Y") {
    $mail->addBCC($fromEmail);
}

// Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->AltBody = $txtmsg;
if(!$mail->send()) { 
   // OK
}else{
   // Not OK
}

=================================

Any help would be very appreciated.

Jim Turner
  • 19
  • 5
  • If you can run PHP locally, you can use composer. To send to a list efficiently, look at the mailing list example provided with PHPMailer. – Synchro May 03 '20 at 07:20

1 Answers1

0

Well, I solved it.. I put the following code before the loop begins ..

$path = '/home/myrootdirectory/public_html/PHPMailer/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

and then changed the

require $path . '/src/Exception.php';
require $path . '/src/PHPMailer.php';
require $path . '/src/SMTP.php';

to

require_once $path . '/src/Exception.php';
require_once $path . '/src/PHPMailer.php';
require_once $path . '/src/SMTP.php';

Works great now.

Jim Turner
  • 19
  • 5