'Trying to send an email with python but I am getting '[Errno 8] nodename nor servname provided, or not known'

I am fairly new to python and have been trying to follow a tutorial for automating emails, but keep getting this error when I try to run the file. Any advice would be appreciated.

For the username and password I am using my actually information put into a string. Same for the receiver.

import smtplib

server = smtplib.SMTP(' imap.gmail.com', 993)

server.login('username', 'password')

server.sendmail('username', 'receiver', 'Mail sent from python code')


Solution 1:[1]

As mentioned in the comments, remove the space at the beginning of the server string. If you're trying for gmail specifically, I've had a lot of luck with Redmail,

Here is an example of how to implement it:

from redmail import gmail
import configparser

def redmail_send():

    config = configparser.ConfigParser()
    config.read("email_server_config.ini")
    gmail.user_name = config["email"]["sender_email"] # Your Gmail address
    gmail.password = config["email"]["password"]

# You can play all sorts of tricks with appending strings to make this generative
    html = f"<h1>Here is some header text</h1>"
    gmail.send(
        subject=f"title goes here",
        receivers=['[email protected]'],
        html = html,
        )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MLTQ