PHP form send email to multiple recipients

PHP form send email to multiple recipients

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP form send email to multiple recipients

The provided code snippet demonstrates a standard pattern for handling form submissions in PHP using the mail() function. While the logic is sound for sending a single notification, it falls short when you need to notify a distribution list or CC additional stakeholders. The core mechanism lies in how headers are constructed within the PHP script. To include multiple recipients, you must modify the $email_to variable and the Cc header string passed to the mail() function.

The original code initializes $email_to as a single string:

$email_to = "email@email.com";

PHP treats email addresses as strings regardless of how many are involved. To send to multiple recipients in the "To" field, you simply need to concatenate them with commas. It is critical to preserve the whitespace or use a specific separator format that your mail server expects. The standard RFC 5322 specification dictates that multiple recipients should be separated by commas followed by spaces. You can achieve this using str_replace() to join an array of addresses:

$recipients = ["email@email.com", "manager@company.com", "support@example.org"];
$email_to = implode(", ", $recipients);

However, the most common requirement in support systems or nomination forms is not just adding more people to the main recipient list, but copying a secondary group without them seeing each other's addresses. This requires adding a Cc header. In your existing code, you have defined $email_to and $email_subject. You need to create a separate variable for the carbon copy addresses and append it to the headers array before calling mail().

Here is how to modify the logic from your snippet to include both To and Cc recipients:

// Define primary recipient(s)
$email_to = "primary@email.com"; 

// Define secondary recipients (CC list)
$cc_emails = ["manager@company.com", "support@example.org"];

// Combine into a single string for the 'To' header if needed, 
// or keep it simple if only one primary contact exists.
if (!empty($cc_emails)) {
    $email_to .= ", " . implode(", ", $cc_emails);
} else {
    // Optional: If you prefer to keep headers distinct, you can build the To string separately.
    // But often, it's cleaner to just set the header explicitly below.
}

// ... validation logic remains unchanged ...

$email_message = "Form details below.\n\n";
$email_message .= "Name: ".clean_string($username)."\n";
$email_message .= "Comments: ".clean_string($body)."\n";

// Create email headers with Reply-To and Cc
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();

if (!empty($cc_emails)) {
    $headers .= "Cc: ".implode(", ", $cc_emails)."\r\n";
}

// Send the email
@mail($email_to, $email_subject, $email_message, $headers);

Technical Considerations and Pitfalls

When manipulating email headers in PHP, there are specific behaviors you must respect to ensure deliverability. The mail() function expects headers to end with a carriage return followed by a newline (\r\n). Using just \n might work on some local servers configured for Linux-style line endings, but Windows servers often require \r\n. The code snippet above uses \r\n explicitly to maintain compatibility across different server environments.

Another significant issue in the original snippet is the reliance on $_POST['email'] for both validation and the "From" header. If multiple users submit forms simultaneously, or if the server processes requests out of order due to caching issues (though rare with PHP's sequential execution), there is a risk of email spoofing if you do not verify that the sender has authenticated. While $_POST['email'] works for simple internal tools, production applications often prefer extracting the verified sender address from the session or using server-side variables like $SERVER['SMTP_AUTH_USER'] to prevent spoofing attacks.

Furthermore, the clean_string() function removes common email headers like "cc:" and "to:". This is a security best practice known as header injection protection. If a user inputs Cc: someone@evil.com into the message body, PHP will strip it. However, you must ensure this cleaning happens on all input fields before they are echoed or appended to email content, not just $username. The original code applies this to $body, which is correct for preventing injection attacks via the message content.

Modern Alternatives

While mail() is built into PHP and requires no installation, it relies on the server's system mail configuration (Sendmail or similar), which can be unreliable in shared hosting environments. If you are building a robust application, consider using a dedicated library like Laravel's Mail facade or a service like Postmark or SendGrid. These services provide better tracking, bounce handling, and HTML rendering capabilities that standard mail() often lacks.

For developers using the Laravel framework, sending emails to multiple recipients is abstracted away entirely. You would define your recipients in a configuration array or directly within the mail queue:

Mail::raw('Form submitted.', function ($message) use ($email_to, $cc_emails) {
    $message->to($email_to)->cc($cc_emails);
});

This approach ensures that headers are constructed correctly without manual string concatenation errors. However, for legacy scripts or simple shared-hosting environments where you must stick to the core PHP mail() function, understanding header manipulation and proper line ending usage is the key to expanding your form's reach to multiple recipients safely.

Our network