phpmailer won't send using class that requires bcc to be passed in

I have created a class for using phpMailer and only on some emails I want a BCC. But if I try to send an email without the BCC it stops working.

The class looks like this:

class Email {
	public static function sendMail($html, $subject, $setFrom, $addReplyTo, $addAddress, $altBody, $addBCC)
		$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
		try {
			//Server settings
			//$mail->SMTPDebug = 3;                                 // Enable verbose debug output
			$mail->isSMTP();                                      // Set mailer to use SMTP
			$mail->Host = '';  // Specify main and backup SMTP servers
			$mail->SMTPAuth = true;                               // Enable SMTP authentication
			$mail->Username = '';                 // SMTP username
			$mail->Password = 'secret';                           // SMTP password
			$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
			$mail->Port = 587;                                    // 465 or 587


			$mail->Subject = $subject;
			$mail->Body    = $html;
			$mail->AltBody = $altBody;

			return true;
			} catch (Exception $e) {
				throw new \Exception($e->errorMessage()); // Wrap it into a global exception
			} catch (\Exception $e) { // Global exception - see the leading slash
				throw $e;

Open in new window

Then in the controller I send the mail like this:

                    $html = $this->getView('emails/contact', $data);
                    $subject = 'Contact Form';
                    $setFrom = '';
                    $addReplyTo = $email;
                    $addAddress = '';
                    $altBody = '';
                    $send = new Email();
                    $send->sendMail($html, $subject, $setFrom, $addReplyTo, $addAddress, $altBody);

Open in new window

I haven't got $addBCC in there because I don't need it for this particular email. I tried to just make $addBCC = '' and then pass it in but that doesn't work either.
Black SulfurAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marcus BointonCommented:
You have two problems here.

If you try to add an empty address, addBCC will throw an exception for an invalid address, so wrap it in a condition.

if (!empty($addBCC)) {

Open in new window

The $addBCC parameter has no default value, so calling the function without providing it is a fatal error, so give it a default value so you only need to supply it if you need to:

public static function sendMail($html, $subject, $setFrom, $addReplyTo, $addAddress, $altBody, $addBCC = null)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Black SulfurAuthor Commented:
Thank you, I did that but now I get a 500 internal server error when I try submit. I am using Ajax.

I changed the line as you suggested and added the if condition.

public static function sendMail($html, $subject, $setFrom, $addReplyTo, $addAddress, $altBody, $addBCC = null)

Open in new window

And here is the code to send the email:

$html = $this->getView('emails/order-details', $data);
$subject = 'Order';
$setFrom = '';
$addReplyTo = '';
$addAddress = $_SESSION['user_email'];
$addBCC = '';
$altBody = '';

$send = new Email();
$send->sendMail($html, $subject, $setFrom, $addReplyTo, $addAddress, $altBody, $addBCC);

Open in new window

Marcus BointonCommented:
OK, fixed that!
Black SulfurAuthor Commented:
Thanks! :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.