0

I want to find the most convenient and easy way to send emails with python. These emails could include in body plain text, html tables and attachements.

I have been learning for past few days how to send emails through outlook with python. At work we use outlook and i am not the developer, i just want to automate my working routine with python.

Found in this old question and through googling everywhere that the most popular answer was to combine 3 modules:

  1. smtplib
  2. email.mime.multipart
  3. email.mime.text

Also i found that we can combine modules:

  1. smtplib
  2. email.message,

but the docs and these examples weren't very helpful, because i couldn't able to mix html + plain text (it will be my next question)

Also i found this module yagmail in this answer convenient and easy to implement.

So, what is the best/convenient/modern/easy way to send emails with python in 2021?

pzelenin92
  • 17
  • 4
  • 1
    the solution depends on what kind of mail you want to send, individual-level or company level? individual-level then I suggest you create a backend server and using flask-mail or Django mail services, configure your SMTP server and send mail, in case of group level, either you need to create or set up your own SMTP server or use third party set up to do so. – sahasrara62 Jan 15 '21 at 14:20
  • 1
    If you want to send email via outlook, use outlook-related tools. Searching the combined [python and outlook tags](https://stackoverflow.com/questions/tagged/python%2boutlook?tab=Newest) should give you some ideas. – snakecharmerb Jan 15 '21 at 14:21

1 Answers1

2

The "Including HTML Content" section from https://realpython.com/python-send-email/ should cover your use cases:

In the example below, our MIMEText() objects will contain the HTML and plain-text versions of our message, and the MIMEMultipart("alternative") instance combines these into a single message with two alternative rendering options:

import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

sender_email = "my@gmail.com"
receiver_email = "your@gmail.com"
password = input("Type your password and press enter:")

message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email

# Create the plain-text and HTML version of your message
text = """\
Hi,
How are you?
Real Python has many great tutorials:
www.realpython.com"""
html = """\
<html>
  <body>
    <p>Hi,<br>
       How are you?<br>
       <a href="http://www.realpython.com">Real Python</a> 
       has many great tutorials.
    </p>
  </body>
</html>
"""

# Turn these into plain/html MIMEText objects
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")

# Add HTML/plain-text parts to MIMEMultipart message
# The email client will try to render the last part first
message.attach(part1)
message.attach(part2)

# Create secure connection with server and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(
        sender_email, receiver_email, message.as_string()
    )

In this example, you first define the plain-text and HTML message as string literals, and then store them as plain/html MIMEText objects. These can then be added in this order to the MIMEMultipart("alternative") message and sent through your secure connection with the email server. Remember to add the HTML message after the plain-text alternative, as email clients will try to render the last subpart first.

I break things
  • 337
  • 3
  • 11