0

My following script runs fine when a single email address is entered into the textarea, but once two are entered, no emails are sent. What am I doing wrong?

if($_POST['submit']=='Send Email') {

$email_addresses = explode(",\n", $_POST['email']);

foreach($email_addresses as $email_address){

$email_address = trim($email_address);

send_mail(  '[email protected]',
                        $email_address,
                        'Test Email',
                        "Hello This Email Is A Test");

}


}

var_dump($email_addresses) results in this

array(1) { [0]=> string(39) "[email protected] [email protected]" }
1
  • print output of $_POST['email'] and check is it separated by \n Commented Aug 3, 2011 at 5:02

4 Answers 4

6

You're using the same variable name twice

foreach($email_addresses as $email_addresses)

so on the second loop, the source is overwritten

Edit:

please post the output of

var_dump($_POST['email']);
var_dump(explode(",\n", $_POST['email']));
Sign up to request clarification or add additional context in comments.

2 Comments

Even when foreach($email_addresses as $email_address){ I have the same problem.
fix the question to reflect this change. That way we know for sure you get it.
2

It should be:

foreach($email_addresses as $email_address){

$email_address = trim($email_address);

send_mail(  '[email protected]',
                        $email_address,
                        'Test Email',
                        "Hello This Email Is A Test");

}

Also splitting using explode on ",\n" separator isn't a good idea (people can send ",\r\n" in some cases). Provide multiple fields or use preg_split().

If it doesn't work try var_dump($email_address); after the explode() function to get the information what exactly happens with the input (and so you can see that input is actually correct).

UPDATE: As you can clearly see there is no \n in $email_address. It depends on your HTML form.

For a quick fix just explode(', ', $email_addresses); Also - you missed , in your input, which you require to explode that string.

3 Comments

Thanks, I've tried doing foreach($email_addresses as $email_address){ and no emails get sent.
Could you update your question with the var_dump() result for misbehaving case?
I've updated the question with the var_dump() after the explode()
1

obviously [email protected] [email protected] is not a valid email, and my psychic powers tell me that this is not the correct way to do this. What if I enter something else than an email address? Try to sanitise the data you receive from user. You can not trust user input blindly.

7 Comments

This is the just condensed version of what I have, I run other checks on the emails to prevent abuse.
Good for your code and the one who will be maintaining it. If I could design a webpage where user is just asked to click and select instead of entering I will still run sanitation check on server side.
This email script is for a referral program for only registered users, if someone is abusing it they need a verified account and logged in. It's not just something that the public will have access to.
Well, out of the context of your real problem, the first commandment of handling user input is, "Thy shalt never trust user input." even for registered users.
It's a little more locked down of a system than you think, but thanks for reminding me.
|
1
foreach($email_addresses as $email_addresses){ 

Means that you are overwriting the source array ($email_addresses) with the first entry in the array, because they are the same variable name. Unfortunately, PHP throw an error on this, so you end up rewriting your array with the first email address, and then prematurely (to your needs) exiting the loop (though this is expected and logical behaviour).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.