11

I am trying to send mail only to Bcc but unable to send. Code given below is working fine with To and Bcc but when i try to send only with Bcc it fails. I tried passing empty string with To but didnt work. I am using mailgun php API.

function send_mail($email,$subject,$msg,$bcc)
{
    $api_key="";
    $domain ="";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, 'api:'.$api_key);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/'.$domain.'/messages');
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'from' => 'Example <examle@examle.com>',
    'to' => $email,
    'bcc' => $bcc,
    'subject' => $subject,
    'html' => $msg,
    'o:tracking' => true)); 
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
send_mail($email, $subject, $msg, $bcc);
niteshd22
  • 510
  • 4
  • 15
  • 1
    The usual solution is to put the senders address in `to`. – Klas Lindbäck Feb 13 '15 at 15:37
  • 2
    An email without a TO is invalid. You must have a TO address. As @KlasLindbäck mentioned, usually you use your own FROM address as the TO. That way the email is FROM: You and TO: You with the BCC addresses of your target emails. – greg_diesel Feb 13 '15 at 20:00

1 Answers1

11

You may not send mail only using bcc. There is a trick I use in which I make the from and to the same address (something like info@mydomain.com) and then fill up the bcc slot with whatever I need.

You may send mail using mailing lists that does not require you to expose other email addresses. https://documentation.mailgun.com/en/latest/api-mailinglists.html

Nathaniel Johnson
  • 4,731
  • 1
  • 42
  • 69