Adam
Adam

Reputation: 2517

Mailgun HTTP error code 400 Bad Request

Mailgun is giving me a HTTP code of 400 which according to their docs means

Bad Request - Often missing a required parameter https://documentation.mailgun.com/en/latest/api-intro.html#errors

I have the mail sending function in a firebase cloud function, this is the cloud function

app.post('/sendMail', (req, res) => {

    const data = {
        from: 'Excited User <[email protected]>',
        to: 'email, apikey',
        subject: 'Hello',
        text: 'Testing some Mailgun awesomness!'
    };

    mailgun.messages().send(data, function (error, body) {
        if(error){
            console.log(error);
            res.send(error)
        }
        console.log(body);
        res.send('sent email!')
    });
});

exports.app = functions.https.onRequest(app);

I call this function inside my app this the code inside the app:

sendEmail(){
    let url  = `URL to my cloud function`;
    let params: URLSearchParams = new URLSearchParams();

 let data = {
      from: 'Excited User <[email protected]>',
      to: 'myEmail , apiKey',
      subject: 'Hello',
      text: 'Testing some Mailgun awesomness!'
};
    
    return this.http.post(url,data)
                    .toPromise()
                    .then(res=>{
                      console.log(`Res:`,res);

                    })
                    .catch(err => {
                      console.log('Error',err);
                    })
  }

I have followed the firebase API https://documentation.mailgun.com/en/latest/quickstart-sending.html#send-via-api i do not know what parameters are missing as i just copied and pasted their sample code

Upvotes: 3

Views: 5337

Answers (1)

Adam
Adam

Reputation: 2517

I changed this

const data = {
        from: 'Excited User <[email protected]>',
        to: 'email, apikey',
        subject: 'Hello',
        text: 'Testing some Mailgun awesomness!'
};

To:

const data = {
        from: '[email protected]',
        to: 'email',
        subject: 'Hello',
        text: 'Testing some Mailgun awesomness!'
    };

Upvotes: 1

Related Questions