Sending a basic message

These are the basic steps we can follow for sending a basic message with smtplib:

  1. To begin, we need to import the necessary classes: import smtplib.
  2. The SMTP class represents a connection to an SMTP server. Next, we specify our email address, destination, and the message:
from_address = "[email protected]"
to_address = "[email protected]"
message = "Message"
  1. To begin with the message format, we need to import the necessary classes:

from email.mime.text import MIMEText
  1. These messages use the MIME standard, and so we must use the MIMEText class to build a plain text email. We implement an instance of the MIMEText class to build the message:   
mime_message = MIMEText(message, "plain")
mime_message["From"] = from_address
mime_message["To"] = to_address
mime_message["Subject"] = "Subject"
  1. In case the message contains Unicode characters, consider specifying the encoding:

mime_message = MIMEText(message, "plain", _charset="utf-8")
  1. You can modify the visual aspect of the message using HTML:

message = "<em>Hello</em>, <strong>python</strong>!"mime_message = MIMEText(message, "html", _charset="utf-8")
  1. Once the message has been elaborated, we must make the connection to the SMTP server:             

import smtplib
smtp = SMTP("smtp_server")
  1. We can use the server address provided by your hosting provider for this. In the case of the most important services, the server addresses are smtp.live.com (Outlook/Hotmail), smtp.mail.yahoo.com (Yahoo!), and smtp.gmail.com (Gmail).
  2. We then need to enter the data for authentication, that is, the username (usually, this is an email address) and password:smtp.login(from_address, "password")
  3. Finally, we send the email and close the connection with the quit() method:
smtp.sendmail(from_address, to_address, mime_message.as_string())
smtp.quit()

In the following script, we are using a basic example for sending email using smtplib.

You can find the following code in the smtp_basic.py file:

#!/usr/bin/env python3

import smtplib

smtp = smtplib.SMTP('smtp_server')

try:
smtp.sendmail('[email protected]', ['[email protected]'], "This is a test email message.")
except SMTPException as exception:
print("Error: unable to send email: "+exception)
finally:
smtp.quit()

In this script, we are using a SMTP object to connect to the SMTP server and then using the sendmail() method, passing from address, the destination address, and the message as parameters.

If you are using a webmail service (such as Gmail), your email provider must have provided you with outgoing mail server details that you can supply them with, as follows:

smtplib.SMTP('mail.server.domain', 25)

Here, we must point out that the third argument, message, is a string representing the email. We know that a message is usually composed of a header, sender, recipient, message content, and attachments.

In the following script, we are reviewing a way to send an email by using MIMeText for the message format. You can find the following code in the smtp_message_format.py file:

#!/usr/bin/env python3

import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = '[email protected]'
receiver = '[email protected]'

mail_host="smtp.domain.com"
mail_user="user"
mail_password="password"

message = MIMEText('Python', 'plain', 'utf-8')
message['From'] = Header(sender, 'utf-8')
message['To'] = Header(receiver, 'utf-8')

subject = 'Python SMTP message'
message['Subject'] = Header(subject, 'utf-8')

smtp = smtplib.SMTP()

try:
smtp.connect(mail_host, 25)
smtp.login(mail_user,mail_password)
smtp.sendmail(sender, receiver, message.as_string())
except smtplib.SMTPException as exception:
print("Error:"+exception)
finally:
smtp.quit()

If you get smtplib.SMTPNotSupportedError: SMTP AUTH, then the extension is not supported by the server. When you're trying this with a Gmail server, it's important to mention that Gmail requires TLS (which we will review in the following examples).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset