簡體   English   中英

在Python郵件功能中填充其他電子郵件字段,例如“主題”

[英]Populate other email fields like Subject in Python mail function

我已經測試了這段代碼,它成功發送了電子郵件,如圖所示,它傾向於將主題字段,抄送和密件抄送字段留空。

import smtplib

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



sent_from = 'dummy@gmail.com'  
to = ['receiver@gmail.com']  
subject = 'OMG Super Important Message'  
body = "Hey, what's up?\n\n- You"

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

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

try:  
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    print("establish connection successful")
    server.ehlo()
    print("identification successful")
    server.login(gmail_user, gmail_password)
    print("login successful")
    server.sendmail(sent_from, to, email_text)
    print("send mail successful")
    server.close()
    print('Email sent!')
except:  
    print('Something went wrong...')

有誰知道我怎么能通過這個腳本填補他們?

收到郵件的屏幕截圖

對於電子郵件主題-您提供給server.sendmail的輸入arg有一種特定的格式應該可以使用。 您可以嘗試:

subject = 'This is the email subject'
text = 'This is the email body'
message = "Subject: {}\n\n{}".format(subject, text)
server.sendmail(sender, recipient, message)

如果您嘗試打開電子郵件的來源,您將看到類似以下內容:

   Received: from ************.net ([***.**.2.17]) by
   *****.****.net ([**.***.224.162]) with mapi id **.03.****.***;
   Mon, 22 May 2017 09:14:59 +0200
   From: *FROMEMAIL* <******@***.com>
   To: *TOEMAIL* <********@***.com>
   CC: *CCEMAIL* <********@*****.com>
   Subject: E-mail - 150633**0686_****.pdf
   ...

那是電子郵件的標題,因此,如果您嘗試這樣的操作:

import smtplib

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



sent_from = 'dummy@gmail.com'  
to = ['receiver@gmail.com']  
subject = 'OMG Super Important Message'  
body = "Hey, what's up?\n\n- You"
cc = "****@***.com"
bcc = "******@*****.com"
email_text = """\
From: %s  
To: %s
CC: %s
BCC: %s
Subject: %s

%s
""" % (sent_from, ", ".join(to), cc, bcc,subject, body)

try:  
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    print("establish connection successful")
    server.ehlo()
    print("identification successful")
    server.login(gmail_user, gmail_password)
    print("login successful")
    server.sendmail(sent_from, to, email_text)
    print("send mail successful")
    server.close()
    print('Email sent!')
except:  
    print('Something went wrong...')

我認為它將起作用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM