Search code examples
python-3.xserverdnssmtpvps

how to set up a mail server and send emails using python SMTP


I would like to send an email with Python smtplib from a custom email address that is linked to a gmail account. The address ends with '.org'. I set up the App Password on the gmail account that the address is linked to. This is my code:

def send_email_from_gmail(**kwargs):
    '''
    kwparams:
    receivers: list
    event_datetime: str
    event_name: str
    parent_name: str
    event_date: str
    event_time: str
    student_name: str
    location: str,
    location_link: str
    '''
    email_data = json.load(open('email_info.json'))

    receivers = kwargs['receivers']
    sender = email_data['email_address']

    msg = EmailMessage()
    msg['Subject'] = email_data['subject'].format(kwargs['event_datetime'], kwargs['event_name'])
    msg['From'] = sender
    msg['To'] = receivers

    msg.set_content(email_data['email'].format(kwargs['parent_name'], kwargs['event_date'], kwargs['event_time'], kwargs['student_name'], kwargs['location'], kwargs['location_link']))

    app_pass = email_data['email_pass']

    with smtplib.SMTP_SSL(host='smtp.gmail.com', port=465) as smtp:
        try:
            smtp.login(sender, app_pass)
            smtp.send_message(msg)
            return {'success': True}
        except:
            return {'success': False}

This is the error:

Traceback (most recent call last):
  File "/Users/ayushpal/MatrixLearning/websitebackend/email_testing.py", line 55, in <module>
    print(send_email_from_gmail(**sample_args))
  File "/Users/ayushpal/MatrixLearning/websitebackend/email_testing.py", line 33, in send_email_from_gmail
    with smtplib.SMTP_SSL(host='smtp.matrixlearning.org', port=587) as smtp:
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 1050, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 1056, in _get_socket
    new_socket = super()._get_socket(host, port, timeout)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socket.py", line 824, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socket.py", line 955, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

If I set the host to 'smtp.{my-domain}.com' instead of '.org', the program runs for about 5 minutes without doing anything before timing out and losing connection.

How can I solve this?


Solution

  • I had to attach the mailbox of my custom domain server to gmail before using gmail as the server to send emails. Thank you to everyone who answered!