Python packages to send emails This is not an exhaustive list and we are not covering any of these in detail. 1. smtplib 2. yagmail [Ref: stackoverflow] 3. From Google: 'googleapiclient', 'google_auth_oauthlib', 'google.auth.transport.requests' and (this is not from Google) email (as in from email.mime.text import MIMEText) Ref: developers.google.com 4. 'email' as in: from email.message import EmailMessage from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.parser import BytesParser, Parser from email.policy import default from email import encoders 5. win32com As in: import win32com.client as win32 outlook=win32.Dispatch('outlook.application')" 6. sendgrid Ref: realpython - - - 1. smtplib This webpage has a very clear and basic code snippet for sending an email: https://www.tutorialspoint.com/python/python_sending_email.htm With this package, you have to provide the server details as in this line of code: smtpObj = smtplib.SMTP('localhost') Or as in here: # creates SMTP session s = smtplib.SMTP('smtp.gmail.com', 587) # 587 is port. Or server = smtplib.SMTP('smtp.gmail.com:587') An Example def send_email_gmail(subject, message, destination): # First assemble the message msg = MIMEText(message, 'plain') msg['Subject'] = subject # Login and send the message port = 587 my_mail = 'my_mail@hotmail.com' my_password = 'my_password' with smtplib.SMTP('smtp-mail.outlook.com', port) as server: server.starttls() server.login(my_mail, my_password) server.sendmail(my_mail, destination, msg.as_string()) SENDING MAIL VIA SSL import smtplib, ssl from email.mime.text import MIMEText def send_email(subject, message, destination): # First assemble the message msg = MIMEText(message, 'plain') msg['Subject'] = subject # Login and send the message port = 465 my_mail = 'me@gmail.com' my_password = 'me' context = ssl.create_default_context() with smtplib.SMTP_SSL('smtp.gmail.com', port, context=context) as server: server.login(my_mail, my_password) server.sendmail(my_mail, destination, msg.as_string()) send_email('Test subject', 'This is the message', 'to_email@email.com') Ref: stackoverflow Reference URLs 1. docs.python.org 2. developers.google.com 3. stackoverflow
Pages
- Index of Lessons in Technology
- Index of Book Summaries
- Index of Book Lists And Downloads
- Index For Job Interviews Preparation
- Index of "Algorithms: Design and Analysis"
- Python Course (Index)
- Data Analytics Course (Index)
- Index of Machine Learning
- Postings Index
- Index of BITS WILP Exam Papers and Content
- Lessons in Investing
- Index of Math Lessons
- Downloads
- Index of Management Lessons
- Book Requests
- Index of English Lessons
- Index of Medicines
- Index of Quizzes (Educational)
Thursday, March 11, 2021
Sending emails using Python
Labels:
Python,
Technology
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment