Sending Email with Python using error handler(Simple mail)


 

import smtplib

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

gmailUser = 'sk9775589@gmail.com'
gmailPassword = 'qwsnxvnheoqyauij'
recipient = 'sabhajeetkmr9@gmail.com'

message = f"""
Type your message here...
"""

msg = MIMEMultipart()
msg['From'] = f'"Your Name" <{gmailUser}>'
msg['To'] = recipient
msg['Subject'] = "Subject here..."
msg.attach(MIMEText(message))

try:
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
print ('Email sent!')
except:
print ('Something went wrong...')

Comments