簡體   English   中英

如何使用發送網格從 php 發送電子郵件?

[英]How to send email using send grid from php?

我想從php發送郵件。 我正在嘗試使用發送網格。 我跟着這個鏈接: https : //github.com/sendgrid/sendgrid-php

我試圖遵循每一步。 我已經下載了不使用作曲家的庫。 所以在php中添加了這個庫並編寫了示例代碼。

但是我在郵遞員中沒有得到任何響應或任何東西,試圖返回請求正文,響應試圖回顯變量,但沒有得到任何結果。

    <?php

namespace SendGrid;
//require 'vendor/autoload.php';
require("C:\Users\Siddhi\Downloads\sendgrid-php\sendgrid-php");

class SendEmail
{
    private $db;

    function SendEmail($database){
        $this->db = $database;
    }

    function helloEmail()
    {
        $from = new Email(null, "siddhijambhale@gmail.com");
        $subject = "Hello World from the SendGrid PHP Library";
        $to = new Email(null, "siddhijambhale@gmail.com");
        $content = new Content("text/plain", "send grid test email");
        $mail = new Mail($from, $subject, $to, $content);
        $to = new Email(null, "siddhijambhale@gmail.com");
        $mail->personalization[0]->addTo($to);
        //echo json_encode($mail, JSON_PRETTY_PRINT), "\n";

        echo $to;
        return $mail;
    }

    function sendHelloEmail()
    {
        $apiKey = getenv('PUT-KEY-HERE');
        $sg = new SendEmail($apiKey);
        $request_body = $this->helloEmail();
        $response = $sg->client->mail()->send()->post($request_body);
        echo $response->statusCode();
        echo $response->body();
        echo $response->headers();

        return $request_body;
    }
}

我還嘗試了使用 composer 鏈接中顯示的第二種方式。 所以我給出了一個作曲家的路徑。

 <?php

namespace SendGrid;
require 'C:/Program Files (x86)/Ampps/www/testslim/v1/src/vendor/autoload.php';
//require("C:/Users/Siddhi/Downloads/sendgrid-php/sendgrid-php");

class SendEmail
{
    private $db;


    function sendEmail($database) {
        $this->db = $database;
    }

    function helloEmail()
    {
        $from = new Email(null, "siddhijambhale@gmail.com");
        $subject = "Hello World from the SendGrid PHP Library";
        $to = new Email(null, "siddhijambhale@gmail.com");
        $content = new Content("text/plain", "send grid test email");
        $mail = new Mail($from, $subject, $to, $content);
        $to = new Email(null, "siddhijambhale@gmail.com");
        $mail->personalization[0]->addTo($to);
        //echo json_encode($mail, JSON_PRETTY_PRINT), "\n";

        echo $from.$to;

        return $mail;
    }

    function sendHelloEmail()
    {
        $apiKey = getenv('PUT-KEY-HERE');
        $sg = new SendEmail($apiKey);
        echo $apiKey;
        $request_body = $this->helloEmail();
        $response = $sg->client->mail()->send()->post($request_body);
        echo $response->statusCode();
        echo $response->body();
        echo $response->headers();

        return $request_body;
    }
}

通過這種方式,它也沒有給出任何結果,它也阻止了我的其他 url,它們沒有給出輸出。

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';
include '../classes/CustomerOrders.php';
include '../classes/ActivatedMerchants.php';
include '../classes/UserAuthentication.php';
include '../classes/UserActivationItem.php';
include '../classes/CustOtpConfirmation.php';
include '../classes/CustomerRegistrationItems.php';
include '../classes/DeviceToken.php';
include '../classes/SearchMerchants.php';
include '../classes/SendActivationRequest.php';
include '../classes/CustomerBills.php';
include '../classes/CustomerRegistration.php';
include '../classes/ItemsUnits.php';
include '../classes/SendEmail.php';

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
ini_set('display_errors', '1');

$config['displayErrorDetails'] = true;
$config['addContentLengthHeader'] = false;

$config['db']['host']   = "localhost";
$config['db']['user']   = "kiranadb";
$config['db']['pass']   = "kirana@12345";
$config['db']['dbname'] = "kiranadb";


$app = new \Slim\App(["settings" => $config]);
$container = $app->getContainer();

$container['logger'] = function($c) {
    $logger = new \Monolog\Logger('my_logger');
    $file_handler = new \Monolog\Handler\StreamHandler("../logs/app.log");
    $logger->pushHandler($file_handler);
    return $logger;
};

