emailing to multiple email addresses with phpmail

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

emailing to multiple email addresses with phpmail

Post by cjkeane »

Hi Everyone.
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 />';
                 }        
        }

User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: emailing to multiple email addresses with phpmail

Post by twinedev »

Quick look, all looks good. Best way to debug it is to do a

Code: Select all

var_dump($emailsExploded,$ccemailsExploded,$bccmailsExploded);  
before the foreachs to make sure you are working with arrays of actual valid e-mail addresses.

If each of those seem ok, change the line in the loop to be something like:

Code: Select all

foreach($emailsExploded as $emailAddress){ var_dump($emailAddress, $email->AddAddress($emailAddress));}
This will dump out the current email address it is trying to add, and the response from adding it (true/false). Make sure every time has returned a TRUE value (again repeat for cc/bcc)

Finally if all of that looks good, right before the $mail->send() call do a flat out var_dump($mail). There will be a lot in there, but make sure all recipients end up in there.

As a side note, you will notice in my sniplet I left off the trim() function as this is not needed. Inside the class, it already applies a trim to the address you feed it.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: emailing to multiple email addresses with phpmail

Post by cjkeane »

I figured it out. thanks you !
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: emailing to multiple email addresses with phpmail

Post by cjkeane »

i am having another issue though, which i hope someone has an idea on how to fix.
phpmailer sends all attachments, but my coding to save the attachments into the uploads folder doesn't save all. for example, if i have two uploads, it saves one, if i have 5 uploads, it saves 4. its like its not looping through all of the attachments and uploading them correctly. it saves a link to the correct number of files in the db and emails the correct number it just doesnt upload the correct number. Any ideas?

Code: Select all

function rand_string( $length ) {
	$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	$size = strlen( $chars );
	for( $i = 0; $i < $length; $i++ ) {
		$str .= $chars[ rand( 0, $size - 1 ) ];
	}
	return $str;
}
foreach($_FILES as $userfile){
	$tmp_name = $userfile['tmp_name'];
	$name = $userfile['name'];
	$strFileType = strrev(substr(strrev($name),0,4));
	if (file_exists($tmp_name)){
		if(is_uploaded_file($tmp_name)){
			$NewUpload = $random_string.$strFileType;
			$uploaddir = 'upload/';								
			$random_string = rand_string(20);
			move_uploaded_file($tmp_name, $uploaddir.$NewUpload);
			$fullpath = $uploaddir . $NewUpload;
			$mail->addAttachment($fullpath, $name);
			if ($filename == "") {
				$filename = mysql_real_escape_string($random_string.$strFileType.'['.$name.']');
			} else {
				$filename = mysql_real_escape_string($filename.','.$random_string.$strFileType.'['.$name.']');
			}
                 }
	}
}                       
Post Reply