Skip to content

The python code can send huge amount of emails once using SMTP server. Just need to upload a csv file (excel file) containing the destination mails.

License

Notifications You must be signed in to change notification settings

ViduraErandika/Send-mails-through-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Send Emails through Python using SMTP server

⭐Jupyter Notebook IDE is used to code and Sendgrid APIs used as a SMTP server⭐

Integration of the SMTP server through Sendgrid :

  • Create a account in Sendgrid and enter Email api. Choose SMTP relay.


  • Create an APIkey and copy the APIkey and password to python code and Verify Integration


Update the python code in Jupyter Notebook :

  • Attach the CSV file which includes the destination mails.

import pandas as pd
import numpy as np
import time
import sys
import os
import re

# sengrid smtplib protocols entered below
from smtplib import SMTP_SSL as SMTP       # this invokes the secure SMTP protocol (port 465, uses SSL)
# from smtplib import SMTP                  # use this for standard SMTP protocol   (port 25, no encryption)

# from email.MIMEText import MIMEText     # old version
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

log = pd.read_csv('/Users/vidur/Downloads/csv-test.csv', encoding='utf-8')    #enter your sending mails csv file HERE
  • Update APIkey and password in followinf send_mail function.

def send_email(name,email,username,password,ref,i,teamname):
    SMTPserver = 'smtp.sendgrid.net'  #since i'm using sendgrid apis placed their SMTPserver here.
    sender =     'Test <***@uom.lk>'   # keyword <your sendgrid mail >
    destination = [email]            #sending destination
    print(i,end=" ")
    print(destination,end="")
    
    USERNAME = "apikey"               #set the sendgrid smtp api key and password here
    PASSWORD = "********************************************************"

    # typical values for text_subtype are plain, html, xml
    text_subtype = 'html'
    content="""\
            
            <p> Place Your Content here. </p>
    
    """ 
    subject="Email Subject here"

    try:
        msg = MIMEText(content, text_subtype)
        msg['Subject']=       subject
        msg['From']   = sender # some SMTP servers will do this automatically, not all

        conn = SMTP(SMTPserver)
        conn.set_debuglevel(False)
        conn.login(USERNAME, PASSWORD)
        try:
            conn.sendmail(sender, destination, msg.as_string())
        finally:
            print("*")
            conn.quit()
    except:
        failed.append(i)
        print("FAILED! - "+ str(destination))
  • Run all the cells starting from 0.

Good Luck ❤

About

The python code can send huge amount of emails once using SMTP server. Just need to upload a csv file (excel file) containing the destination mails.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published