sendgrid / sendgrid-php

The Official Twilio SendGrid PHP API Library
https://sendgrid.com
MIT License
1.49k stars 624 forks source link

Unable to send complete web template via Email in V3 version #834

Closed marksingh07 closed 4 years ago

marksingh07 commented 5 years ago

I am using V3 version and trying to send email its working with single string via - addcontent - function but need to send dynamic web template getting 400 error.

earlier We were using old code was working fine suddenly stops working, no error reporting and email are being sent . Earlier used code is

$subject = "Newsletter"; $from = new SendGrid\Email('From', $FROM_EMAIL); foreach( $To_email as $emails){
$to = new SendGrid\Email($To_name[$i], $emails);
// Create Sendgrid content $content = new SendGrid\Content("text/html",$message); // Create a mail object $mail = new SendGrid\Mail($from, $subject, $to, $content);

$sg = new \SendGrid("**");

  $i++; }

      $response = $sg->client->mail()->send()->post($mail);
if ($response->statusCode() == 202) {
 // Successfully sent
echo 'done';
} else {
 echo 'false';

}

thinkingserious commented 5 years ago

Hello @marksingh07,

Here is an example of sending with our new dynamic templates (using v7.3.0 of this library). Please take a look and let me know if it helps.

With Best Regards,

Elmer

marksingh07 commented 5 years ago

I tried and getting permission denied error. I am using legacy template example for multiple users. I checked and pasted Api key many times to make sure its correct, and using same template d as given in example.

401 Array ( [0] => HTTP/1.1 401 Unauthorized [1] => Server: nginx [2] => Date: Mon, 20 May 2019 07:17:04 GMT [3] => Content-Type: application/json [4] => Content-Length: 88 [5] => Connection: keep-alive [6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io [7] => Access-Control-Allow-Methods: POST [8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl [9] => Access-Control-Max-Age: 600 [10] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html [11] => [12] => ) {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]

marksingh07 commented 5 years ago

Now we are able to send mails with html template, but one thing needs to every emails is being set in CC but we don't want to show any user any other person's email.

thinkingserious commented 5 years ago

Hello @marksingh07,

I believe this is the example you need. Please give it a try and let me know.

Thanks!

With Best Regards,

Elmer

marksingh07 commented 5 years ago

Thank you for reply @thinkingserious , but it's not working as required we need to send email without showing CC to any other user, but example given in earlier reply is sending email as CC. Thank you

thinkingserious commented 5 years ago

Hello @marksingh07,

I believe in that case you will want to build the Mail object yourself, making sure that each CC object is within its own Personalization object.

With Best Regards,

Elmer

marksingh07 commented 5 years ago

Thank you for your reply, Sorry I may be not asking clear question, I am trying

I need to send dynamic html template or data to multiple users with BCC emails examples I tried given by you its sending mail very perfectly but going with CC, so it's not required Required only to send html template with BCC emails.

Last example is not sending html template.

Thank you

thinkingserious commented 5 years ago

Hello @marksingh07,

Please let me make sure I understand by verifying each point:

  1. You have an email to send which has one To address and multiple BCC addresses.
  2. You want to apply a dynamic template to the email above that works on both the To and BCC addresses.
  3. You want to ensure the BCC emails are not seen by other recipients.

With Best Regards,

Elmer

marksingh07 commented 5 years ago

Thank you for reply @thinkingserious

yes, you are right we want same thing as explained by you

Thank you

thinkingserious commented 5 years ago

OK, this is the code that worked for me, could you please give it a try? Thanks!

<?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases

use \SendGrid\Mail\From as From;
use \SendGrid\Mail\To as To;
use \SendGrid\Mail\Bcc as Bcc;
use \SendGrid\Mail\Mail as Mail;

$from = new From("dx@sendgrid.com", "DX");
$tos = [
    new To(
        "personal@example.com",
        "Personal",
        [
            'subject' => 'Subject 1',
            'firstname' => 'Example User 1',
            'location' => 'Denver'
        ]
    )
];
$email = new Mail(
    $from,
    $tos
);
$email->addBcc(new Bcc("ethomas@twilio.com", "Twilio"));
$email->setTemplateId("d-142c5c8d375041b6bd2efd5d794db692");

$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";
}