Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailgun API: "'from' parameter is not a valid address. please check documentation"

Tags:

python

curl

Trying to run a very simple python program for test. Following is the code:

import requests
def send_email():
files = [
('to', '<my-email-address>'),
('subject', 'Thank you from Foo'),
('text', 'Hello World'),
('from', '\"Info\" <[email protected]>'),
]
r =requests.post('https://api.mailgun.net/v3/mail1.parkl.com/messages', files=files, auth=('api', '<my-key>'))
print r.text, r.status_code

if __name__ == "__main__":
send_email()

It spits out { "message": "'from' parameter is not a valid address. please check documentation" } 400

I have tried various option for From parameter but none seem to work. "Info" , [email protected], [email protected]

Reading online it seems I am not the first one running into this issue but could someone point me how to fix this and move ahead.

A curl request with exact same parameter just works fine:

curl -s —user 'api:<my-key>' \
https://api.mailgun.net/v3/mail1.parkl.com/messages \
-F from=‘Foo <[email protected]>' \
-F to=<my-email-address> \
-F subject='Info Hello' \
-F text='Testing some Mailgun awesomness!'
like image 975
rms_13 Avatar asked Oct 20 '17 16:10

rms_13


1 Answers

Escape characters would not be required in the value for From parameter when specifying a display name. Though, you could use double quote for the display name like so:

import requests

def send_simple_message():
    return requests.post(
        "https://api.mailgun.net/v3/example.com/messages",
        auth=("api", "key-<api-key-here>"),
        data={"from": '"Excited User" <[email protected]>',
              "to": "[email protected]",
              "subject": "Hello",
              "text": "Testing some Mailgun Awesomeness!"})

print send_simple_message().text

Or set the value for the From parameter in single or double quotes such as:

"from": 'Excited User <[email protected]>'

** Disclaimer I work at Mailgun **

like image 121
Nolan Avatar answered Nov 04 '22 21:11

Nolan