Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import smtplib
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- gmail_user = 'username@gmail.com'
- gmail_password = 'p@ssw0rd'
- def send_mail(params, type_):
- email_subject = params['email_subject']
- email_from = "from_email@domain.com"
- email_to = params['email_to']
- email_cc = params.get('email_cc')
- email_bcc = params.get('email_bcc')
- email_body = params['email_body']
- msg = MIMEMultipart('alternative')
- msg['To'] = email_to
- msg['CC'] = email_cc
- msg['Subject'] = email_subject
- mt_html = MIMEText(email_body, type_)
- msg.attach(mt_html)
- server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
- server.ehlo()
- toaddrs = [email_to] + [email_cc] + [email_bcc]
- server.login(gmail_user, gmail_password)
- server.sendmail(email_from, toaddrs, msg.as_string())
- server.close()
- params = {
- 'email_to': 'username@gmail.com',
- 'email_cc': 'username@gmail.com',
- 'email_bcc': 'username@gmail.com',
- 'email_subject': 'Test message from python library',
- 'email_body': '<h1>Hello World</h1>'
- }
- for t in ['plain', 'html']: #sending plain and html email
- try:
- send_mail(params, t)
- print ('Email sent!')
- except Exception as e:
- print('Something wrong: '+e)
Add Comment
Please, Sign In to add comment