I'm having a slight issue with sending to multiple email addresses from a form using phpmailer. I can send to a single email address, but when I type multiple email addresses separated by commas into either the to, cc, or bcc textboxes on the form, the mail fails to send. This is a brief snippet of my code. The issue is related to my explode() lines of code. Any help would be appreciated. Thank you.
Code: Select all
if (empty($errors)) {
require_once 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsHTML(true);
$to = $_POST['emailTo'];
$emailcc = $_POST['emailcc'];
$emailbcc = $_POST['emailbcc'];
$subject = "RE: " . $_POST['emailSubject'];
$mail->From = "cjkeane@hotmail.com";
$mail->FromName = "cjkeane@hotmail.com";
$emailsExploded = explode(",", $to);
$ccemailsExploded = explode(",", $emailcc);
$bccemailsExploded = explode(",", $emailbcc);
foreach($emailsExploded as $emailAddress){$email->AddAddress(trim($emailAddress));}
foreach($ccemailsExploded as $ccemailAddress){$email->AddCC(trim($ccemailAddress));}
foreach($bccemailsExploded as $bccemailAddress){$email->AddBCC(trim($bccemailAddress));}
$mail->Subject = "RE: " . $subject;
$mail->Body = removeHTML($_POST['ActionTextField']);
$mail->AltBody = removeHTML($_POST['ActionTextField']);
}
if(!$mail->Send()) {
echo '<div style="color:red;" align="right">Reply failed to be sent...</div><br/>';
} else {
echo '<b>Validation Error(s):</b><br /><br />';
foreach ($errors as $msg) {
echo '<div style="color:red;"> -' . $msg . '</div><br />';
}
}