Using MailHost don't works

Hello,

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"
site_properties=context.portal_properties.site_properties
admin_email = site_properties.email_from_address
send_emails = "webadmin@domain.com"

# 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:
%s
"""

# format the message
message = message % (
    admin_email,
    send_emails,
    mailsubject,
    firstname,
    surname,
    company,
    position,
    country,
    email,
    context.absolute_url()
)

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

# send Mail
mhost.send(new_message)

Thanks for help o other examples!
Bieli

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 % (
    firstname,
    surname)

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

Thanks a lot!
Bieli

...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.set_charset('utf-8')
my_message['CC']= Header('someone@example.com')
my_message['BCC']= Header('secret@example.com')
my_message['X-Custom'] = Header(u'Some Custom Parameter', 'utf-8')
mailhost.send(my_message, mto, mfrom, subject)

Thanks for help and ideas.
Bieli

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
api.portal.send_email(
            recipient='some@email.com',
            subject="Mail title",
            body=u'something',
            )

(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): https://github.com/propertyshelf/ps.plone.mls/blob/master/src/ps/plone/mls/browser/developments/details.py#L289.

Hope that helps
Thomas

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')

api.portal.send_email(
            recipient='some@email.com',
            subject="Mail title",
            body=mailbody,
            )