Gmail 邮箱python自动发送邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gmail 邮箱python自动发送邮件相关的知识,希望对你有一定的参考价值。

参考技术A 反正就是要给一批人发送内容差不多的邮件, 手上只有文本格式的每个人的email address和对应的message.
写了很久了,弄出来以后好看。。
反正gmail的话,得到邮箱选项里设置几个东西(降低自己账户安全等级)。否则有可能发不出去,或者是发几十封之后账户被锁。。。

根据文件附件条件从 python 发送自动电子邮件

【中文标题】根据文件附件条件从 python 发送自动电子邮件【英文标题】:Send Automated emails from python based on file attachment conditions 【发布时间】:2020-10-06 19:43:25 【问题描述】:

我正在尝试使用 python 发送一封自动电子邮件。我想根据一些条件修改代码。

1) 如果附件不为空,那么收件人和邮件内容应如下所示

receiver_address = 'abc@gmail.com'
cc_address="efg@gmail.com'
mail_content = ''' PFA the lastest file '''

2) 否则应该是

receiver_address = 'xyz@gmail.com'
cc_address="efg@gmail.com'
mail_content = ''' File is empty '''

我的代码

data1.to_excel('Report.xlsx')
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from datetime import date

mail_content = '''PFA the latest file'''
sender_address = 'will@gmail.com'
receiver_address = 'abc@gmail.com'
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = "Weekly Active Billing Report for week ending on" +" " + (ls).strftime('%Y%m%d')

#The subject line
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
attach_file_name = 'Report.xlsx'
attach_file = open(attach_file_name, 'rb') # Open the file as binary mode
payload = MIMEBase('application', 'octate-stream')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload) #encode the attachment
#add payload header with filename
#payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
payload.add_header('Content-Disposition',"attachment; filename=%s" % attach_file_name)
message.attach(payload)

#Create SMTP session for sending the mail
session = smtplib.SMTP('smtplocal.xx.xx.xx',25) #use gmail with port
#session.starttls() #enable security
#session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address.split(",") , text) 
session.quit()
print('Mail Sent')

【问题讨论】:

【参考方案1】:

使用字典根据文件是否为空来获取所需的值并更新变量。应该可以的。

data1.to_excel('Report.xlsx')
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from datetime import date

import os

attach_file_name = 'Report.xlsx'
filesize = os.stat(attach_file_name).st_size
dict1 = "Non-empty":  'receiver': 'xyz@gmail.com', "mail_content": " PFA the lastest file"  ,
        "Empty":  "receiver": 'abc@gmail.com',  "mail_content":" File is empty " 
receiver_address = ""
mail_content = ""
if filesize == 0:
    receiver_address = dict1["Empty"]["receiver"]
    mail_content = dict1["Empty"]["mail_content"]
else:
    receiver_address = dict1["Non-empty"]["receiver"]
    mail_content = dict1["Non-empty"]["mail_content"]


mail_content = '''PFA the latest file'''
sender_address = 'will@gmail.com'
#Setup the MIME
message = MIMEMultipart()

# Rest of the code is same. 

【讨论】:

如果将代码合并到现有代码中会更容易理解 @HArdRese,我也在寻找类似的解决方案。能否请添加此现有的上述代码以更好地理解

以上是关于Gmail 邮箱python自动发送邮件的主要内容,如果未能解决你的问题,请参考以下文章

python QQ邮箱自动发送邮件

利用Python自动发送邮件

5Selenium+Python自动登录163邮箱发送邮件

gmail邮件服务器无法发送邮件

Python自动化测试发送邮件太麻烦?!一起聊一聊 Python 发送邮件的3种方式

根据文件附件条件从 python 发送自动电子邮件