In this tutorial, we will introduce how to send text email using python smtp.
If you want to send email with file attachment, you can read:
Python: Send Email with File Attachment Using SMTP
1.Import smtp library
import smtplib
2.Log in into the server
server.login('your email address', 'your password')
3.Create email message
subject = 'This is my subject' body = 'This is the message in the email' message = f'Subject: {subject}\n\n{body}'
4.Send email
server.sendmail('your email address', 'recipient email address', message) server.quit()
5.Create a python function to send email
def send_email(subject, body): try: server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo() server.starttls() server.login('your email address', 'your password') message = f'Subject: {subject}\n\n{body}' server.sendmail('your email address', 'recipient email', message) server.quit() except: pass
Then you can send an email as follows:
send_email('this is the subject', 'this is the body')