Sending email

In this section, we are going to learn about sending an email from Gmail through a Python script. To do this, Python has a module named smtplib. The smtplib module in Python provides SMTP client session object that is used to send an email to any internet machine with SMTP listener.

We are going to look at an example. In this example, we will send an email containing a simple text from Gmail to the recipients.

Create a send_email.py script and write the following content in it:

import smtplib
from email.mime.text import MIMEText
import getpass

host_name = 'smtp.gmail.com'
port = 465

u_name = 'username/emailid'
password = getpass.getpass()
sender = 'sender_name'
receivers = ['receiver1_email_address', 'receiver2_email_address']

text = MIMEText('Test mail')
text['Subject'] = 'Test'
text['From'] = sender
text['To'] = ', '.join(receivers)

s_obj = smtplib.SMTP_SSL(host_name, port)
s_obj.login(u_name, password)
s_obj.sendmail(sender, receivers, text.as_string())
s_obj.quit()
print("Mail sent successfully")

Run the script as follows:

student@ubuntu:~/work$ python3 send_text.py

Output:

Password:
Mail sent successfully

In the preceding example, we have sent an email from our Gmail ID to the receivers. The user name variable will store your email ID. In the password variable, either you can enter your password or else you can prompt for password using the getpass module. Here, we prompt for the password. Next, the sender variable will have your name. Now we are going to send this email to multiple receivers. Then, we included subject, from, and to for that email. Then in login(), we mentioned our username and password variables. Next, in sendmail(), we mentioned the sender, receivers, and text variables. So, using this process, we have sent the email successfully.

Now, we will look at one more example of sending an email with an attachment. In this example, we are going to send an image to the recipient. We are going to send this mail via Gmail. Create a send_email_attachment.py script and write the following content in it:

import os
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import getpass

host_name = 'smtp.gmail.com'
port = 465

u_name = 'username/emailid'
password = getpass.getpass()
sender = 'sender_name'
receivers = ['receiver1_email_address', 'receiver2_email_address']

text = MIMEMultipart()
text['Subject'] = 'Test Attachment'
text['From'] = sender
text['To'] = ', '.join(receivers)

txt = MIMEText('Sending a sample image.')
text.attach(txt)

f_path = '/home/student/Desktop/mountain.jpg'
with open(f_path, 'rb') as f:
img = MIMEImage(f.read())

img.add_header('Content-Disposition',
'attachment',
filename=os.path.basename(f_path))

text.attach(img)

server = smtplib.SMTP_SSL(host_name, port)
server.login(u_name, password)
server.sendmail(sender, receivers, text.as_string())
print("Email with attachment sent successfully !!")
server.quit()

Run the script as follows:

student@ubuntu:~/work$ python3 send_email_attachment.py

Output:

Password:
Email with attachment sent successfully!!

In the preceding example, we sent an image as an attachment to the receivers. We mentioned the sender's and receivers' email IDs. Next, in the f_path, we mentioned the path of the image that we sent as an attachment. Next, we sent that image as an attachment to the receiver.

In the previous two examples – send_text.py and send_email_attachment.py – we sent email via Gmail. You can send via any other email providers. To use any other email provider, just write that provider name in host_name. Don’t forget to add smtp before it. In these example, we used smtp.gmail.com; for Yahoo! you can use smtp.mail.yahoo.com. So, you can change the hostname as well as the port, according to your email providers.
..................Content has been hidden....................

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