简体   繁体   中英

SendGrid: Missing argument 1 for SendGrid\Email::__construct()

I am sure this is a simple solution, I am attempting to set up a sendgrid to send confirmation emails out.

require 'sendgrid-php/vendor/autoload.php';
$sendgrid = new SendGrid($user,$pass);
$email    = new SendGrid\Email();

$email->addTo($sEmail)
      ->setFrom($email)
      ->setSubject("Sending with SendGrid is Fun")
      ->setHtml("and easy to do anywhere, even with PHP");

$sendgrid->send($email);

Upon execution of the code i get these error messages:

 Warning: Missing argument 1 for SendGrid\Email::__construct()

I am running PHP Version 5.6.16

I am sure i have missed something silly

There have been some updates to the SendGrid-PHP library. See the example from the readme : https://github.com/sendgrid/sendgrid-php

<?php
// If you are using Composer
require 'vendor/autoload.php';

// If you are not using Composer (recommended)
// require("path/to/sendgrid-php/sendgrid-php.php");

$from = new SendGrid\Email(null, "test@example.com");
$subject = "Hello World from the SendGrid PHP Library!";
$to = new SendGrid\Email(null, "test@example.com");
$content = new SendGrid\Content("text/plain", "Hello, Email!");
$mail = new SendGrid\Mail($from, $subject, $to, $content);

$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo $response->headers();
echo $response->body();

You get that error because of $email = new SendGrid\\Email(); which needs two parameters as seen here https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L845 which are $name and $email .

$name you can leave null if you don't want a From Name.

Also, the use of username and password like $sendgrid = new SendGrid($user,$pass); was deprecated, see here: https://github.com/sendgrid/sendgrid-php/blob/master/lib/SendGrid.php#L34

You need to create an API key and use it. See here : https://app.sendgrid.com/settings/api_keys

I was also tackling with similar kind of issue for send grid.

I got it worked by adding following header.

"Content-Type": "application/json; charset=utf8"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM