How to

how to send email using python

You presumably arrived here because you want to send emails with Python. You may want to get email reminders from your code, send users a confirmation email when they register an account or send emails to members of your organization reminding them to pay their dues. Manually sending emails takes time and is prone to errors, but Python makes it simple to automate the process.

 

Setting up a safe SMTP connection

When you use Python to send emails, you should ensure that your SMTP connection is encrypted so others can’t easily read your message or login information. TLS (Transport Layer Security) and SSL (Secure Sockets Layer) are two protocols that can be used to encrypt an SMTP connection. Using a local debugging server, you don’t need to use either of these.

 

Option one is to use SMTP SSL ()

Using the library’s SMTP SSL() function to start a TLS-encrypted connection with Gmail’s SMTP server, the code below makes a secure connection with the server. By default, SSL checks the hostname and its certificates and makes the connection as safe as possible. Make sure to type in your email address instead of [email protected]: When using smtplib. SMTP SSL() as server: closes the connection automatically when the indented code block is done. If the port is 0 or not given, then. SMTP SSL() will use the standard port (port 465) for SMTP over SSL. Putting your email password in your code is not a good idea, especially if you plan to share it with other people. Instead, use input(), as shown above, to let the user type in their password when the script is run. If you don’t want your password to appear on your screen as you type it, you can import the get pass module and use it. Instead of getpass()

 

Option 2 is to use.starttls ()

Instead of using.SMTP SSL() to create a secure connection from the start, we can use startTLS to encrypt an unsecured SMTP connection (). To do this, make an instance of smtplib.SMTP, which wraps an SMTP connection and gives you access to its methods. Setting up your SMTP server and port at the beginning of your script would be the easiest. In the code below, server = SMTP() is used instead of SMTP() as the server, which is what we did in the previous example. Putting your main code in a try block and letting an except block print any error messages to stdout will keep your code from crashing when something goes wrong:

 

Now Send a Plain-Text Email

After you’ve set up a secure SMTP connection using one of the above methods, you can use it. SendMail() to send your email. This function does what it says on the box: The message string starts with “Subject: Hi there,” followed by two newlines (n). This ensures that “Hello there” shows up as the email’s subject, and the text after the newlines will be used as the email’s body. Here is some code that sends a plain-text email over a secured SMTP connection. starttls as a comparison (). Suppose you don’t need the server. ehlo() lines because starttls() and.Sendmail() call them automatically: