Search code examples
pythonvisual-studio-codemailgun

Python Mailgun Question - 500 Internal Server Error


I am just learning Python and VS code and am in training.

Per Mailgun documentation, I have created a sendmail script (app.py) using Python:

def send_simple_message():
return requests.post(
    "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
    auth=("api", "YOUR_API_KEY"),
    data={"from": "Excited User <mailgun@YOUR_DOMAIN_NAME>",
          "to": ["[email protected]", "YOU@YOUR_DOMAIN_NAME"],
          "subject": "Hello",
          "text": "Testing some Mailgun awesomness!"})

I run the Python script in Visual Studio Code from the terminal like so:

 Python app.py

When I test using Postman, the email is sending successfully, but in Postman, I am getting a 500 error and not exactly sure why:

<!doctype html>
<html lang=en>
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request.     
Either the server is overloaded or there is an error in the application.</p>

Still trying to get the gist of using VS Code, it seems to be a bit of a learning curve though.


Solution

  • The MailGun documentation/Python example that I used didn't seem to work as expeected:

    def send_simple_message():
    return requests.post(
        "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
        auth=("api", "YOUR_API_KEY"),
        data={"from": "Excited User <mailgun@YOUR_DOMAIN_NAME>",
              "to": ["[email protected]", "YOU@YOUR_DOMAIN_NAME"],
              "subject": "Hello",
              "text": "Testing some Mailgun awesomness!"})
    

    enter image description here When I changed the code to the following:

    @app.route('/notifications', methods=['POST'])
    def notifications():
     r=requests.post(
        "https://api.mailgun.net/v3/blahblahblah.mailgun.org/messages",
        auth=("api", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
        data={"from": "Mailgun Test <[email protected]>",
              "to": ["addressee", "[email protected]"],
              "subject": "Hello",
              "text": "Testing some Mailgun awesomness!"})
     
     return r.text
    

    I received the following return:

    enter image description here