Results 1 to 2 of 2

Thread: Issues with phpMailer

  1. #1
    Join Date
    Aug 2009
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Issues with phpMailer

    I am using phpMailer to email information from a form. On the form I am using check boxes for the people to CC: into the email. The check boxes are an array (ccs[]) and the value of each check box is the individual's email address. When I check off one person on the form the email is sent with no problems. When I check off more than one person though I get an error. I'm sure that it has something to do with the fact that I'm using a variable to fill my array but I can't figure out how to fix this problem and make it work.

    Here is what I am doing in my code:

    Code:
    $ccsChecked 	= "";
    
    foreach($_POST['ccs'] as $value2) {
    	$ccsChecked .= "$value2 ";
    }
    
    $emailAddresses = array("myEmail@email.com");
    $ccAddresses = array($ccsChecked);
    
    ...
    
    foreach($ccAddresses as $ccAddress) {
    	$mail->AddCC($ccAddress);
    }
    Any help is much appreciated! Thanks!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That code is not sending the email. $mail->AddCC() is referencing a class that defines the real functions that operate the email. Without knowing what that is, this looks like it would work, assuming the class operates in a way that fits in with that-- but we'd need to see the class to know.

    However, you are not creating an array. You are creating a string.

    Change this: $ccsChecked = ""; to $ccsChecked = array();
    And this: $ccsChecked .= "$value2 "; to $ccsChecked[] = $value2;

    Now this: $ccAddresses = array($ccsChecked);
    can be just $ccAddresses = $ccsChecked, or, simpler, just use $ccAddresses throughout the script and skip $ccsChecked all together.

    One big problem with this is that you are not doing any validation, so people could be sending emails to anyone they want (spamming). So to fix this, you can verify the array against a real array of emails:
    PHP Code:
    $allowedEmails = array('1@1.1','2@2.2',....);
    foreach (
    $ccAddresses as $ccK=>$ccV) {
    if (!
    in_array($ccV,$allowedEmails)) { unset($ccAddresses[$ccK]); }

    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •