phpemailmailgunbcc

How to send mail only to bcc with mailgun php API?


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);

Solution

  • 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