PHP - 简单的SendGrid致命错误:找不到类'SendGrid \ Email'

时间:2013-12-11 07:05:40

标签: php sendgrid

我正在尝试使用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邮件,因此它可能不相关。

我错过了什么?

* PHP版本5.4.21 * SendGrid版本1.1.6

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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

您需要安装swiftmailer。有关这方面的说明,请访问:https://github.com/sendgrid/sendgrid-php#optional

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

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