Email Automation

Sending mail via gmail

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
mail_content = '''Hello, Mail is sent!!
'''

#The mail addresses and password
sender_address = 'sahil.6906@gmail.com'
sender_pass = ''
receiver_address = ''

#Setup the MIME
message = MIMEMultipart()
message['From'] = 'sahil.6906@gmail.com'
message['To'] = ''
message['Subject'] = 'Test mail'   #The subject line

#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))

#Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()

print("mail sent")

Sending Mail via gmail with attachment

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase 
from email import encoders 

mail_content = '''Hello, Mail is sent!!
'''

#The mail addresses and password
sender_address = 'sahil.6906@gmail.com'
sender_pass = ''
receiver_address = ''

#Setup the MIME
message = MIMEMultipart()
message['From'] = 'sahil.6906@gmail.com'
message['To'] = 'agarwalshalki@gmail.com'
message['Subject'] = 'Test mail'   #The subject line

# attach the body with the msg instance 
message.attach(MIMEText(mail_content, 'plain')) 

# open the file to be sent
filename = "type conversion.xlsx"
attachment = open("C:/sahil/type conversion.xlsx", "rb") 

# instance of MIMEBase and named as p
p = MIMEBase('application', 'octet-stream') 

# To change the payload into encoded form
p.set_payload((attachment).read()) 

# encode into base64 
encoders.encode_base64(p)

# Add header as key/value pair to attachment part
p.add_header('Content-Disposition', "attachment; filename= %s" % filename) 
message.attach(p) 

s = smtplib.SMTP('smtp.gmail.com', 587) 
s.starttls() 
s.login(sender_address, sender_pass) 
text = message.as_string() 
s.sendmail(sender_address, receiver_address, text) 
s.quit() 
print("mail sent")

Sending one mail to multiple email ids in excel

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import xlrd

loc = ("C:/sahil/student details.xlsx") 
wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0)

for a in range(1,sheet.nrows):

    mail_content = '''Hello, Mail is sent!!
    '''

    #The mail addresses and password
    sender_address = 'sahil.6906@gmail.com'
    sender_pass = ''
    receiver_address = sheet.cell_value(a, 1)

    #Setup the MIME
    message = MIMEMultipart()
    message['From'] = 'sahil.6906@gmail.com'
    message['To'] = sheet.cell_value(a, 1)
    message['Subject'] = sheet.cell_value(a, 0)   #The subject line

    #The body and the attachments for the mail
    message.attach(MIMEText(mail_content, 'plain'))

    #Create SMTP session for sending the mail
    session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
    session.starttls() #enable security
    session.login(sender_address, sender_pass) #login with mail_id and password
    text = message.as_string()
    session.sendmail(sender_address, receiver_address, text)
    session.quit()

print("mail sent")

Last updated