如何解决使用 python smtplib
我有一个 python 脚本,用于将邮件发送到邮件 ID 列表。 我现在正在尝试从别名邮件 ID 发送邮件。我已经在 GSuite 中创建了别名邮件。 我现在有以下代码:
user = 'loginmail@example.com' # Email userID
password = 'password' # Email password
from_addr = 'alias@example.com'
from_name = 'User Name'
recipients_addr = 'recipient@example.com'
subject = 'This is subject'
body = "this is mail body"
file_path = [file1,file2]
send_email(user,password,from_addr,from_name,recipients_addr,subject,body,file_path)
send_email() 函数:
def send_email(user,cc_addr,files_path=None,server='smtp.gmail.com'):
# assert isinstance(recipents_addr,list)
FROM = from_addr
FROMNAME = from_name
TO = recipients_addr if isinstance(recipients_addr,list) else recipients_addr.split(' ')
CC = cc_addr if isinstance(cc_addr,list) else cc_addr.aplit(' ')
PASS = password
SERVER = server
SUBJECT = subject
BODY = body
msg = mime_init(FROM,FROMNAME,TO,CC,SUBJECT,BODY)
for file_path in files_path or []:
with open(file_path,"rb") as fp:
part = MIMEBase('application',"octet-stream")
part.set_payload((fp).read())
# Encoding payload is necessary if encoded (compressed) file has to be attached.
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= %s" % os.path.basename(file_path))
msg.attach(part)
if SERVER == 'localhost': # send mail from local server
# Start local SMTP server
server = smtplib.SMTP(SERVER)
text = msg.as_string()
server.send_message(msg)
else:
# Start SMTP server at port 587
server = smtplib.SMTP(SERVER,587)
server.starttls()
# Enter login credentials for the email you want to sent mail from
server.login(user,PASS)
text = msg.as_string()
# Send mail
server.sendmail(FROM,text)
server.quit()
print('Mail Sent!')
mime_init() 函数:
def mime_init(from_addr,body):
msg = MIMEMultipart()
msg['From'] = formataddr((from_name,from_addr))
msg['To'] = ','.join(recipients_addr)
msg['CC'] = ','.join(cc_addr)
msg['Subject'] = subject
msg.attach(MIMEText(body,'plain'))
return msg
我什至尝试将别名邮件作为电子邮件用户 ID 和发件人地址传递,但它不起作用。
我正在尝试从别名邮件 ID 发送邮件。有人可以帮我吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。