10

I'm using sendgrid with php, I've used both options the client library and the curl option. So far, I've been able to send emails directly with the addTo option with no problem. But when I try to add Cc or Bcc options, the email still gets sent but the copies are never delivered. Are there any known issues with the php version? In other project the java library works just fine.

Here is a simple piece of code I'm trying to make it work

<?php
require ('sendgrid/sendgrid-php.php');

$sendgrid = new SendGrid('user', 'pwd');

$mail = new SendGrid\Email();
$mail ->addTo("[email protected]");
$mail ->addCc("[email protected]");
$mail ->setFrom("[email protected]");
$mail ->setSubject("TEST");
$mail->setHtml("<h1>Example</h1>");
$sendgrid->send($mail);

?>
Ajoy
  • 1,838
  • 3
  • 30
  • 57
Mike
  • 485
  • 1
  • 4
  • 14

2 Answers2

16

The documentation does not seem to have addCc method. You can try these alternatives.

$mail = new SendGrid\Email();
$mail->addTo('[email protected]')->
       addTo('[email protected]')->
       addTo('[email protected]');

or

$mail   = new SendGrid\Email();
$mail->addBcc('[email protected]');
$sendgrid->send($mail);

https://github.com/sendgrid/sendgrid-php#bcc

  • Multiple addTo does the trick to mimic the Bcc, however I need the emails to be sent with their respective Cc and Bcc. The method addCc does not appear on the documentation but it does on the library. – Mike Apr 08 '14 at 21:37
  • They have removed addCc from the documentation. https://github.com/sendgrid/sendgrid-php/commit/7d5e428321db6419829eb52a206364b072182d81 –  Apr 08 '14 at 21:40
  • You can also chk this issue here. https://github.com/sendgrid/sendgrid-php/issues/83 –  Apr 08 '14 at 21:42
  • 1
    That is definitely good to know. Thanks, I guess I'll take the phpMailer approach for this – Mike Apr 08 '14 at 22:32
0
Find script with CC and BCC 
$email = new \SendGrid\Mail\Mail();
$email->setFrom("[email protected]", "Example User");
$email->setSubject("Sending with Twilio SendGrid is Fun");
$email->addTo("[email protected]", "Example User");
$email->addCc("[email protected]", "Example User");
$email->addBcc("[email protected]", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
    "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
}