如何发送带有隐藏的密件抄送和HTML内容的电子邮件

时间:2018-06-30 09:42:39

标签: html hyperlink hidden smtplib bcc

这是我在这里的第一篇文章:) 这是我做过的第一个python。 我正在尝试编写一个脚本,该脚本在运行时将发送电子邮件。 关于此电子邮件,对我来说有3件重要的事情:

1)没有“收件人”,只有“抄送”和“密件抄送”。

2)密件抄送-密件抄送下有很多地址,所有地址都必须获得电子邮件,但其他电子邮件地址必须隐藏。

3)我想为一个单词添加超链接。

我尝试使用MIMEMultipart,但是我找不到想让BCC正常工作的方法(我遍及google和stackoverflow都无法正常工作)。 我能够实现发送带有隐藏的密件抄送功能的电子邮件,以便按我的意愿工作,但是它不使用MIMEMultipart-仅使用smtplib,但是我似乎不明白如何集成html部分。

def send_email():
password = '*********'
bcc = ['danielofir8@gmail.com','danielofirpython@gmail.com']
from_addr = 'danielofirsales@gmail.com'
to_addr = ''
cc_addr = 'danielofirsales@gmail.com'
mail_subject = "Testing E-mail"
content = "Maintenance on the New York data center will be performed on Sunday, July 1st, 2018 at 06:30 UTC.\nWe expect to complete the maintenance by Sunday, July 1st, 2018 at 08:30 "

body = f"From: {from_addr}\r\n" + f"To: {to_addr}\r\n" + f"Cc: {cc_addr}\r\n" + f"Subject: {mail_subject}\r\n" + "\r\n"  + content
to_addr = [to_addr] + [cc_addr] + bcc

try:
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login('danielofirsales@gmail.com',password)
    server.sendmail(from_addr, to_addr ,body)
    server.quit()
    print("Success")
except:
    print("Failed")
send_email()

我找到了一个人在这里写的关于如何使用MIMEMultipart在电子邮件中发送HTML的代码,并且能够发送带有超链接的电子邮件,但是每次尝试添加密件抄送时(我发现了几种不同的方式)在线),出现错误。

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
password = '*******'
from_adr='danielofirsales@gmail.com'
to_adr='danielofirpython@gmail.com'

msg = MIMEMultipart('alternative')
msg['Subject'] = "Emailing a link"
msg['From'] = 'danielofirsales@gmail.com'
msg['To'] = 'danielofirpython@gmail.com'

html = """
<html>
<head></head>
  <body>
    <p>Maintenance on the New York data center will be performed on Sunday, July 1st, 2018 at 06:30 UTC.\nAs published on our
    <a href="http://www.google.com">Status Page</a> - We expect to complete the maintenance by Sunday, July 1st, 2018 at 08:30</p>
  </body>
</html>
"""

part1=MIMEText(html, 'html')
part2=MIMEText("Maintenance on the New York data center will be performed on Sunday, July 1st, 2018 at 06:30 UTC.\nAs published on our http://www.google.com - We expect to complete the maintenance by Sunday, July 1st, 2018 at 08:30", 'text')

msg.attach(part1)
msg.attach(part2)


server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('danielofirsales@gmail.com',password)
server.sendmail(msg['From'], msg['To'] ,msg.as_string())
server.quit()
print("Success")

是否可以使用smtplib的隐藏BCC选项和MIMEMultipart的html选项?

同样重要的是,当前该脚本应通过Gmail发送电子邮件,但在实际应用时,它将从公司的电子邮件服务器发送电子邮件。

感谢您的帮助:)

丹尼尔。

1 个答案:

答案 0 :(得分:0)

好的,所以我能够找到某种方式来实现自己想要的东西。基本上,“困难”部分是使用MIMETEXT也以smtplib格式添加侦听器。 (标题) 希望这对以后的人有所帮助:)

def send_mail():
pop_input= input("Please write the PoP's name: ")
password = '*******'



from_adr='danielofirsales@gmail.com'
cc_adr='danielofirsales@gmail.com'
to_adr = ''
mail_subject = ''

msg = MIMEMultipart('alternative')
msg['Subject'] = f"Planned Maintenance on the {pop_input} PoP"
msg['From'] = 'danielofirsales@gmail.com'
msg['Cc'] = 'danielofirsales@gmail.com'

content = f"As published on our https://www.google.com, a maintenance on {pop_input}."
html = """
<html>
<head></head>
  <body>
    <p>As published on our
    <a href="https://www.google.com">Status Page</a>, a maintenance on {POP}</p>
  </body>
</html>
"""
new_html = html.format(POP=pop_input,START_TIME=start_message,END_TIME=end_message)
header = MIMEText(f"From: {from_adr}\r\n" + f"Cc: {cc_adr}\r\n" + f"Subject: {mail_subject}\r\n" + "\r\n",'plain') 


bcc = ['danielofir8@gmail.com','danielofirpython@gmail.com']
to_adr = [to_adr] + [cc_adr] + bcc

part1=MIMEText(new_html, 'html')
part2=MIMEText(content, 'text')

msg.attach(header)
msg.attach(part1)
msg.attach(part2)

try:
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login('danielofirsales@gmail.com',password)
    server.sendmail(from_adr, to_adr ,msg.as_string())
    server.quit()
    print("Success")
except:
    print("Failed")