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:
Code: Select all
Name, Email
bill bright, bill@gmail.com
Mick Jagger , mick@yahoo.com
Peter Frampton, peter@hotmail.com
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.
Code: Select all
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:
Code: Select all
# 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'
Prasoon Shukla shows a version that makes use of CSV-based mailing-adresses. see here https://www.hackerea...iler-in-python/
i am not sure how he treats the including ( that is the fetching ) of the adresses that are stored in the csv file.
but note; he uses the gmail-server. so he does not need to run a own server does he!?
import csv import smtplib
from email.mime.text import MIMEText
import re
fp = open('message.txt', 'rb')
msg = MIMEText(fp.read())
fp.close()
msg['Subject'] = 'Subject goes here'
msg['From'] = 'sender.address@gmail.com'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login('sender.address@gmail.com', 'password')
email_data = csv.reader(open('email.csv', 'rb'))
email_pattern= re.compile("^.+@.+\..+$")
for row in email_data:
if( email_pattern.search(row[1]) ):
del msg['To']
msg['To'] = row[1]
try:
server.sendmail('test@gmail.com', [row[1]], msg.as_string())
except SMTPException:
print "An error occured."
server.quit()
and his calc-sheet is called email.csv so he is working like so+
email_data = csv.reader(open('email.csv', 'rb'))
email_pattern= re.compile("^.+@.+\..+$")
for row in email_data:
if( email_pattern.search(row[1]) ):
del msg['To']
msg['To'] = row[1]
try:
server.sendmail('test@gmail.com', [row[1]], msg.as_string())
except SMTPException:
but - the for-loop he is using - is it only to find out the correct rows where the adresses are stored!?
note - if we have the following sheeme:
see the versions he uses - see hereName, Email
bill bright, bill@gmail.com
Mick Jagger , mick@yahoo.com
Peter Frampton, peter@hotmail.com
https://musingsofafriend.wordpress.com/category/email/
https://www.hackerearth.com/practice/no ... in-python/