Skip to content

Commit

Permalink
The best eMailer API in Basic
Browse files Browse the repository at this point in the history
  • Loading branch information
prrvchr committed Nov 4, 2023
1 parent c9b8bfd commit 91b8155
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 70 deletions.
Binary file modified eMailerOOo.oxt
Binary file not shown.
Binary file modified source/eMailerOOo/dist/eMailerOOo.oxt
Binary file not shown.
5 changes: 3 additions & 2 deletions source/eMailerOOo/resource/eMailer_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,9 @@
2020=MailMessage._getBodyData()
2021=Unable to compose email: %s, the transferable does not provide the data in a correct type: %s

2030=MailMessage._getAttachmentData()
2031=Unable to attach file: %s to email: %s, the transferable does not provide the data in a correct type: %s
2030=MailMessage._getMessageAttachment()
2031=Unable to attach file: %s to email: %s, the transferable seem to be empty...
2032=Unable to attach file: %s to email: %s, the transferable does not provide the data in a correct type: %s

3000=Transferable.getTransferData()
3001=Unable to get data transfer, the data flavor with mime type: %s and type: %s is not supported...
Expand Down
5 changes: 3 additions & 2 deletions source/eMailerOOo/resource/eMailer_fr_FR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,9 @@
2020=MailMessage._getBodyData()
2021=Impossible de r\u00e9diger le courriel: %s, le transf\u00e9rable ne fournit pas les données dans un type reconnu: %s

2030=MailMessage._getAttachmentData()
2031=Impossible de joindre le fichier: %s au courriel: %s, le transf\u00e9rable ne fournit pas les données dans un type reconnu: %s
2030=MailMessage._getMessageAttachment()
2031=Impossible de joindre le fichier: %s au courriel: %s, le transf\u00e9rable semble vide...
2032=Impossible de joindre le fichier: %s au courriel: %s, le transf\u00e9rable ne fournit pas les données dans un type reconnu: %s

3000=Transferable.getTransferData()
3001=Impossible d'obtenir le transfert de donn\u00e9es, le type de donn\u00e9es avec le type mime: %s et le type: %s n'est pas pris en charge...
Expand Down
97 changes: 32 additions & 65 deletions source/eMailerOOo/service/MailMessage.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,6 @@
#!
# -*- coding: utf-8 -*-

# *************************************************************
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# *************************************************************

# Caolan McNamara [email protected]
# a simple email mailmerge component

# manual installation for hackers, not necessary for users
# cp mailmerge.py /usr/lib/openoffice.org2.0/program
# cd /usr/lib/openoffice.org2.0/program
# ./unopkg add --shared mailmerge.py
# edit ~/.openoffice.org2/user/registry/data/org/openoffice/Office/Writer.xcu
# and change EMailSupported to as follows...
# <prop oor:name="EMailSupported" oor:type="xs:boolean">
# <value>true</value>
# </prop>

