Ask coding questions

← Back to all posts
Python SMTP
KiranPretzel (1)

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

Comments
hotnewtop
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 = """\
<html>
	<head>
	</head>
	<body>
	        This is some html
	</body>
</html>"""
	part1 = MIMEText(plaintext, 'plain')
	part2 = MIMEText(HTMLtext, 'html')
	msg.attach(part1)
	msg.attach(part2)

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

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