That's my first post here :) This is the first python I ever made. I'm trying to write a script that will send an e-mail when run. About this e-mail there are 3 important things for me:
1) There's no "To" - only "CC" and "BCC".
2) BCC - there are a lot of addresses under BCC, all of them must get the e-mail but other e-mail addresses must be hidden.
3) I want to add a hyperlink for a word.
I have tried to use the MIMEMultipart, but I could not find a way to make the BCC work as I want (I went all over google and stackoverflow and couldn't get it to work). I was able to achieve the sending an e-mail with hidden BCC to work as I want but it's without using MIMEMultipart - using only smtplib, however I can't seem to understand how to integrate the html part.
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()
I found a code someone here wrote on how to use the MIMEMultipart to send HTML in the e-mail and was able to send the e-mail with the hyperlink, however everytime a try to add BCC (couple different ways I found online), I get an error.
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")
Is there a way to use the hidden BCC option of smtplib and the html option of MIMEMultipart?
Also important, currently the script should send e-mail through Gmail, but when actually applied it will send the e-mail from the company's e-mail server.
Thanks ahead for the help :)
Daniel.