Ben Darlington
Ben Darlington

Reputation: 161

Mail () BCC - can't get it to work

I'm having a bit of trouble creating a mail function that sends form result - some calculations - to the user. It was working fine until I added a bcc header and now no email gets sent.

The pertinent code is below and the page can be seen here: http://benefacto.org/calculator/

This is my first major php project!

Here is my mail function:

if($_POST["submit1"]) {
    $recipient=$_POST["email"] . ",[email protected]";
    $subject="Volunteering Costs at $companyname";
    $sender=$_POST["sender"];
    $senderEmail="[email protected]";
    $message=round($recturnoverdcrease,3) . "%";
    $message1=$_POST["companyname"];
    $message2=round($totalcost,2);
    $message3=round($increaserec,3) . "%";
    $themodel='http://benefacto.org/wp-content/uploads/BNFO_CostCalculator_BD_v1.0_160127.xlsx';
    $headers='Bcc: [email protected]' . "\r\n";

    // Email Message
    $mailBody="Volunteering at $message1\n\n
        Outputs: \n\n

        Productivity Gain Needed Amongst staff to cover cost: $message3 \n
        Decrease in Staff Turnover Required to Offset Cost:$message \n
        Total Cost £$message2 \n

        Inputs: \n\n

        Your Company's UK Headcount: $valuea\n
        Average Salary at your Company: $value \n
        Your Company's UK Operating Profit: $valueb\n
        Brokerage Costs: $valuec\n
        Anticipated Uptake: $valued\n

        Download the model here: $themodel \n\n
        ";

    mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>", $headers);
}

Upvotes: 0

Views: 24

Answers (1)

martinczerwi
martinczerwi

Reputation: 2847

If you look at the docs (http://php.net/manual/de/function.mail.php), mail accepts headers as the 4th parameter. Your 4th parameter is the "From"-line. The fifth parameter is passed to the mail binary (sendmail or whatever, generally you don't need to mess with it).

You need to append the BCC line to the FROM line (both are headers). Try this:

$headers  = "From: $sender <$senderEmail>\r\n"
$headers .= "Bcc: [email protected]\r\n";

// ...

mail($recipient, $subject, $mailBody, $headers);

Upvotes: 1

Related Questions