1

I want to send emails with python but I don't know how to do so.

When I searched google I got this result:

Set up the SMTP server and log into your account. Create the MIMEMultipart message object and load it with appropriate headers for From, To, and Subject fields. Add your message body. Send the message using the SMTP server object.

SJXD
  • 15
  • 1
  • 6
  • Follow https://realpython.com/python-send-email/ – balderman Aug 22 '21 at 12:53
  • 1
    Does this answer your question? [Sending mail from Python using SMTP](https://stackoverflow.com/questions/64505/sending-mail-from-python-using-smtp) – Piero Aug 22 '21 at 13:16

1 Answers1

1
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

msg = MIMEMultipart()
msg['From'] = 'me@gmail.com'
msg['To'] = 'you@gmail.com'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))

mailserver = smtplib.SMTP('smtp.gmail.com',587)
# identify ourselves to smtp gmail client
mailserver.ehlo()
# secure our email with tls encryption
mailserver.starttls()
# re-identify ourselves as an encrypted connection
mailserver.ehlo()
mailserver.login('me@gmail.com', 'mypassword')

mailserver.sendmail('me@gmail.com','you@gmail.com',msg.as_string())

mailserver.quit()

If you want send html read this

Piero
  • 404
  • 3
  • 7
  • 22