Need to install SendGrid in PHP App Service??

MAC 21 Reputation points
2021-10-06T22:20:45.407+00:00

I have a PHP App Service within which I'm trying to send a simple email using SendGrid (as instructed). SendGrid says to use their API. Do I need to install that within my App Service, or does it come pre-loaded?

I'm guessing the former, so how do I install the API as instructed here:
https://docs.sendgrid.com/for-developers/sending-email/v2-php-code-example
Using the console?

Obviously I'm very new to all of this so any basic guidance is very much appreciated!

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,933 questions
0 comments No comments
{count} votes

Accepted answer
  1. ajkuma 28,036 Reputation points Microsoft Employee Moderator
    2021-10-08T11:57:36.38+00:00

    @MAC , Apologies for the delay in responding here. Thanks for the good question.

    Yes, you need to install it. It is recommended to use the Composer.
    Edit the compose.json file located in the root folder of your website

    Kindly see this doc for some samples: https://github.com/sendgrid/sendgrid-php and simple sample below:

        // If you are using Composer (recommended)  
        //require 'vendor/autoload.php';  
           
        // If you are not using Composer  
        require("./sendgrid-php/<sendgrid-php.php>");  
           
        $from = new SendGrid\Email(null, "******@domain.com");  
        $subject = "Hello World from the SendGrid PHP Library!";  
        //$to = new SendGrid\Email(null, "******@domain.com");  
        $to = new SendGrid\Email(null, "******@domain.com");  
        $content = new SendGrid\Content("text/plain", "Hello, Email! This is test from user1");  
        $mail = new SendGrid\Mail($from, $subject, $to, $content);  
           
        //$apiKey = getenv('SENDGRID_API_KEY');  
        //$sg = new \SendGrid($apiKey);  
        $sg = new \SendGrid("xxxx");  
           
        $response = $sg->client->mail()->send()->post($mail);  
        echo $response->statusCode();  
        echo $response->headers();  
        echo $response->body();  
           
    

    Additionally,

    Twilio SendGrid's v3 APIs expect an API key to be passed in an Authorization header as a Bearer Token.
    The Twilio SendGrid helper libraries all provide a method to set your key, handling the authentication via Bearer Token for you.

    1. Sending Email with Microsoft Azure.
    2. Kindly take a look at enabling Composer and the steps outlined (it’s related to SengGrid, but similar steps need to be applied).
      Azure App Service Web Apps - Install Mail package using composer
    3. Configure a PHP app for Azure App Service for additional customization/configuration options.

    Kindly let us know if this helps or you need further assistance we will be more than happy to assist you.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. MAC 21 Reputation points
    2021-10-11T18:29:43.267+00:00

    I got it to work! After quite a bit of fiddling though...

    FYI, couldn't get your sample code to work, but I was able to use the sample code on the Integration page of SendMail's Dashboard:

    $email = new \SendGrid\Mail\Mail(); 
    $email->setFrom("test@example.com", "Example User");
    $email->setSubject("Sending with SendGrid is Fun");
    $email->addTo("test@example.com", "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";
    }
    

    Thanks much!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.