I'm trying to use php sendgrid to send emails from a contact form. I only need the simplest implementation and followed the basic steps indicated here: https://github.com/sendgrid/sendgrid-php
Here's my code:
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 . "
" .
"Company: " . $company . "
" .
"Message: " . $message;
$mail->addTo($recipient)->
setFrom($email)->
setSubject($subject)->
setText($message);
$sendgrid->smtp->send($mail);
echo 'SUCCESS';
}
} else {
echo "Fail";
}
And I'm getting this error:
<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 />
Also, when I try to add this to my code (As indicated in the above link):
SendGrid::register_autoloader();
I get this error:
<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 />
^ Based on that, I won't be using swift mailer so it might not be related.
What am I missing?
*PHP Version 5.4.21 *SendGrid Version 1.1.6
Since you are using the SMTP sending method:
$sendgrid->smtp->send($mail);
You need to install swiftmailer. Instructions for that are here: https://github.com/sendgrid/sendgrid-php#optional
Your other option is to send using web - which is actually what we recommend at SendGrid now. It's faster than the chatty SMTP protocol.
$sendgrid->web->send($mail);
I did not install swiftmailer, but I did install smtpapi-php and unirest-php
https://github.com/Mashape/unirest-php
https://github.com/sendgrid/smtpapi-php
My test code looks like this:
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 );
And it works on php 5.4