"""
╔════════════════════════════════════════════════════════════════════════════════════╗
║ ║
Expand Down Expand Up @@ -73,7 +39,6 @@

from com.sun.star.mail import XMailMessage2

from emailer import createService
from emailer import getConfiguration
from emailer import getCurrentLocale
from emailer import getLogger
Expand All @@ -95,7 +60,6 @@
from email.utils import make_msgid
from email.utils import parseaddr

import six
import traceback

# pythonloader looks for a static g_ImplementationHelper variable
Expand All @@ -108,24 +72,23 @@ class MailMessage(unohelper.Base,
XServiceInfo):
def __init__(self, ctx, to='', sender='', subject='', body=None, attachment=None):
self._ctx = ctx
self._logger = getLogger(ctx, g_mailservicelog)
self._mtf = getMimeTypeFactory(ctx)
self._local = getCurrentLocale(ctx)
self._recipients = [to]
self._ccrecipients = []
self._bccrecipients = []
self._charset = 'charset'
self._encoding = 'UTF-8'
self._attachments = []
if attachment is not None:
self._attachments.append(attachment)
self._language = None
self._sname, self._saddress = parseaddr(sender)
host = self._saddress.replace('@', '.')
self._messageid = make_msgid(None, host)
self.MessageId = make_msgid(None, host)
self.ThreadId = ''
self.ReplyToAddress = sender
self.Subject = subject
self.Body = body
self._attachments = []
self._charset = 'charset'
self._encoding = 'utf-8'
if attachment is not None:
self._attachments.append(attachment)
self.ThreadId = ''

@property
def SenderName(self):
Expand All @@ -135,13 +98,6 @@ def SenderName(self):
def SenderAddress(self):
return self._saddress

@property
def MessageId(self):
return self._messageid
@MessageId.setter
def MessageId(self, messageid):
self._messageid = messageid

def addRecipient(self, recipient):
self._recipients.append(recipient)

Expand Down Expand Up @@ -204,7 +160,7 @@ def _getMessage(self):
if len(data):
body['Content-Type'] = mimetype
body['MIME-Version'] = '1.0'
data = self._getBodyData(data, encoding).decode(encoding)
data = self._getBodyData(data, encoding)
c = Charset(encoding)
c.body_encoding = QP
body.set_payload(data, c)
Expand All @@ -213,7 +169,7 @@ def _getMessage(self):
# FIXME: We need to check if the body has been parsed,
# FIXME: if not we raise a UnsupportedFlavorException
if not parsed:
msg = self._logger.resolveString(2001, self.Subject)
msg = self._getLogger().resolveString(2001, self.Subject)
raise UnsupportedFlavorException(msg, self)
if self.hasAttachments():
message = MIMEMultipart()
Expand Down Expand Up @@ -247,21 +203,21 @@ def _getMessage(self):
# FIXME: We need to check if the attachments have a ReadableName,
# FIXME: if not we raise a MailMessageException
if not name:
msg = self._logger.resolveString(2002, self.Subject)
msg = self._getLogger().resolveString(2002, self.Subject)
raise MailMessageException(msg, self, ())
content = attachment.Data
flavors = content.getTransferDataFlavors()
# FIXME: We need to check if the attachments have at least
# FIXME: one DataFlavor, if not we raise a UnsupportedFlavorException
if not isinstance(flavors, tuple) or not len(flavors):
msg = self._logger.resolveString(2003, name, self.Subject)
msg = self._getLogger().resolveString(2003, name, self.Subject)
raise UnsupportedFlavorException(msg, self)
flavor = flavors[0]
msgattachment, encoding = self._getMessageAttachment(content, flavor, name)
encode_base64(msgattachment)
msgattachment.add_header('Content-Disposition',
'attachment',
filename=(encoding, self._local.Language, name))
filename=(encoding, self._getLanguage(), name))
message.attach(msgattachment)
return message

Expand All @@ -270,32 +226,35 @@ def _getTransferData(self, transferable, flavor):
data = transferable.getTransferData(flavor)
if flavor.DataType == uno.getTypeByName(interface):
if not hasInterface(data, interface):
msg = self._logger.resolveString(2011, self.Subject, interface, flavor.DataType.typeName)
msg = self._getLogger().resolveString(2011, self.Subject, interface, flavor.DataType.typeName)
raise UnsupportedFlavorException(msg, self)
data = getStreamSequence(data)
return data

def _getBodyData(self, data, encoding):
# Normally it's a bytesequence, get raw bytes
# If it's a byte sequence, decode it
if isinstance(data, uno.ByteSequence):
data = data.value
# If it's a string, get it as 'encoding' bytes
data = data.value.decode(encoding)
# If it's a string, nothing to do
elif isinstance(data, str):
data = data.encode(encoding)
pass
# No data is available, we need to raise an Exception
else:
msg = self._logger.resolveString(2021, self.Subject, repr(type(data)))
msg = self._getLogger().resolveString(2021, self.Subject, repr(type(data)))
raise UnsupportedFlavorException(msg, self)
return data

def _getMessageAttachment(self, content, flavor, name):
data = self._getTransferData(content, flavor)
if not len(data):
msg = self._getLogger().resolveString(2031, name, self.Subject)
raise UnsupportedFlavorException(msg, self)
mct = self._mtf.createMimeContentType(flavor.MimeType)
if mct.hasParameter(self._charset):
encoding = mct.getParameterValue(self._charset)
else:
# Default to utf-8
encoding = self._encoding
data = self._getTransferData(content, flavor)
# Normally it's a bytesequence, get raw bytes
if isinstance(data, uno.ByteSequence):
data = data.value
Expand All @@ -304,12 +263,20 @@ def _getMessageAttachment(self, content, flavor, name):
data = data.encode(encoding)
# No data is available, we need to raise an Exception
else:
msg = self._logger.resolveString(2031, name, self.Subject, repr(type(data)))
msg = self._getLogger().resolveString(2032, name, self.Subject, repr(type(data)))
raise UnsupportedFlavorException(msg, self)
msgattachment = MIMEBase(mct.getMediaType(), mct.getMediaSubtype())
msgattachment.set_payload(data)
return msgattachment, encoding

def _getLanguage(self):
if self._language is None:
self._language = getCurrentLocale(self._ctx).Language
return self._language

def _getLogger(self):
return getLogger(self._ctx, g_mailservicelog)

# XServiceInfo
def supportsService(self, service):
return g_ImplementationHelper.supportsService(g_ImplementationName, service)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(self, ctx, logger):
self._detection = getTypeDetection(ctx)
self._charset = 'charset'
self._encode = False
self._default = 'utf-8'
self._default = 'UTF-8'
self._encoding = self._default
self._uiname = 'E Documents'
self._mimetype = 'application/octet-stream'
Expand Down

0 comments on commit 91b8155

Please sign in to comment.