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

Black Sulfur
Black Sulfur used Ask the Experts™
on
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 = 'myhost.com';  // Specify main and backup SMTP servers
			$mail->SMTPAuth = true;                               // Enable SMTP authentication
			$mail->Username = 'info@mysite.com';                 // SMTP username
			$mail->Password = 'secret';                           // SMTP password
			$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
			$mail->Port = 587;                                    // 465 or 587

			//Recipients
			$mail->setFrom($setFrom);
			$mail->addAddress($addAddress);
			$mail->addReplyTo($addReplyTo);
                   $mail->addBCC($addBCC);


			//Content
			$mail->isHTML(true);
			$mail->Subject = $subject;
			$mail->Body    = $html;
			$mail->AltBody = $altBody;

			$mail->send();
			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 = 'info@mysite.com';
                    $addReplyTo = $email;
                    $addAddress = 'info@mysite.com';
                    $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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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)) {
    $mail->addBCC($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

Author

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 = 'info@somesite.com';
$addReplyTo = 'info@somesite.com';
$addAddress = $_SESSION['user_email'];
$addBCC = 'info@anothermail.com';
$altBody = '';

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

Open in new window

OK, fixed that!

Author

Commented:
Thanks! :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial