send gmail through python

less than 1 minute read

Published:

Using only smtplib and getpass you can send your email from the terminal:

import sys

import smtplib
import getpass as gp

me    = 'tuguldur.s@gmail.com'
gmail = 'smtp.gmail.com:587'

def msgheader(to):
    header  = f'From: {me}\n'
    header += f'To: {to}\n\n'
    return header

def sendgmail(to, msg):
    header = msgheader(to)
    msg  = header + msg
    serv = smtplib.SMTP(gmail)
    # start encryption
    serv.starttls()
    pwd  = gp.getpass(f'>>> pwd for {me} :')
    serv.login(me, pwd)
    serv.sendmail(me, [to], msg)
    serv.quit()


if __name__ == "__main__":
    sendgmail(sys.argv[1], sys.argv[2])

run it as:

$ python3 sendgmail.py sukhbold.1@osu.edu 'my test message'

Note that your message needs to have a header with at least From/To fields, otherwise the message body alone won’t be recognized as valid. For gmail users you may need to turn on the access for “Less secure app” in your account settings. Since we are using TLS the gmail port should be 587, see here. Finally, note the receiving address(to) is passed as a list in serv.sendmail(); this is in case you have multiple recipients.