I'm wondering which approach is better for sending mail with different body using Python:
send each mail using separate functions
using one function and select body message with if-else statement
First case:
FROM = *from_email_address*
def send_mail_notify():
SUBJECT = *some_subject_for_notification_event*
TEXT = *any_text*
msg = EmailMessage()
msg['From'] = FROM
msg['To'] = *to_email_address*
msg['Subject'] = SUBJECT
msg.set_content(TEXT)
...(initialize connection to mail server, etc.)
FROM = *from_email_address*
def send_mail_error():
SUBJECT = *some_subject_for_error_event*
TEXT = *any_text*
msg = EmailMessage()
msg['From'] = FROM
msg['To'] = *email_address*
msg['Subject'] = SUBJECT
msg.set_content(TEXT)
...(initialize connection to mail server, etc.)
Second case:
FROM = *from_email_address*
def send_mail(param):
if param == "notify":
SUBJECT = *some_subject_for_notification_event*
TEXT = *any_text*
elif param == "error":
SUBJECT = *some_subject_for_error_event*
TEXT = *any_text*
msg = EmailMessage()
msg['From'] = FROM
msg['To'] = *email_address*
msg['Subject'] = SUBJECT
msg.set_content(TEXT)
...(initialize connection to mail server, etc.)
You can create an unlimited functions for different types of subject and text and you don't need to check the condition with if-else statement:
FROM = *from_email_address*
def notify():
return {"SUBJECT":"notification", "TEXT": "new message"}
def error():
return {"SUBJECT":"error: message dont send", "TEXT": "error message text"}
def send_mail(param):
msg = EmailMessage()
msg['From'] = FROM
msg['To'] = *email_address *
msg['Subject'] = globals().get(param)()["SUBJECT"]
msg.set_content(globals().get(param)()["TEXT"])
The param value must be the same as the name of the function.
Related
I'm writing a Python program that generates an invitation. The code below works approximately.
It send the invitation, the invitation is recognized by an Outlook client, but is not treated by Mac OS calendar.
What is missing or what's wrong ?
Thanks
subject = "Mail subject"
sender_email = "someone#domain.com"
to = ["someone_else#domain.com"]
cc = []
# Create the plain-text and HTML version of your message
text = """\
Text message version
"""
html = """\
<!DOCTYPE html><html><head></head><body><p>The HTML message</p></body></html>
"""
# Create a multipart message and set headers
message = MIMEMultipart("mixed")
message["From"] = sender_email
message["To"] = ", ".join(to)
message["Cc"] = ", ".join(cc)
message["Subject"] = subject
message['Date'] = formatdate(localtime=True)
message_alternative = MIMEMultipart('alternative')
message_related = MIMEMultipart('related')
message_related.attach(MIMEText(html, 'html'))
message_alternative.attach(message_related)
#message_alternative.attach((text, 'plain'))
# --- Begin ical integration ---
ical = CreateIcs() # this function return an ICS content
ical_atch = MIMEBase("text", "calendar", method="REQUEST", name="invite.ics")
ical_atch.set_payload(ical)
encoders.encode_quopri(ical_atch)
message_alternative.attach(ical_atch)
# --- END ical integration ---
message.attach(message_alternative)
# convert message to string
text = message.as_string()
# Send mail
host='127.0.0.1'
port=25
context = None
with smtplib.SMTP(host=host, port=port) as server:
server.sendmail(sender_email, message["To"].split(",") + message["Cc"].split(","), text)
print(f"Mail sent TO : {message['To'].split(',')}, CC : {message['Cc'].split(',')}")
I am trying to write a very basic email sending script. Here is my code ..
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg.set_content("Test message.")
msg['Subject'] = "Test Subject!!!"
msg['From'] = "myemail#gmail.com"
email_list = ["xyz#gmail.com", "abc#gmail.com"]
for email in email_list:
msg['To'] = email
server = smtplib.SMTP(host='smtp.gmail.com', port=587)
server.starttls()
server.login("myemail#gmail.com", "mypassword")
server.send_message(msg)
server.quit()
the script should send mail to multiple recipients so, I need to change the msg['To'] field when iterating through loop But I get the following error in traceback bellow.
Traceback (most recent call last):
File "exp.py", line 66, in <module>
msg['To'] = email
File "/usr/lib/python3.8/email/message.py", line 407, in __setitem__
raise ValueError("There may be at most {} {} headers "
ValueError: There may be at most 1 To headers in a message
How do I solve ? Please help. Thank you..
Clean the 'To' property of the message.
for email in email_list:
msg['To'] = email
server = smtplib.SMTP(host='smtp.gmail.com', port=587)
server.starttls()
server.login("myemail#gmail.com", "mypassword")
server.send_message(msg)
server.quit()
del msg['To]
Below is the code that throws the exception: (\Python385\Lib\email\message.py)
def __setitem__(self, name, val):
"""Set the value of a header.
Note: this does not overwrite an existing header with the same field
name. Use __delitem__() first to delete any existing headers.
"""
max_count = self.policy.header_max_count(name)
if max_count:
lname = name.lower()
found = 0
for k, v in self._headers:
if k.lower() == lname:
found += 1
if found >= max_count:
raise ValueError("There may be at most {} {} headers "
"in a message".format(max_count, name))
self._headers.append(self.policy.header_store_parse(name, val))
Not knowing the inner working of the EmailMessage class, what I can assume is that every call to __setitem__ writes to the head of the email message, so by calling it in a loop, the header is being written multiple times, what I'd recommend is that you make an email message for every email you'll send, but create only one server:
server = smtplib.SMTP(host='smtp.gmail.com', port=587)
server.starttls()
server.login("myemail#gmail.com", "mypassword")
email_list = ["xyz#gmail.com", "abc#gmail.com"]
for email in email_list:
msg = EmailMessage()
msg.set_content("Test message.")
msg['Subject'] = "Test Subject!!!"
msg['From'] = "myemail#gmail.com"
msg['To'] = email
server.send_message(msg)
server.quit()
Only if you need for the messages to be sent separately. If you want to send the same message to everyone at the same time you could do something like
msg['To'] = ', '.join(email_list)
If you have a list of addresses, and some of them include a name/title, then I think this is the correct way to do it. Please note that parseaddr + formataddr pair may not be needed, but parseaddr can correct some malformed recipients.
from email.header import Charset
from email.message import EmailMessage, MIMEPart
from email.utils import formataddr, parseaddr
test_recipients = [
"Mr. John Doe <johndoe#example.com>",
"Mr. Jane Doe <janedoe#example.com>",
"somebody#example.com"
]
to_header= []
for raw_address in (test_recipients):
# Parse and recreate
title, email = parseaddr(raw_address)
if title and email:
to_header.append(f"{title} <{email}>")
elif email:
to_header.append(email)
# Encode after join
message.add_header("To", Charset("utf-8").header_encode(", ".join(to_header)))
if you just delete
server.quit() from loop
and add
del msg['to']
then there is no error
I have a code which sends an email that uses HTML for templating.
All of the recipients get the message, but all of them are BCC.
def sendMail(to, cc, bcc, template, bodyParams, subjectParams):
global connection
if not testCurrentConnection(connection):
connect(currentConnectionName)
msg = MIMEMultipart('alternative')
subject, fromEmail, body = templateController.readTemplate(template)
header = 'To: ' + to + '\n' + 'Cc: ' + cc + 'From: ' + fromEmail + '\n' + 'Subject: ' + subject + '\n'
msg.attach(MIMEText(header, 'text'))
if subjectParams:
msg['Subject'] = templateController.replaceTextWithParams(subject, subjectParams)
else:
msg['Subject'] = subject
if bodyParams:
msg['Body'] = templateController.replaceTextWithParams(body, bodyParams)
else:
msg['Body'] = body
msg['From'] = fromEmail
msg['To'] = to
msg['Cc'] = cc
# no need to specify bcc here
msg.attach(MIMEText(msg['Body'], 'html'))
connection.sendmail(msg['From'], [msg['To'], msg['Cc'], bcc], msg.as_string())
del msg
I'm calling the function like this:
smtpController.sendMail("myMail#gmail.com", "ccMail#gmail.com", "", "email.html", None, None)
(The last two variables are actually a dict with key-value mapping used to populate the HTML, but the problem reproduces without them)
I read that I need to add header to my message to prevent it but for some reason, adding the header doesn't change anything (lines 7-8 in the above code).
What am I missing?
OK, I don't know how it makes a difference, but I fixed it by moving msg['To'] = to and msg['Cc'] = cc up, before Subject and Body. I completely removed the header.
def sendMail(to, cc, bcc, template, bodyParams, subjectParams):
global connection
if not testCurrentConnection(connection):
connect(currentConnectionName)
subject, fromEmail, body = templateController.readTemplate(template)
msg = MIMEMultipart('alternative')
msg['From'] = fromEmail
msg['To'] = to
msg['Cc'] = cc
if subjectParams:
msg['Subject'] = templateController.replaceTextWithParams(subject, subjectParams)
else:
msg['Subject'] = subject
if bodyParams:
msg['Body'] = templateController.replaceTextWithParams(body, bodyParams)
else:
msg['Body'] = body
msg.attach(MIMEText(msg['Body'], 'html'))
connection.sendmail(msg['From'], to.split(',') + cc.split(',') + bcc.split(','), msg.as_string())
del msg
I am trying to send screenshots along with an email message..
The message gets through fine.
In Windows Live mail it has the attachment icon. but no attachments are there.
Online in outlook it has no attachment..
msg = MIMEMultipart('alternative')
msg['Subject'] = client_name + " eBay Template " + date
msg['From'] = sender_address
msg['To'] = recipients_address
msg.preamble = 'images'
...
# attach screenshot
iways_filename = dictstr['ItemID'] + "_i-ways" + '.png'
ebay_filename = dictstr['ItemID'] + "_ebay" + '.png'
# iways
img_data = open(iways_filename, 'rb').read()
image = MIMEImage(img_data, name=os.path.basename(iways_filename))
msg.attach(image)
#ebay
img_data2 = open(ebay_filename, 'rb').read()
image = MIMEImage(img_data2, name=os.path.basename(ebay_filename))
msg.attach(image)
I get no errors..
I found the solution..
msg = MIMEMultipart('alternative')
msg['Subject'] = client_name + " eBay Template " + date
msg['From'] = sender_address
msg['To'] = recipients_address
msg.preamble = 'images'
Take away the 'alternative' and Voila!
msg = MIMEMultipart()
msg['Subject'] = client_name + " eBay Template " + date
msg['From'] = sender_address
msg['To'] = recipients_address
msg.preamble = 'images'
I'm trying to email multiple recipients using the pyton script below. I've searched the forum for answers, but have not been able to implement any of them correctly. If anyone has a moment to review my script and spot/resolve the problem it would be greatly appreciated.
Here's my script, I gather my issue is in the 'sendmail' portion, but can't figure out how to fix it:
gmail_user = "sender#email.com"
gmail_pwd = "sender_password"
recipients = ['recipient1#email.com','recipient2#email.com']
def mail(to, subject, text, attach):
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = ", ".join(recipients)
msg['Subject'] = subject
msg.attach(MIMEText(text))
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
mailServer.close()
mail("recipient1#email.com, recipient2#email.com",
"Subject",
"Message",
"attchachment")
Any insight would be greatly appreciated.
Best,
Matt
It should be more like
mail(["recipient1#email.com", "recipient2#email.com"],
"Subject",
"Message",
"attchachment")
You already have a array of recipients declared,that too globally,You can use that without passing it as an argument to mail.
I wrote this bit of code to do exactly what you want. If you find a bug let me know (I've tested it and it works):
import email as em
import smtplib as smtp
import os
ENDPOINTS = {KEY: 'value#domain.com'}
class BoxWriter(object):
def __init__(self):
pass
def dispatch(self, files, box_target, additional_targets=None, email_subject=None, body='New figures'):
"""
Send an email to multiple recipients
:param files: list of files to send--requires full path
:param box_target: Relevant entry ENDPOINTS dict
:param additional_targets: other addresses to send the same email
:param email_subject: optional title for email
"""
destination = ENDPOINTS.get(box_target, None)
if destination is None:
raise Exception('Target folder on Box does not exist')
recipients = [destination]
if additional_targets is not None:
recipients.extend(additional_targets)
subject = 'Updating files'
if email_subject is not None:
subject = email_subject
message = em.MIMEMultipart.MIMEMultipart()
message['From'] = 'user#domain.com'
message['To'] = ', '.join(recipients)
message['Date'] = em.Utils.formatdate(localtime=True)
message['Subject'] = subject
message.attach(em.MIMEText.MIMEText(body + '\n' +'Contents: \n{0}'.format('\n'.join(files))))
for f in files:
base = em.MIMEBase.MIMEBase('application', "octet-stream")
base.set_payload(open(f, 'rb').read())
em.Encoders.encode_base64(base)
base.add_header('Content-Disposition', 'attachment; filename={0}'.format(os.path.basename(f)))
message.attach(base)
conn = smtp.SMTP('smtp.gmail.com', 587)
un = 'user#gmail.com'
pw = 'test1234'
conn.starttls()
conn.login(un, pw)
conn.sendmail('user#domain.com', recipients, message.as_string())
conn.close()
I was facing the same issue, I fixed this issue now. Here is my code -
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
import datetime
def sendMail():
message = MIMEMultipart()
message["To"] = "xxxxx#xxxx.com,yyyy#yyyy.com"
message["Cc"] = "zzzzzz#gmail.com,*********#gmail.com"
message["From"] = "xxxxxxxx#gmail.com"
message["Password"] = "***************"
server = 'smtp.gmail.com:587'
try:
now = datetime.datetime.now()
message['Subject'] = "cxxdRL Table status (Super Important Message) - "+str(now)
server = smtplib.SMTP(server)
server.ehlo()
server.starttls()
server.login(message["From"], message["Password"])
server.sendmail(message["From"], message["To"].split(",") + message["Cc"].split(","), message.as_string())
server.quit()
print('Mail sent')
except:
print('Something went wrong...')
sendMail()