2016-11-19 37 views
2

http://datamakessense.com/easy-scheduled-emailing-with-python-for-typical-bi-needs/のスニペットに基づくコードを使用して、私の会社の電子メールで顧客にPDF添付ファイルを送信しました。私たちは1つの電子メールアドレス( "[email protected]")で約100件を一度に送信し、送信された電子メールごとにBCCのコピーを内部電子メールアドレスに送信します( "reports @ companyname.com ")。SMTPlib添付ファイルが受信されていません

随時(約5分の5)、顧客は添付ファイルを取得しないと報告します。時にはそれはまったく表示されないこともあり、時には赤い疑問符で表示されることもあります。しかし、BCCのコピーは常に問題なく添付ファイルを持ち、送信アカウントに入ると、送信された電子メールのコピーには問題なく添付ファイルが常に表示されます。添付ファイルを受け取っていない(共有ドメインなどの、実際にはほとんどが@ gmail.com)顧客の電子メールには、顕著な類似点はありません。報告する例外またはエラーはありません。すべて正常に動作しているように見えます。

これは私が初めてMIMEを使って仕事をしたり、Pythonを使って電子メールを自動化したりしていますが、98%の時間を費やしているという事実は私を混乱させています。これが起こっている理由が分かっていますか?多分私はタイプを正しく設定していないでしょうか?それとも、MIME for Gmailで何かすべき特別なことはありますか?ここで

は私のコードです:

wdir = 'PDFs\\' 
filelist = [] 
for file in os.listdir(wdir): 
    if file.endswith('.pdf'): 
     filelist += [wdir + file] # sending all of the PDFs in a local directory 

email = {} 
rf = wdir + 'Reports_data.csv' # get email addresses for customers by ID (row[2]) 
with open(rf, 'rbU') as inf: 
    read = csv.reader(inf) 
    read.next() 
    for row in read: 
     email[row[2]] = row[3] 

hfi = open('HTML\\email.html', 'rb') # the HTML for the email body, itself 
htmltxt = hfi.read() 
hfi.close() 


class Bimail: 
    def __init__(self, subject, recipients): 
     self.subject = subject 
     self.recipients = recipients 
     self.htmlbody = '' 
     self.sender = "[email protected]" 
     self.senderpass = 'password' 
     self.attachments = [] 

    def send(self): 
     msg = MIMEMultipart('alternative') 
     msg['From'] = self.sender 
     msg['Subject'] = self.subject 
     msg['To'] = self.recipients[0] 
     msg.preamble = "preamble goes here" 
     if self.attachments: 
      self.attach(msg) 
     msg.attach(MIMEText(self.htmlbody, 'html')) 
     s = smtplib.SMTP('smtp.gmail.com:587') 
     s.starttls() 
     s.login(self.sender, self.senderpass) 
     s.sendmail(self.sender, self.recipients, msg.as_string()) 
     s.quit() 

    def htmladd(self, html): 
     self.htmlbody = self.htmlbody + '<p></p>' + html 

    def attach(self, msg): 
     for f in self.attachments:  
      ctype, encoding = mimetypes.guess_type(f) 
      if ctype is None or encoding is not None: 
       ctype = "application/octet-stream" 
      maintype, subtype = ctype.split("/", 1) 
      fn = f.replace(wdir, '') 
      fp = open(f, "rb") 
      attachment = MIMEBase(maintype, subtype) 
      attachment.set_payload(fp.read()) 
      fp.close() 
      encoders.encode_base64(attachment) 
      attachment.add_header("Content-Disposition", "attachment", filename=fn) 
      attachment.add_header('Content-ID', '<{}>'.format(f)) # or should this be format(fn)? 
      msg.attach(attachment) 

    def addattach(self, files): 
     self.attachments = self.attachments + files 


if __name__ == '__main__': 
    for fi in filelist: 
     code = fi.split('_')[1].split('\\')[1] # that "ID" for email is in the filename 
     addr = email[code] 
     mymail = Bimail(('SUBJECT HERE'), [addr, '[email protected]']) 
     mymail.htmladd(htmltxt) 
     mymail.addattach([fi]) 
     mymail.send() 

答えて

2

のコードブロックを試してみてください:

import smtplib 
from email.MIMEMultipart import MIMEMultipart 
from email.MIMEText import MIMEText 
from email.MIMEBase import MIMEBase 
from email import encoders 

fromaddr = "[email protected]" 
password = "password" 
toaddr = "[email protected]" 

msg = MIMEMultipart() 

msg['From'] = fromaddr 
msg['To'] = toaddr 
msg['Subject'] = "Report" 

body = "Hi, have a look at the Report" 

msg.attach(MIMEText(body, 'plain')) 

filename = "Report.pdf" 
attachment = open("Report.pdf", "rb") 

part = MIMEBase('application', 'octet-stream') 
part.set_payload((attachment).read()) 
encoders.encode_base64(part) 
part.add_header('Content-Disposition', "attachment; filename= %s" % filename) 

msg.attach(part) 

server = smtplib.SMTP('smtp.office365.com', 587) 
server.starttls() 
server.login(fromaddr, "password") 
text = msg.as_string() 
server.sendmail(fromaddr, toaddr, text) 
server.quit() 

それは、電子メールあたり100の添付ファイルのそば送信

+0

あり、いくつかのマイナーな違いがありますが、それは私がやっているとほとんど同じです。私が言ったように、私のコードは、 "私のために働く" - 大部分の時間。しかし、何千人ものお客様がいる場合、1%の故障率は大きな問題です。 – Xodarap777

+0

これは実際には機能しました。元のコードで何が間違っていたのか分かりません。私は賞金に遅れました。ごめんなさい。 – Xodarap777

+0

コードが助けてくれてうれしいです。 –

2

は、誰もが使用されているものではありません私のために働きました毎日するには、Google SMTPサーバー側に制限がないことを確認しましたか?

以下の質問は、「G SuiteのGmail送信制限」に関連していますが、私の意見では他のGmailアカウントにも同様のルールが適用されています。

https://support.google.com/a/answer/2956491#sendinglimitsforrelay

https://support.google.com/a/answer/166852?hl=en

私のsendmailの機能を怒鳴るご覧ください。

def sendMail(to, subject, text, files=[],server="smtp.gmail.com:587",username="contact.name",password="mygmailurrentpassord"): 
    assert type(to)==list 
    assert type(files)==list 
    fro = "Contact Name <[email protected]>" 
    msg = MIMEMultipart() 
    msg['From'] = fro 
    msg['To'] = COMMASPACE.join(to) 
    msg['Date'] = formatdate(localtime=True) 
    msg['Subject'] = subject 
    msg.attach(MIMEText(text))  
    for file in files: 
     part = MIMEBase('application', "octet-stream") 
     part.set_payload(open(file,"rb").read()) 
     Encoders.encode_base64(part) 
     part.add_header('Content-Disposition', 'attachment; filename="%s"' 
         % os.path.basename(file)) 
     msg.attach(part)  
    smtp = smtplib.SMTP(server) 
    smtp.starttls() 
    smtp.login(username,password) 
    smtp.sendmail(fro, to, msg.as_string()) 
    smtp.close() 

よろしく

+0

私は実際にはすでに限度額についてGoogle(有料/ビジネスユーザー)と呼ばれていて、リレーを介して1日あたり数千の電子メールを許可しています。 – Xodarap777

関連する問題