Back to index

plone3  3.1.7
mail.py
Go to the documentation of this file.
00001 from zope.interface import Interface
00002 from zope.component import adapts
00003 from zope.component import getUtility
00004 from zope.formlib import form
00005 from zope.interface import implements
00006 from zope.schema import Int
00007 from zope.schema import Password
00008 from zope.schema import TextLine
00009 from zope.schema import ASCII
00010 from zope.app.form.browser.textwidgets import ASCIIWidget
00011 
00012 from Products.CMFCore.interfaces import ISiteRoot
00013 from Products.CMFCore.utils import getToolByName
00014 from Products.CMFDefault.formlib.schema import ProxyFieldProperty
00015 from Products.CMFDefault.formlib.schema import SchemaAdapterBase
00016 from Products.CMFPlone import PloneMessageFactory as _
00017 from Products.CMFPlone.interfaces import IPloneSiteRoot
00018 from Products.CMFPlone.utils import safe_hasattr
00019 
00020 from form import ControlPanelForm
00021 
00022 class IMailSchema(Interface):
00023     """Combined schema for the adapter lookup.
00024     """
00025 
00026     smtp_host = TextLine(title=_(u'label_smtp_server',
00027                                  default=u'SMTP server'),
00028                          description=_(u"help_smtp_server",
00029                                        default=u"The address of your local "
00030                                        "SMTP (outgoing e-mail) server. Usually "
00031                                        "'localhost', unless you use an "
00032                                        "external server to send e-mail."),
00033                          default=u'localhost',
00034                          required=True)
00035 
00036     smtp_port = Int(title=_(u'label_smtp_port',
00037                             default=u'SMTP port'),
00038                     description=_(u"help_smtp_port",
00039                                   default=u"The port of your local SMTP "
00040                                   "(outgoing e-mail) server. Usually '25'."),
00041                     default=25,
00042                     required=True)
00043 
00044     smtp_userid = TextLine(title=_(u'label_smtp_userid',
00045                                    default=u'ESMTP username'),
00046                            description=_(u"help_smtp_userid",
00047                                          default=u"Username for authentication "
00048                                          "to your e-mail server. Not required "
00049                                          "unless you are using ESMTP."),
00050                            default=None,
00051                            required=False)
00052 
00053     smtp_pass = Password(title=_(u'label_smtp_pass',
00054                                  default=u'ESMTP password'),
00055                          description=_(u"help_smtp_pass",
00056                                        default=u"The password for the ESMTP "
00057                                        "user account."),
00058                          default=None,
00059                          required=False)
00060 
00061     email_from_name = TextLine(title=_(u"Site 'From' name"),
00062                                description=_(u"Plone generates e-mail using "
00063                                               "this name as the e-mail "
00064                                               "sender."),
00065                                default=None,
00066                                required=True)
00067 
00068     email_from_address = ASCII(title=_(u"Site 'From' address"),
00069                                description=_(u"Plone generates e-mail using "
00070                                               "this address as the e-mail "
00071                                               "return address. It is also "
00072                                               "used as the destination "
00073                                               "address on the site-wide "
00074                                               "contact form."),
00075                                default=None,
00076                                required=True)
00077 
00078 
00079 class MailControlPanelAdapter(SchemaAdapterBase):
00080 
00081     adapts(IPloneSiteRoot)
00082     implements(IMailSchema)
00083 
00084     def __init__(self, context):
00085         super(MailControlPanelAdapter, self).__init__(context)
00086         self.context = getToolByName(context, 'MailHost')
00087 
00088     smtp_host = ProxyFieldProperty(IMailSchema['smtp_host'])
00089     smtp_port = ProxyFieldProperty(IMailSchema['smtp_port'])
00090 
00091     def get_smtp_userid(self):
00092         return getattr(self.context, 'smtp_userid',
00093                        getattr(self.context, 'smtp_uid', None))
00094 
00095     def set_smtp_userid(self, value):
00096         if safe_hasattr(self.context, 'smtp_userid'):
00097             self.context.smtp_userid = value
00098             #SecureMailhost 1.x also uses this:
00099             if safe_hasattr(self.context, '_smtp_userid'):
00100                 self.context._smtp_userid = value
00101         elif safe_hasattr(self.context, 'smtp_uid'):
00102             self.context.smtp_uid = value
00103 
00104     smtp_userid = property(get_smtp_userid, set_smtp_userid)
00105 
00106     def get_smtp_pass(self):
00107         return getattr(self.context, 'smtp_pass',
00108                        getattr(self.context, 'smtp_pwd', None))
00109 
00110     def set_smtp_pass(self, value):
00111         # Don't update the value, if we don't get a new one
00112         if value is not None:
00113             if safe_hasattr(self.context, 'smtp_pass'):
00114                 self.context.smtp_pass = value
00115                 #SecureMailhost 1.x also uses this:
00116                 if safe_hasattr(self.context, '_smtp_pass'):
00117                     self.context._smtp_pass = value
00118             elif safe_hasattr(self.context, 'smtp_pwd'):
00119                 self.context.smtp_pwd = value
00120 
00121     smtp_pass = property(get_smtp_pass, set_smtp_pass)
00122 
00123     def get_email_from_name(self):
00124         return getUtility(ISiteRoot).email_from_name
00125 
00126     def set_email_from_name(self, value):
00127         getUtility(ISiteRoot).email_from_name = value
00128 
00129     email_from_name = property(get_email_from_name, set_email_from_name)
00130 
00131     def get_email_from_address(self):
00132         return getUtility(ISiteRoot).email_from_address
00133 
00134     def set_email_from_address(self, value):
00135         getUtility(ISiteRoot).email_from_address = value
00136 
00137     email_from_address = property(get_email_from_address,
00138                                   set_email_from_address)
00139 
00140 
00141 class MailControlPanel(ControlPanelForm):
00142 
00143     form_fields = form.FormFields(IMailSchema)
00144     form_fields['email_from_address'].custom_widget = ASCIIWidget
00145     label = _("Mail settings")
00146     description = _("Mail settings for this site.")
00147     form_name = _("Mail settings")