【发布时间】: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();你在哪里包含这个?