$container['db'] = function ($c) {
    $db = $c['settings']['db'];
    $pdo = new PDO("mysql:host=" . $db['host'] . ";dbname=" . $db['dbname'],
        $db['user'], $db['pass']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $pdo;
};


$app->get('/getcustorders/[{orderFrom}]', function ($request, $response, $args) {
    $headers = apache_request_headers();
    $customerOrders=new CustomerOrders($this->db);
    $result= $customerOrders->fetchOrders($args['orderFrom'],$headers['Authorization']);
    return $this->response->withJson($result);
});


$app->post('/confirmCustomerOTP', function ($request, $response) {

    $input = $request->getParsedBody();
    $data = [];
    $data['otp'] = filter_var($input['otp'], FILTER_SANITIZE_STRING);
    $data['email_id'] = filter_var($input['email_id'], FILTER_SANITIZE_STRING);
//    $activateUser=new CustomerRegistrationItems($data);
    $custOtpConfirmation=new CustOtpConfirmation($this->db);
    $result= $custOtpConfirmation->activateUserStatus($input['otp'],$input['email_id']);
    return $response = $response->withJson($result);
});

$app->post('/sendCustomerOTP', function ($request, $response) {

    $input = $request->getParsedBody();
    $reg_data = [];
    $reg_data['phone_no'] = filter_var($input['phone_no'], FILTER_SANITIZE_STRING);
    $reg_data['email_id'] = filter_var($input['email_id'], FILTER_SANITIZE_STRING);
   // $mobileno=new CustomerRegistrationItems($reg_data);
    $custOtpConfirmation=new CustOtpConfirmation($this->db);
    $result= $custOtpConfirmation->sendSms($input['phone_no'],$input['email_id']);
    return $response = $response->withJson($result);
});

$app->get('/getactivatedmerchants/[{customer_id}]', function ($request, $response, $args) {

    $headers = apache_request_headers();
    $activatedMerchants=new ActivatedMerchants($this->db);
    $result= $activatedMerchants->fetchMerchants($args['customer_id'],$headers['Authorization']);
    return $this->response->withJson($result);
});

$app->post('/getSearchedMerchants', function ($request, $response, $args) {

    $input = $request->getParsedBody();
    $headers = apache_request_headers();
    $searchMerchant = new SearchMerchants($this->db);
    $result= $searchMerchant->fetchMerchants($input['customer_id'],$headers['Authorization'],$input['latitude'],$input['longitude']);
    return $this->response->withJson($result);
});



$app->post('/sendMail', function ($request, $response, $args) {
    $headers = apache_request_headers();
    $input = $request->getParsedBody();
    $sendMails=new \SendGrid\SendEmail($this->db);
    $result = $sendMails->sendHelloEmail();
    return $this->response->withJson($result);
});

$app->run();

這是 api1.php,它有 sendEMail 的 url。 我在 php 中使用超薄框架。

怎么了?? 請幫忙..謝謝..

一、換行

require("C:\Users\Siddhi\Downloads\sendgrid-php\sendgrid-php");

require("C:\Users\Siddhi\Downloads\sendgrid-php\sendgrid-php.php");

現在它將是庫索引文件的正確絕對路徑。

注意:但是,您應該避免使用絕對路徑。

在這之后,改變

$sg = new SendEmail($apiKey);

$sg = new \SendGrid($apiKey);

因為它是正確的 SendGrid 實例,所以您必須通過。 目前,您正在使用 API 密鑰創建 SendEmail 實例。

現在,它應該工作。

這是使用 sendgrid 的基本發送

$sendgrid = new SendGrid($key);
$email = new SendGrid\Email();
$email
  ->addTo($to,$toName)
  ->setFrom($from)
  ->setFromName($fromName)
  ->setSubject($subject)
  ->setText('Hello World!')
  ->setHtml($html);

try {
  $sendgrid->send($email);
} catch(\SendGrid\Exception $e) {
}

修改這一行

require("C:\Users\Siddhi\Downloads\sendgrid-php\sendgrid-php");

require("C:/Users/Siddhi/Downloads/sendgrid-php/sendgrid-php");

這里也可能有錯別字

require("C:/Users/Siddhi/Downloads/sendgrid-php/sendgrid-php");
                                // should this be a dot ^

請記住,DOS 反斜杠是轉義字符,不應在任何 PHP 上使用。 Windows 上的 PHP 將自動將 Unix 正斜杠轉換為 Windows 系統上所需的內容。

但是我不得不說,如果沒有其他原因,您應該將此代碼移動到您的 Web 服務器文件夾中,而不是將它放在C:users ,當您將其復制到實時主機時,這會讓您感到困惑,您會忘記移動此代碼,但更重要的是,此數據結構在 unix 操作系統上不存在,並且在 Windows 服務器上不可用

試試這個它會起作用:

    <?php

namespace SendGrid;
//require 'vendor/autoload.php';
require("C:\Users\Siddhi\Downloads\sendgrid-php\sendgrid-php.php");
class SendEmail
{
    private $db;

    function SendEmail($database){
        $this->db = $database;
    }

    function helloEmail()
    {
        $from = new Email(null, "siddhijambhale@gmail.com");
        $subject = "Hello World from the SendGrid PHP Library";
        $to = new Email(null, "siddhijambhale@gmail.com");
        $content = new Content("text/plain", "send grid test email");
        $mail = new Mail($from, $subject, $to, $content);
        $to = new Email(null, "siddhijambhale@gmail.com");
        $mail->personalization[0]->addTo($to);
        //echo json_encode($mail, JSON_PRETTY_PRINT), "\n";

        echo $to;
        return $mail;
    }

    function sendHelloEmail()
    {
        $apiKey = getenv('PUT-KEY-HERE');
        $sg = new \SendGrid($apiKey);
        $request_body = $this->helloEmail();
        $response = $sg->client->mail()->send()->post($request_body);
        echo $response->statusCode();
        echo $response->body();
        echo $response->headers();

        return $request_body;
    }
}

暫無
暫無

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

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