Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Resend documentation and checks #293

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions django_mailbox/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def _get_dehydrated_message(self, msg, record):
new = EmailMessage()
if (
msg.is_multipart()
and not 'attachment' in msg.get('Content-Disposition', '')
and 'attachment' not in msg.get('Content-Disposition', '')
):
for header, value in msg.items():
new[header] = value
Expand Down Expand Up @@ -647,14 +647,24 @@ def to_addresses(self):
return addresses

def reply(self, message):
"""Sends a message as a reply to this message instance.
"""Sends an EmailMessage as a reply to this message instance::

from django.core.mail import EmailMessage

message.reply(
EmailMessage(subject="pong", body="pongpong")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to also add the to field so that the email was actually sent.

from django.core import mail

def reply_to_message(message, subject, messagebody):
    rpl = mail.EmailMessage(
        subject=subject,
        body=messagebody,
        to=[message.from_header],
    )
    message.reply(rpl)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though it might be worth adding that to the reply method?

if not to:
  add to from reply_to or from_header

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting!
Is it something to document or should we expect the "reply" function to set it automatically...
WDYT @Pietro395 ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! Is it something to document or should we expect the "reply" function to set it automatically... WDYT @Pietro395 ?

Interesting observation @enaut
In my opinion since you are using the reply function I would expect the “to” to be set automatically if not specified.

I think that's what a user would expect

)

Although Django's e-mail processing will set both Message-ID
and Date upon generating the e-mail message, we will not be able
to retrieve that information through normal channels, so we must
pre-set it.

"""
from django.core.mail import EmailMessage as DjangoEmailMessage

if not isinstance(message, DjangoEmailMessage):
raise ValueError('Message must be an instance of django.core.mail.EmailMessage')

if not message.from_email:
if self.mailbox.from_email:
message.from_email = self.mailbox.from_email
Expand Down Expand Up @@ -888,7 +898,6 @@ def __str__(self):
return f'{self.get_filename()}: {self.document.url}'
return self.get_filename()


class Meta:
verbose_name = _('Message attachment')
verbose_name_plural = _('Message attachments')
5 changes: 4 additions & 1 deletion django_mailbox/tests/test_process_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ def test_message_reply(self):
)
msg = self.mailbox.record_outgoing_message(email_object.message())

with self.assertRaises(ValueError):
msg.reply(Message(subject="ping", body="pong"))

self.assertTrue(msg.outgoing)

actual_from = '[email protected]'
Expand Down Expand Up @@ -439,7 +442,7 @@ def test_message_compressed(self):

msg = self.mailbox.process_incoming_message(message)

actual_email_object = msg.get_email_object()
_actual_email_object = msg.get_email_object()

self.assertTrue(msg.eml.name.endswith('.eml.gz'))

Expand Down
2 changes: 1 addition & 1 deletion django_mailbox/transports/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def get_email_from_bytes(self, contents):
return message

def close(self):
pass
pass
2 changes: 1 addition & 1 deletion django_mailbox/transports/imap.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def close(self):
try:
self.server.close()
self.server.logout()
except (imaplib.IMAP4.error, OSError) as e:
except (imaplib.IMAP4.error, OSError) as e:
logger.warning(f'Failed to close IMAP connection, ignoring: {e}')
pass

Expand Down
29 changes: 15 additions & 14 deletions docs/topics/mailbox_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ POP3 and IMAP as well as local file-based mailboxes.

.. table:: 'Protocol' Options

============ ================ ====================================================================================================================================================================
Mailbox Type 'Protocol':// Notes
============ ================ ====================================================================================================================================================================
POP3 ``pop3://`` Can also specify SSL with ``pop3+ssl://``
IMAP ``imap://`` Can also specify SSL with ``imap+ssl://`` or STARTTLS with ``imap+tls``; additional configuration is also possible: see :ref:`pop3-and-imap-mailboxes` for details.
Gmail IMAP ``gmail+ssl://`` Uses OAuth authentication for Gmail's IMAP transport. See :ref:`gmail-oauth` for details.
Office365 API``office365://`` Uses OAuth authentication for Office365 API transport. See :ref:`office365-oauth` for details.
Maildir ``maildir://``
Mbox ``mbox://``
Babyl ``babyl://``
MH ``mh://``
MMDF ``mmdf://``
Piped Mail *empty* See :ref:`receiving-mail-from-exim4-or-postfix`
============ ================ ====================================================================================================================================================================
================ ================ ====================================================================================================================================================================
Mailbox Type 'Protocol':// Notes
================ ================ ====================================================================================================================================================================
POP3 ``pop3://`` Can also specify SSL with ``pop3+ssl://``
IMAP ``imap://`` Can also specify SSL with ``imap+ssl://`` or STARTTLS with ``imap+tls``; additional configuration is also possible: see :ref:`pop3-and-imap-mailboxes` for details.
Gmail IMAP ``gmail+ssl://`` Uses OAuth authentication for Gmail's IMAP transport. See :ref:`gmail-oauth` for details.
Office365 API ``office365://`` Uses OAuth authentication for Office365 API transport. See :ref:`office365-oauth` for details.
Maildir ``maildir://`` *empty*
Mbox ``mbox://`` *empty*
Babyl ``babyl://`` *empty*
MH ``mh://`` *empty*
MMDF ``mmdf://`` *empty*
Piped Mail *empty* See :ref:`receiving-mail-from-exim4-or-postfix`
================ ================ ====================================================================================================================================================================


.. warning::
Expand Down Expand Up @@ -117,6 +117,7 @@ Build your URI accordingly::


.. _office365-oauth:

Office 365 API
-------------------------------------

Expand Down
Loading