2

I've written a Python script to automatically send some information to my friends. I used SMTPlib, it works well if I only sent to me or one additional email.

When I try to send to 17 emails, (including my sender email), then it shows in sent mail on web-based Gmail. I saw that the mail was sent but I didn't receive it. Only the first recipient received the email. If I reply to all from that mail, then everyone got only that reply.

I can't figure out why they didn't receive it when I sent it from script, I ask my friend check spam, but she didn't find anything.

This is my code:

#!/usr/bin/env python
import smtplib
import csv
from datetime import datetime, timedelta

SMTP_SERVER  = 'smtp.gmail.com'
SMTP_PORT = 587

sender = 'MYBOT@gmail.com'

password = None
with open('pass', 'rt') as f:
    password = f.read().strip('\n')


def send_mail(recipient, subject, body):
    """
    Send happy bithday mail
    """
    headers = ["From: " + sender,
               "Subject: " + subject,
               "To: " + recipient,
               "MIME-Version: 1.0",
               "Content-Type: text/html"]

    headers = "\r\n".join(headers) 

    smtp = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo
    smtp.login(sender, password)

    body = "" + body +""
    smtp.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
    print "Sent to ", 
    print recipient

    smtp.quit()

def send_happybirthday(recipient):
    body = """Happy birthday to you!
            \n<br/>From C2k8pro with love
          """
    subject ='[BirthReminder] Happy birthday to you! from C2k8pro'
    send_mail(recipient, subject, body)


def send_notification(all_mails, names):
    body = """Tomorrow is birthday of %s""" % names
    send_mail(all_mails, body,  body)

def test_send_mail():

    notify_body = """Tomorrow is birthday of """
    recipients = ['MYBOT@gmail.com']

    today = datetime.now()
    format = "%d-%m-%Y"
    print today
    today_in_str = datetime.strftime(today, format)


def read_csv():
    FILENAME = 'mails.csv'
    reader = csv.reader(open(FILENAME, 'rt'), delimiter=',')

    today = datetime.now()
    one_day = timedelta(days=1)
    tomorrow = today + one_day

    all_mails = []
    str_format = "%d/%m"
    str_today = today.strftime(str_format)
    str_tomorrow = tomorrow.strftime(str_format)

    print 'Today is ', str_today
    tomorrow_birth = []
    for row in reader:
        name = row[1].strip()
        dob = row[2]
        dmy = dob.split("/")
        mail = row[3]
        all_mails.append(mail)

        #TODO fix dob with only 1 digit
        birth_date = dmy[0] + "/" + dmy[1]
        if str_today == birth_date:
            print 'Happy birthday %s' % name

            try:
                send_happybirthday(mail)
            except Exception, e:
                print e

        elif str_tomorrow == birth_date:
            tomorrow_birth.append(name)
            print "Tomorrow is %s's birthday" % name

    # Remove empty string
    all_mails = filter(None, all_mails)
    print 'All mails: ', len(all_mails)
    str_all_mails = ', '.join(all_mails)

    if tomorrow_birth:
        all_tomorrow = ', '.join(tomorrow_birth)
        send_notification(str_all_mails, all_tomorrow)


def main():
    read_csv()

if __name__ == "__main__":
    main()

Can anyone explain this. Thanks!

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
HVNSweeting
  • 2,859
  • 2
  • 35
  • 30
  • Can you please provide your code? – Max Jul 31 '12 at 17:15
  • Maybe I found solution from here: http://stackoverflow.com/questions/6941811/send-email-to-multiple-recipients-from-txt-file-with-python-smtplib I passed same string to msg['To'] and sendmail(from_email, emails, msg.as_string() ) – HVNSweeting Jul 31 '12 at 17:21
  • Downvote: The question is impossible to approach without seeing code or error messages. – jforberg Jul 31 '12 at 17:24

1 Answers1

1

I found solution from here

Send Email to multiple recipients from .txt file with Python smtplib

I passed a string contain all recipients separated by comma to msg['To'] and sendmail(). It's true for msg['To'] but with sendmail, I have to use a list.

Community
  • 1
  • 1
HVNSweeting
  • 2,859
  • 2
  • 35
  • 30