0

I'm creating a program to send e-mails using python. Here's my code:

import os
import smtplib
from email.message import EmailMessage

EMAIL_ADDRESS = os.environ.get("xxx@gmail.com")
EMAIL_PASSWORD = os.environ.get("xxxXXxxx")

msg = EmailMessage()
msg['Subject'] = 'Python'
msg['From'] = EMAIL_ADDRESS
msg['To'] = EMAIL_ADDRESS

msg.set_content('Mail fom Python')

msg.add_alternative("""\
<!DOCTYPE html>
<html>
    <body>
        <h1 style="color:SlateGray;">This is an HTML Email!</h1>
    </body>
</html>
""", subtype='html')

with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
    smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
    smtp.send_message(msg)

A minute after executing, I see the following in the terminal:

Traceback (most recent call last):
  File "multimail.py", line 24, in <module>
    with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
  File "/usr/local/lib/python3.7/smtplib.py", line 1031, in __init__
    source_address)
  File "/usr/local/lib/python3.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/local/lib/python3.7/smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/local/lib/python3.7/smtplib.py", line 1037, in _get_socket
    self.source_address)
  File "/usr/local/lib/python3.7/socket.py", line 727, in create_connection
    raise err
  File "/usr/local/lib/python3.7/socket.py", line 716, in create_connection
    sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address

I've tried to understand the other answers to this question, but I'm a noob so can anyone help me with my specific code?

JRajan
  • 672
  • 4
  • 19
Yuvan Seth
  • 19
  • 4
  • Unrelated, but why do you expect `os.environ.get("yseth27@gmail.com")` to do something useful? In other words, do you really have an environment variable whose *name* is `yseth27@gmail.com`? – tripleee Dec 31 '19 at 08:15
  • "Cannot assign requested address" typically means that you tried to use a privileged port. Can you resolve `smtp.gmail.com` with `nslookup` at the prompt? – tripleee Dec 31 '19 at 08:24
  • Your code won't actually create a useful email message. `add_alternative` only works if the container is `multipart/alternative`. The proper approach here is to create a multipart container message, then add a `text/plain` part with your plain text, and a `text/html` part with the HTML rendering. – tripleee Dec 31 '19 at 08:25
  • Can you explain all the changes you said in code form, i really don't understand the nomenclature – Yuvan Seth Dec 31 '19 at 13:25
  • The Python documentation has examples which do pretty much exactly this. Maybe also examine https://stackoverflow.com/questions/33627662/python-email-in-html-format-mimelib/33628542#33628542 which has an example (though outdated in parts -- definitely prefer `EmailMessage` now) and https://stackoverflow.com/questions/48562935/what-are-the-parts-in-a-multipart-email/48563281#48563281 which explains the nomenclature briefly. – tripleee Dec 31 '19 at 13:48
  • Still the answer to the *actual* question here probably revolves around whether your network is operating correctly, and getting things sent correctly will be a much later question. – tripleee Dec 31 '19 at 13:52

0 Answers0