Ask coding questions

← Back to all posts
Python SMTP
KiranPretzel (1)

Is it possible to send an email (via Gmail) with Python

multicoder2021 (3)

Adding on StringentDev, you also need to Google's Admin Settings and Allow Less Secure Websites or something like that

StringentDev (224)

yes it is, you can use the example script from the script provided:

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

def email():
	sender = "Poly message daemon <[email protected]>"
	receiver = "Schlat Team <[email protected]>"
	msg = MIMEMultipart('alternative')
	msg['Subject'] = "Error on Schlacht"
	msg['From'] = sender
	msg['To'] = receiver

	plaintext = "plaintext email"
	HTMLtext = """\
	        This is some html
	part1 = MIMEText(plaintext, 'plain')
	part2 = MIMEText(HTMLtext, 'html')

	with smtplib.SMTP('', 587) as server:
            server.login("[email protected]", "password") 
            server.sendmail(sender, receiver, msg.as_string())

this script allows a plaintext and a html version of a email to be sent depending on the receiver client.