Using MailHost don't works


I have a little python script for sending information to the site admin, but it doesn't works.
I got this error:

MailHostError: No message recipients designated

Changed the use of MailHost from Plone 2 to Plone 4.3? Are there other examples?

# new_message = convert_string(message, 'utf8', 'iso-8859-15')
def convert_string(s, enc_from, enc_to):
  return unicode(s, enc_from).encode(enc_to)

# Variables
mhost = context.MailHost
mailsubject = "New information"
admin_email = site_properties.email_from_address
send_emails = ""

# get data from formular
firstname = context.REQUEST.get('firstname')
surname = context.REQUEST.get('surname')
company = context.REQUEST.get('company')
position = context.REQUEST.get('position')
country = context.REQUEST.get('country')
email = context.REQUEST.get('email')

# the message format, %s will be filled in from data
message = """
From: %s
To: %s, 
Subject: %s

New Information

First name: %s
Surname:    %s
Company:    %s
Position:   %s
Country:    %s
Email:      %s

From URL:

# format the message
message = message % (

# encode Text from utf8 to iso-8859
new_message = convert_string(message, 'utf8', 'iso-8859-15')

# send Mail

Thanks for help o other examples!

This error message indicates that the send was unable to determine the recipients for the message. In principle, send should be able to determine them from the To: message header. Seems that this fails in your case (maybe due to the trailing , of the To: header in your message template). send has the parameter mto to pass the message recipients explicitely (likely, this should get a list as value).

Thanks. Resolved. I changed to:

message = """
First name: %s
Surname:    %s

# format the message
message = message % (

# send Mail
mhost.send(message, send_emails, admin_email, mailsubject)

Thanks a lot!

...and now how to send mails like "undisclosed-recipients" using BCC?

This example don't work. I get an error (Insufficient Privileges) only adding this two first import lines:

from email import message_from_string
from email.Header import Header
my_message = message_from_string(message_body.encode('utf-8'))
my_message['CC']= Header('')
my_message['BCC']= Header('')
my_message['X-Custom'] = Header(u'Some Custom Parameter', 'utf-8')
mailhost.send(my_message, mto, mfrom, subject)

Thanks for help and ideas.

The send method expects a string (the corresponding parameter is called messageText to clearly indicate this), not a Message object. This will give you an error, though not a not enough privileges.

Apparently, you are using the code above in a restricted environment (such as a TTW script). For security reasons, you must declare which modules should be available in those contexts (because some modules can contains very dangerous material). I suggest that you move the code into an unrestricted environment, such as e.g. a view.

In a browser view, you can also use Plone.api

from plone import api
            subject="Mail title",

(not sure how to set the body to html, but it you case it is probably

body = message_from_string(message_body.encode('utf-8'))

Using plone.api's send_email does not allow BCC by default. It's not plone.api's fault, more the code in Products.MailHost. We use the following code to send emails from a form within a browser view (including BCC):

Hope that helps

does it have many limitations ?
can it send html and attachments ?

No, but we stumbled across the BCC thing recently.

Yes. It is just MailHosts's _mungeHeaders method which does some weird things. E.g., if you provide a recipient address for the send method, additional headers like CC and BCC are ignored, and worse: BCC gets removed.

BCC is an auxiliary message header used to communicate with the mail delivery component to indicate additional addressees. This component will internally add addresses and then remove the BCC header.

MailHost may not handle BCC (or may handle it incorrectly); however, you can pass additional addresses via the mto parameter of Mailhost.send.

I did test it, and came up with this (I paste it here for future reference, please correct me if it is 'wrong'):

from email.mime.text import MIMEText
from plone import api

html = <html><head></head><body><h1>hello</h1><body></html>
mailbody = MIMEText(html, 'html')

            subject="Mail title",