Home > OS >  Send email with python SMTP from custom '.org' email address
Send email with python SMTP from custom '.org' email address

Time:08-10

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?

CodePudding user response:

According to DNS queries from my comp smtp.matrixlearning.org has no DMS records so it can not be mapped to IP address.

Traceback (most recent call last):
…
  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:
…
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

CodePudding user response:

In order to use your own mail server: You need to have a vps(Virtual Private Server)

After that:

Step 1: Set a Hostname and Create DNS Records for Domain

Step 2: Installing Apache or Nginx, MariaDB(any DB you prefer) on Ubuntu

Step 3: Installing Postfix Mail Server on Ubuntu

Step 4: Testing Postfix Mail Server on Ubuntu

Step 5: Installing Dovecot IMAP and POP in Ubuntu

Step 6: Installing Roundcube Webmail in Ubuntu

Step 7: Create an Apache Virtual Host for Roundcube Webmail

Step 8: Creating Mail Users

Follow this tutorial might help you!

  • Related