簡體   English   中英

如何通過php正確發送sendgrid電子郵件

[英]How to properly send email with sendgrid via php

我有下面的代碼來自sendgrid用於發送帶有我的API密鑰的電子郵件但我無法讓它工作,因為它顯示

錯誤致命錯誤:未捕獲錯誤:在...中調用字符串上的成員函數send()

我根據需要安裝了composer

看來,但是, $sendgrid->send($email)實際上並沒有返回任何內容,因此$ mail是一個空變量。 關於如何解決這個問題的任何想法。

Api鏈接

<?php

// https://sendgrid.com/docs/API_Reference/index.html

// using SendGrid's PHP Library
// https://github.com/sendgrid/sendgrid-php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("./sendgrid-php.php");
// If not using Composer, uncomment the above line

$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('my api goes here'));
$sendgrid = 'my api goes here';
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";
}

您正在嘗試在字符串上調用函數。

$sendgrid = 'my api goes here';

然后:

$response = $sendgrid->send($email);

我認為你的意思是:

$response = $email->send($email);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM