Last active
February 3, 2017 08:48
-
-
Save infinite-Joy/edce6494f1ea29ac8c54676e40cd704f to your computer and use it in GitHub Desktop.
send email to people in bulk using templates from gmail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import yagmail | |
import csv | |
import logging | |
""" | |
requirements: | |
pip install yagmail | |
pip install keyrings.alt | |
""" | |
# logging | |
logger = logging.getLogger() | |
handler = logging.StreamHandler() | |
formatter = logging.Formatter( | |
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s') | |
handler.setFormatter(formatter) | |
logger.addHandler(handler) | |
logger.setLevel(logging.INFO) | |
class BulkEmail(object): | |
"""docstring for BulkEmail.""" | |
def __init__(self, email_id): | |
super(BulkEmail, self).__init__() | |
self.email_id = email_id | |
self.yag = yagmail.SMTP(email_id) | |
def get_body(self, user): | |
""" | |
the return must be a list of the lines of the file. | |
""" | |
return ["Hi {user}".format(user=user), "this is another line for the body4"] | |
def get_subj(self): | |
return "this is the subject" | |
def get_reciepients_from_csvfile(self): | |
with open("reciepients.txt") as csvfile: | |
""" | |
eager evaluation with the help of lists is ddone because this | |
will put the list to memory and then we can leave the with block | |
else it gives Value error cant do IO operation on closed file | |
""" | |
reciepients = list(csv.reader(csvfile, delimiter=',')) | |
return reciepients | |
def send_bulk_email(self): | |
for r in self.get_reciepients_from_csvfile(): | |
name = r[1] | |
email_id = r[0] | |
self.yag.send(email_id, self.get_subj(), self.get_body(name)) | |
logger.info('email sent to {email_id}'.format(email_id=email_id)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment