Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6598501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:21:24+00:00 2026-05-25T18:21:24+00:00

I’m using PHP’s Mail function to send emails to individuals. I wanted an easy

  • 0

I’m using PHP’s Mail function to send emails to individuals. I wanted an easy way to customize the greeting name in the email, so i created a form where there is a comma separated list of emails and a comma separated list of names, and they get posted and mailed via PHP.

However, every time I send it, the name spits out the entire array of names, and not the ONE value I specify. I’ve been working on this for an hour now and cannot for the life of me see where the problem is.. its such simple code! Here is the code let me know if you see any glaring errors.

$to_emails = $_POST['to_emails'];
$to_names = $_POST['to_names'];
$from_email = $_POST['from_email'];
$message = $_POST['message'];
$subject = $_POST['subject'];

$explode_emails = str_replace(" ", "", explode(",",$to_emails));    
$explode_names = explode(",",$to_names);

$email_array = array();

$i=0;
foreach($explode_names as $e) {
    $name = $e;
    $to      = $explode_emails[$i];
    $subject = $subject;
    $message = $name.",\r\n".$message;
    $headers = 'From: '.$from_email . "\r\n" .
        'Reply-To: '.$from_email . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    mail($to, $subject, $message, $headers);        
    echo "<font color='#FF0000'>".$i." Emailed: ".$name." : ".$to."</font><br />";
    $i++;

}


<form method="post">
To Emails (comma separated)
<input type="text" name="to_emails" value="">
<br /><br />
To Names (comma separated)
<input type="text" name="to_names" value="">
<br /><br />


From
<input type="text" name="from_email" value="trgentes@gmail.com">
<br /><br />

Subject
<input type="text" name="subject" value="">
<br /><br />

Message
<textarea name="message" rows="5" id="message"></textarea>
<br /><br />

<input type="submit" />

</form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:21:25+00:00Added an answer on May 25, 2026 at 6:21 pm

    Here’s what I came up with. I’m not sure if this exactly answers what you want, but it would send email to all the people specified:

    $to_emails = $_POST['to_emails'];
    $to_names = $_POST['to_names'];
    $from_email = $_POST['from_email'];
    $message = $_POST['message'];
    $subject = $_POST['subject'];
    
    $explode_emails = array_map('trim', explode(",", $to_emails));
    $explode_names = array_map('trim', explode(",",$to_names));
    
    $recipients = array_combine($explode_emails, $explode_names);
    
    $i=0;
    foreach($recipients as $email => $name) {
        $headers = 'From: ' . $from_email;
        $headers .= "\r\nReply-To: " . $from_email;
        $headers .= "\r\nX-Mailer: PHP/" . phpversion();
    
        $finalMessage = $name . ",\r\n" . $message;
    
        mail($email, $subject, $finalMessage, $headers);        
    
        echo "<font color='#FF0000'>" . $i . " Emailed: " . $name . " : " . $email . "</font><br />";
    
        $i++;
    }
    

    Here’s a quick rundown of the changes I made:

    1. str_replace() wasn’t really needed. trim() is a more efficient function to trim leading or trailing whitespace.

    2. I merged the email addresses and their corresponding names into an associative array called $recipients. This is easier to keep track of than referencing them by a numeric key, and actually associates the data to each other. This also makes looping over them easier to read.

    3. I simplified how headers are created. It’s easier to add the carriage return \r\n at the start of the string, making it easier to read, and less likely to forget one, or leave an extra return on the end.

    While the above code will work, I’ll also take this opportunity to also warn you of a vulnerability present in your current code: Email Header Injection.

    If an attacker submits content to your form containing the “\r\n” character, they can inject their own headers. Meaning, they could end up sending this email to more people than you want it to go to, or they may inject their own custom body message.

    This example should be a good example for you on how to prevent this kind of attack, as its code example highly resembles yours.

    You should NEVER trust input sent via $_POST. You should validate the submitted data is in the correct format you expect and does not contain any malicious characters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I would like to count the length of a string with PHP. The string
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.