为什么电子邮件没有发送给收件人?

时间:2017-07-20 13:57:55

标签: python pandas email smtp

这是我运行的代码,据说工作完全正常,但实际上并没有将电子邮件发送到我指定的地址。它与CSV没有任何关系因为我有另一个脚本做同样的事情并且工作得很好。问题是电子邮件被放置在发件人收件箱中......这很奇怪。

我宁愿使用这个脚本,因为它很好地面向对象,并且它具有所有适当的主题字段等。

import smtplib
import pandas as pd


class Gmail(object):
    def __init__(self, email, password, recepient):
        self.email = email
        self.password = password
        self.recepient = recepient
        self.server = 'smtp.gmail.com'
        self.port = 465
        session = smtplib.SMTP_SSL(self.server, self.port)        
        session.ehlo
        session.login(self.email, self.password)
        self.session = session
        print('Connected to Gmail account successfully.')

    def send_message(self, subject, body):
        headers = [
            "From: " + self.email,
            "Subject: " + subject,
            "To: " + self.recepient,
            "MIME-Version: 1.0",
           "Content-Type: text/html"]
        headers = "\r\n".join(headers)
        self.session.sendmail(
            self.email,
            self.email,
            headers + "\r\n\r\n" + body)
        print('- Message has been sent.')

df = pd.read_csv('test.csv', error_bad_lines=False)

for index, row in df.iterrows():
    print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
    comp_name = (row['name'])
    print('Email to: ' + comp_name)
    rec = (row['email'])
    print('Email to: ' + rec)
    gm = Gmail('email@gmail.com', 'password', rec)
    gm.send_message('email to ' + comp_name, '<b>This is a test<b>')
    print('-- Message for ' + rec + ' (' + comp_name + ') is completed.')

print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
print('*********************************')
print('Finish reading through CSV.')
print('*********************************')
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')

如果出现问题,请告诉我。我真的希望这能奏效。

只是为了让你可以看到它正在运行,这是我正在测试它的另一个脚本(格式很差)并且它完全正常运行。

import smtplib
import pandas as pd

df = pd.read_csv('test.csv', error_bad_lines=False)

gmail_user = 'email@gmail.com'  
gmail_password = 'password'

for index, row in df.iterrows():

    sent_from = gmail_user  
    to = (row['email'])
    subject = 'Important Message'  
    body = 'Hey, whats up'

    rec = (row['email'])
    comp_name = (row['name'])
    print('Email to: ' + comp_name)
    print('Email to: ' + rec)

    email_text = """\  
    From: %s  
    To: %s  
    Subject: %s

    %s
    """ % (sent_from, to, subject, body)

    try:  
        server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
        server.ehlo()
        server.login(gmail_user, gmail_password)
        server.sendmail(sent_from, to, email_text)
        server.close()

        print ('-- Email sent!')
    except:  
        print ('-- Something went wrong...')

print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
print('*********************************')
print('Finish reading through CSV.')
print('*********************************')
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')

1 个答案:

答案 0 :(得分:1)

您应该传递收件人而不是发件人。

    self.session.sendmail(
    self.email,
    # self.email, <- wrong here
    self.recepient,
    headers + "\r\n\r\n" + body)