sendgrid / sendgrid-php

The Official Twilio SendGrid PHP API Library
https://sendgrid.com
MIT License
1.49k stars 623 forks source link

Empty from email address (required) even though it's provided #212

Closed wernight closed 8 years ago

wernight commented 8 years ago

I get the error: Empty from email address (required) however this is the email object being sent:

bcc: null
bccName: null
cc: null
ccName: null
content: null
date: null
from: "do-not-reply@example.com"
fromName: "Foo's Example"
headers: null
html: "<html>↵<body>...</body>↵</html>↵"
replyTo: false
smtpapi: Object
subject: "[Example] on 21-03-2016"
text: null
to: Array[1]
toName: null

Running latest sendgrid/sendgrid release (version 4.0.4) which uses latest sendgrid/smtpapi (version 0.5.0).

Similar to issues:

wernight commented 8 years ago

Here is the $email->toWebFormat() generated:

thinkingserious commented 8 years ago

Thanks for letting us know. I've added this to our backlog.

Could you provide the relevant code snippet?

wernight commented 8 years ago
<?php
require('vendor/autoload.php');

$email = new \SendGrid\Email();
$email
    ->addTo('example@example.com', 'Foo Bar')
    ->setFrom('example@example.com')
    ->setSubject('test')
    ->setText('test');

$sendgrid = new \SendGrid(\file_get_contents('/mnt/sendgrid/api-key'));
$sendgrid->send($email);
$ php snippet.php
Fatal error: Uncaught exception 'SendGrid\Exception' with message '{"errors":["Empty from email address (required)"],"message":"error"}' in /srv/vendor/sendgrid/sendgrid/lib/SendGrid.php:120
Stack trace:
#0 /srv/snippet.php(12): SendGrid->send(Object(SendGrid\Email))
#1 {main}
  thrown in /srv/vendor/sendgrid/sendgrid/lib/SendGrid.php on line 120

Note:

wernight commented 8 years ago

I did notice that my key has a line return at the end. May be those are not properly handled/escaped.

Without the line return it works (e.g. using \trim()).

thinkingserious commented 8 years ago

Hmm, I think I had a similar issue, that I fixed with this: https://github.com/sendgrid/php-http-client/blob/master/lib/SendGrid/config.php#L31

Are you saying that once you added the trim() to your api key, it now works?

wernight commented 8 years ago

Yes

However I think it should be fixed as the error really is misleading

thinkingserious commented 8 years ago

Agreed, we'll get that into our backlog. Thanks!

thinkingserious commented 8 years ago

@wernight We have completely revamped our error messaging. Check out our new docs for details: https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html

Thanks for your continued support!

wizvs commented 8 years ago

@thinkingserious I am getting the same error [Error: Empty from email address (required)] when using nodejs sendgrid module. Here is my code.:

sendgrid.send({ to: 'nitefoodie@gmail.com ', from: 'talk@aircountr.com', subject: 'Report Export', text: 'Please find the attached exported report.', files: [ { filename: '', // required only if file.content is used. contentType: '', // optional cid: '', // optional, used to specify cid for inline content path: '', // url: './file.csv', // == One of these three options is required content: ('' | Buffer) // } ] },function(err, json) { if (err) { return console.error(err); }

console.log(json); res.json("Mail sent successfully"); });

thinkingserious commented 8 years ago

@wizvs Please follow this issue over in the nodejs repo: https://github.com/sendgrid/sendgrid-nodejs/issues/228

I'll be responding to that ticket soon.

ndo360 commented 7 years ago

I am now having this issue and this link now points to a 404: https://github.com/sendgrid/php-http-client/blob/master/lib/SendGrid/config.php#L31

thinkingserious commented 7 years ago

Hello @ndo360,

Could you please provide some further detail so that I can try and reproduce?

Specifically:

  1. Sample source code
  2. Version of the SDK you are using

Thank you!

Elmer

ndo360 commented 7 years ago

@thinkingserious Thank you for replying fast, here is the source code referenced in the error: [sendgrid.js] if (json.message !== 'success') { var error = 'sendgrid error'; if (json.errors) { error = json.errors.shift(); } return callback(new Error(error), null); } [request.js] // Protect against double callback if (!self._callback && self.callback) { self._callback = self.callback self.callback = function () { if (self._callbackCalled) { return // Print a warning maybe? } self._callbackCalled = true self._callback.apply(self, arguments) }

if (self._json) { try { response.body = JSON.parse(response.body, self._jsonReviver) } catch (e) { debug('invalid JSON received', self.uri.href) } } debug('emitting complete', self.uri.href) if (typeof response.body === 'undefined' && !self._json) { response.body = self.encoding === null ? new Buffer(0) : '' } self.emit('complete', response, response.body) }) }

responseContent.on('end', function (chunk) { self.emit('end', chunk) })

Here is the mailer code: [mailer.js]

`'use strict';

var Config = require.main.require('./server/tools/config'); var SendGrid = require('sendgrid')(Config.SENDGRID_API_KEY);

module.exports = {

sendPasskey: function(name, email, passcode) {
    var appName = 'Secret Hitler Online';
    var passcodeMail = new SendGrid.Email({
        to: email,
        from: process.env.SENDGRID_FROM_EMAIL,
        subject: passcode,
        text: ' ',
        html: ' ',
    });
    passcodeMail.setSubstitutions({
        ':app': [appName, appName],
        ':name': [name],
        ':passcode': [passcode, passcode]
    });
    passcodeMail.setFilters({
        'templates': {
            'settings': {
                'enable': 1,
                'template_id': process.env.SENDGRID_EMAIL_TEMPLATE,
            }
        }
    });

    SendGrid.send(passcodeMail, function(err, json) {
        if (err) {
            console.error('sendPasskey', err);
        }
    });
},

};`

Also I have attached the screenshot of the error displayed error screenshot

thinkingserious commented 7 years ago

@ndo360,

What version of the SendGrid Node.js library are you using?

ndo360 commented 7 years ago

@thinkingserious I manged to fix it as it turned out I missed a step, thanks for everything though!

Sachin-Suresh commented 7 years ago

what step had u missed...please post ur working code!

thinkingserious commented 7 years ago

Hi @Sachin-Suresh,

I'd be happy to try and help. Could you please post the code you are having trouble with and the error message?

Thanks!

With Best Regards,

Elmer