Python Forum
how to automate sending bulk emails from an excel file or a calc-file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to automate sending bulk emails from an excel file or a calc-file
#1
hello dear pythonists - dear friends, Smile

I want to automatically send custom/personalized emails to a list of recipients - we can take excel-based data or csv.
However, my thoughts in doing so are not quite finished. I think it's not that difficult to roll something like that:
Still, some things I found important when doing Bulk-Email: should i take my own SMTP to send those!?

the format of the mail-adressses:
First comes the format in which we have got the email addresses. I prefer the format that generally provides them in a csv file (comma separated values file).That is pretty nice: a CSV file can be opened using any spreadsheet program (such as Microsoft Excel). If we open a csv file using a text editor (such as notepad), we may find that it contains something of the following format:

how to do this - some idas bout the flow:

a: we try to read the spreadsheet: there are many options - eg also the use of the pandas library.
b: afterwards we try to get connection with the gmail account using smtplib library.
c: to fill in the names and adresses we gather the names and email addresses from the calc-spreadsheet.
d: create a loop that is runned for every record and within this loop we can go and send an email.
e: finally we close the smtp server - in the end.
f. starting over and doing the same again... starting with a.

here - we try to do this witout pandas -...

Name, Email
bill bright, [email protected]
Mick Jagger , [email protected]
Peter Frampton, [email protected]
The format is good to use

Python-approach: Python comes with various very intersting modules. If we want to send some mails we can use smptlib,smtplib would be a great choice: it uses the Simple Mail Transfer Protocol (SMTP). There we can go and use the Gmail SMTP server to send emails using Port 587 or 465.

Sending Plain-Text Emails is not that difficult - if we start importing all the needet helper-modules that are required to send out emails.We can make use of email.mime to structure our email message.
of course we need to specify the email account and its credentials, which will be used to send out emails.
and afterwards if were done with this we do more


Yes we need to do more: of course we need to include more data: like the following

the list of receiving emails and various headers of the email, and even more - like Subject, Body.
In this case, we will send a simple text message.

import os,datetime
def send_mail(recipient, subject, message, files=None):

    import smtplib,email,os
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.mime.application import MIMEApplication
    from os.path import basename

    username = "myemail"
    password ="mypass"
if we want to use Port 465 you have to create an SMTP_SSL object:

# here we have a nice SMTP_SSL Example
server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server_ssl.ehlo() # optional, called by login()
server_ssl.login(gmail_user, gmail_pwd)  
# if the ssl server doesn't support or need tls, then we avoid to call server_ssl.starttls() 
server_ssl.sendmail(FROM, TO, message)
#server_ssl.quit()
server_ssl.close()
print 'successfully sent the mail'
but the question is - how to include the recipients form the calc data-sheet?!


well - with the integration of the csv-data i have some issues- can you advice here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 252 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 349 Feb-07-2024, 12:24 PM
Last Post: Viento
  file open "file not found error" shanoger 8 946 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Search Excel File with a list of values huzzug 4 1,148 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 756 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Bulk loading of data using python shivamsvmsri 2 615 Sep-28-2023, 09:04 AM
Last Post: shivamsvmsri
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 874 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,048 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 2,845 Feb-20-2023, 07:19 PM
Last Post: avd88

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020