【问题标题】:PHP - Simple SendGrid Fatal error: Class 'SendGrid\Email' not foundPHP - 简单的 SendGrid 致命错误:找不到类“SendGrid\Email”
【发布时间】:2014-08-25 08:25:34
【问题描述】:

我正在尝试使用 php sendgrid 从联系表单发送电子邮件。我只需要最简单的实现,并遵循此处指示的基本步骤:https://github.com/sendgrid/sendgrid-php

这是我的代码:

require_once 'sendgrid-php/lib/SendGrid.php';
require_once 'unirest-php/lib/Unirest.php';

function spamcheck($field) {
    $field = filter_var($field, FILTER_SANITIZE_EMAIL);

    if (filter_var($field, FILTER_VALIDATE_EMAIL)) {
        return TRUE;
    } else {
        return FALSE;
    }
}

if (isset($_REQUEST['email'])) {
    $mailcheck = spamcheck($_REQUEST['email']);

    if ($mailcheck == FALSE) {
        echo "Invalid Input";
    } else {        

        $email = $_REQUEST['email'];
        $name = $_REQUEST['name'];
        $company = $_REQUEST['company'];
        $message = $_REQUEST['message'];
        $subject = "This is the subject";
        $recipient = "test@email.com"; // Not the real value
        $username = 'test'; // Not the real value
        $password = '1234'; // Not the real value

        $sendgrid = new SendGrid($username, $password);     
        $mail = new SendGrid\Email();

        $body = "Name: " . $name . "\n" .
                "Company: " . $company . "\n" .
                "Message: " . $message;

        $mail->addTo($recipient)->
            setFrom($email)->
            setSubject($subject)->
            setText($message);

        $sendgrid->smtp->send($mail);

        echo 'SUCCESS';

    }
} else {
    echo "Fail";
}

我收到了这个错误:

<br />
<b>Fatal error</b>:  Class 'SendGrid\Email' not found in <b>/usr/local/www/vhosts/.../intl/mail.php</b> on line <b>27</b><br />

另外,当我尝试将其添加到我的代码中时(如上面的链接所示):

SendGrid::register_autoloader();

我收到此错误:

<br />
<b>Warning</b>:  require_once(swift_required.php): failed to open stream: No such file     or directory in <b>/usr/local/www/vhosts/.../intl/sendgrid-    php/lib/SendGrid/Smtp.php</b> on line <b>25</b><br />
<br />
<b>Fatal error</b>:  require_once(): Failed opening required 'swift_required.php'  (include_path='.:/usr/local/share/pear') in <b>/usr/local/www/vhosts/.../intl/sendgrid-   php/lib/SendGrid/Smtp.php</b> on line <b>25</b><br />

^ 基于此,我不会使用 swift mailer,所以它可能不相关。

我错过了什么?

*PHP 版本 5.4.21 *SendGrid 1.1.6 版

【问题讨论】:

  • PHP U 使用什么版本?需要什么版本的 SendGrid 库?
  • 对不起。我现在包含了这些版本。
  • 尝试在require_once '/path/to/unirest-php/lib/Unirest.php'; 之前设置 - 例如先行
  • 对不起,仍然无法正常工作,同样的错误。
  • SendGrid::register_autoloader(); 你在哪里包含这个?

标签: php sendgrid


【解决方案1】:

我没有安装swiftmailer,但是我安装了smtpapi-php和unirest-php

https://github.com/Mashape/unirest-php

https://github.com/sendgrid/smtpapi-php

我的测试代码如下所示:

require_once ('../includes/sendgrid-php/lib/SendGrid.php');
require_once ('../includes/smtpapi-php/lib/Smtpapi.php');
require_once ('../includes/unirest-php/lib/Unirest.php');

SendGrid::register_autoloader();
Smtpapi::register_autoloader();

    $sendgrid = new SendGrid('user', 'pass');
    $email    = new SendGrid\Email();
    $email->addTo( $to)->
           setFrom($from)->
           setSubject($subject)->
           setText('Test')->
           setHtml($html);

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

var_dump($x );

它适用于 php 5.4

【讨论】:

    【解决方案2】:

    由于您使用的是SMTP发送方式:

    $sendgrid->smtp->send($mail);
    

    您需要安装 swiftmailer。相关说明在这里:https://github.com/sendgrid/sendgrid-php#optional

    您的另一个选择是使用网络发送 - 这实际上是我们现在在 SendGrid 推荐的。它比健谈的 SMTP 协议更快。

    $sendgrid->web->send($mail);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2022-10-20
      • 2022-01-02
      • 2013-09-27
      • 2013-08-16
      • 2016-08-21
      相关资源
      最近更新 更多