Jump to content

[SOLVED] mail() question regarding multiple recipients


dwest

Recommended Posts

I have this code:

 

$pdfu_subject = $_POST['pdfu_subject'];
                $pdfu_send_to = $_POST['pdfu_send_to'];
                $pdfu_support_email = $_POST['pdfu_support_email'];
                $pdfu_msg = $_POST['pdfu_msg'];
                $pdfu_msg = stripslashes($pdfu_msg);
                $pdfu_headers = 'MIME-Version: 1.0'."\r\n".
                'Content-type: text/html; charset=iso-8859-1'."\r\n".'From: '.
                $pdfu_support_email."\r\n".'Reply-To: '.$pdfu_support_email.
                "\r\n".'BCC: '.$pdfu_support_email."\r\n".'X-Mailer: PHP/' .
                phpversion();

                mail($pdfu_send_to, $pdfu_subject, $pdfu_msg, $pdfu_headers);
                echo 'The notification was sent successfully.';

 

In the form that feeds this I have a textbox where the recipient is entered.

If I enter a recipient, a comma, and another recipient, when I submit the form the mail is not processed.

If I enter a single recipient, all is well.

 

I need to be able to enter multiple recipients in the text box. I've tried comma, comma +space, etc. to no avail.

 

Can someone tell me what I'm doing wrong?

 

Thanks!

 

This should work for you...

 

this will separate by commas and remove whitespace from before and after each address

 


$pdfu_send_to = explode(",", $_POST['pdfu_send_to']);

$pdfu_subject = $_POST['pdfu_subject'];
$pdfu_support_email = $_POST['pdfu_support_email'];
$pdfu_msg = $_POST['pdfu_msg'];
$pdfu_msg = stripslashes($pdfu_msg);
$pdfu_headers = 'MIME-Version: 1.0'."\r\n".
'Content-type: text/html; charset=iso-8859-1'."\r\n".'From: '.
$pdfu_support_email."\r\n".'Reply-To: '.$pdfu_support_email.
"\r\n".'BCC: '.$pdfu_support_email."\r\n".'X-Mailer: PHP/' .
phpversion();

foreach($pdfu_send_to as $var => $val){

  $pdfu_send_to = ltrim(rtrim($val));
  mail($pdfu_send_to, $pdfu_subject, $pdfu_msg, $pdfu_headers);

}



Thanks for the reply!

Unfortunately is still isn't working and won't even work with a single sent to address now. Most likely, I've got something incorrect in the copy paste process.

 

Here is what I have:

$pdfu_send_to = explode(",", $_POST['pdfu_send_to']);
$pdfu_subject = $_POST['pdfu_subject'];
$pdfu_support_email = $_POST['pdfu_support_email'];
$pdfu_msg = $_POST['pdfu_msg'];
$pdfu_msg = stripslashes($pdfu_msg);
$pdfu_headers = 'MIME-Version: 1.0'."\r\n".
'Content-type: text/html; charset=iso-8859-1'."\r\n".'From: '.
$pdfu_support_email."\r\n".'Reply-To: '.$pdfu_support_email.
"\r\n".'BCC: '.$pdfu_support_email."\r\n".'X-Mailer: PHP/' .
phpversion();

foreach($pdfu_send_to as $var => $val){

$pdfu_send_to = ltrim(rtrim($val));
mail($pdfu_send_to, $pdfu_subject, $pdfu_msg, $pdfu_headers);

}
echo 'The notification was sent successfully.';

 

If you don't see anything obvious, tell me what to do to get the error to echo and I'll post the error.

 

Thanks!

 

 

error_reporting(E_ALL);

$addresses = $_POST['pdfu_send_to'];

$pdfu_send_to = explode(',', $addresses);

$pdfu_subject = $_POST['pdfu_subject'];
$pdfu_support_email = $_POST['pdfu_support_email'];
$pdfu_msg = $_POST['pdfu_msg'];
$pdfu_msg = stripslashes($pdfu_msg);
$pdfu_headers = 'MIME-Version: 1.0'."\r\n".
'Content-type: text/html; charset=iso-8859-1'."\r\n".'From: '.
$pdfu_support_email."\r\n".'Reply-To: '.$pdfu_support_email.
"\r\n".'BCC: '.$pdfu_support_email."\r\n".'X-Mailer: PHP/' .
phpversion();

foreach($pdfu_send_to as $var => $val){

$address = ltrim(rtrim($val));
if(mail($address, $pdfu_subject, $pdfu_msg, $pdfu_headers)){
  echo "Mail to $address successful.<br/>";
}

}
echo 'The notification was sent successfully.';

 

see if that works..

That seems to work! Thanks!

However, now my email address validator throws an error because there are oddities in the text box for the recipient. :P

 

Here is the validator code:

function is_valid_email_address($email)
{
    $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
    $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
    $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
    '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
    $quoted_pair = '\\x5c[\\x00-\\x7f]';
    $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
    $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
    $domain_ref = $atom;
    $sub_domain = "($domain_ref|$domain_literal)";
    $word = "($atom|$quoted_string)";
    $domain = "$sub_domain(\\x2e$sub_domain)*";
    $local_part = "$word(\\x2e$word)*";
    $addr_spec = "$local_part\\x40$domain";
    return preg_match("!^$addr_spec$!", $email) ? 1 : 0;
}

 

Can something be adjusted to allow for the multiple addresses in the text box?

 

Thanks so much for your help on this!

just run your validator inside a foreach loop, after exploding the variable just like i did

 

if the validation is before the mail, which it obviously is, then put this

 

$addresses = $_POST['pdfu_send_to'];

$pdfu_send_to = explode(',', $addresses);

 

before the validator function and run the validator inside the loop

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.