2026-07-15

PHP Mail, CC Field

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

PHP Mail, CC Field

Mastering Email Headers: How to Include a CC Field in Your PHP Mail Function

As senior developers, we often deal with the necessity of sending complex emails—those involving attachments, multiple recipients, and intricate formatting. While PHP’s built-in mail() function is deceptively simple, handling advanced features like Carbon Copy (CC) or Blind Carbon Copy (BCC) requires meticulous attention to the intricacies of RFC 5322 email standards.

If you are building a custom mail utility, as demonstrated by the function provided below, incorporating CC fields means correctly structuring the header lines so that the receiving Mail Transfer Agent (MTA) understands who should receive a copy of the message alongside the primary recipients.

This post will walk you through exactly how to modify your existing function to seamlessly include a CC field.

Understanding Email Headers and Recipient Fields

Email communication relies on a standardized set of headers to define the message's metadata, routing, and content type. When using PHP’s mail() function, we are essentially constructing this header block manually. To add recipients beyond the primary To: field, we need to insert the appropriate fields: Cc: for Carbon Copy and Bcc: for Blind Carbon Copy.

The key is ensuring these fields are placed correctly within the overall MIME structure you are building.

Modifying the PHP Function for CC Inclusion

Your existing function successfully handles multipart messages for attachments. To integrate a CC field, we need to introduce new parameters for the CC addresses and then append the corresponding header lines to the $header string before calling mail().

Here is the enhanced version of your function incorporating support for CC recipients:

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message, $cc = null) {
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);

    // Start building the base header
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";

    // --- CC Field Integration ---
    if ($cc !== null && !empty($cc)) {
        // Append the CC header line
        $header .= "Cc: " . $cc . "\r\n";
    }
    // -----------------------------

    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: text/html; charset=iso-8859-1\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";

    // Attachment part
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; 
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";

    $header .= "--".$uid."--";

    if (mail($mailto, $subject, "", $header)) {
        echo  "OK"; 
    } else {
        echo  "ERROR!";
    }
}

Explanation of Changes and Best Practices

  1. Adding the $cc Parameter: I introduced an optional parameter $cc = null to handle the CC address. This makes the function flexible, allowing it to work without a CC if none is provided.
  2. Conditional Header Insertion: The crucial step is the conditional check: if ($cc !== null && !empty($cc)). We only append the Cc: [address]\r\n line if we actually have a valid recipient address to send the copy to. This prevents sending malformed headers.
  3. RFC Compliance: By correctly placing the Cc: header before the main message body and attachment boundaries, we adhere more closely to established email protocols.

When developing robust systems for communication, especially when dealing with complex data structures like MIME messages, consider how modern frameworks handle these complexities. For example, developers building large-scale applications often leverage libraries that abstract away these low-level details, ensuring better security and maintainability than raw system calls alone. Frameworks like Laravel provide excellent tools for handling HTTP requests and integrating services, which can simplify the overall process of managing external communications securely and efficiently.

Conclusion

Including a CC field in a custom PHP mail function is fundamentally about correctly constructing the MIME headers. By treating the header string as structured data rather than plain text, you gain control over exactly what the receiving server interprets. Always validate inputs for recipient addresses before inserting them into headers to maintain high standards of email deliverability and security. Mastering these underlying protocols is what separates a functional script from a professional-grade application.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.