coddingtonbear / django-mailbox

Import mail from POP3, IMAP, local email mailboxes or directly from Postfix or Exim4 into your Django application automatically.
MIT License
356 stars 164 forks source link

Exception 'str' object has no attribute 'from_email' when calling Message.reply() #285

Closed joshpark23 closed 6 months ago

joshpark23 commented 6 months ago

Issue description

When calling the reply() method on a Message instance, an exception is thrown with the message "'str' object has no attribute 'from_email'".

Proposed solution

Lines 658-662 in models.py, update implementation to avoid accessing the property not found on the Message model. https://github.com/coddingtonbear/django-mailbox/blob/f4a0782d3df6fb54ce474448050764cc28b64ecd/django_mailbox/models.py#L658C1-L659C1

Note

This property is found on the mailbox model but not the message, not sure why it's being accessed here. Happy to raise the PR

pfouque commented 6 months ago

Hi @joshpark23, I was about to ask if you were properly calling this method with an instance of EmailMessage? Did you find a solution? Do you think something would help clarify the usage of that method? Thanks

joshpark23 commented 5 months ago

Hey @pfouque appreciate the reply -- I wasn't passing an instance of EmailMessage to the reply method which was causing the error

Depending on any backwards compatibility requirements, adding type hints (>= python 3.5) could be helpful. Something less breaking could be an update to the doc string specifying the expected type of the message param

pfouque commented 5 months ago

I like typing, but I'm not sure we are ready to do that.

Maybe adding a good old parameter check would have been enough.

        if not isinstance(message, EmailMessage):
            raise ValueError('Message must be an instance of EmailMessage')
enaut commented 3 months ago

Could someone provide me with a working example? I fail to get it to work:

from email.message import EmailMessage
from django_mailbox.models import Message
def signal_handler(sender, message, **args):
    rpl = Message( subject="pong",  body="pongpong" )
    message.reply(rpl)

I tried with EmailMessage but I cannot instantiate that and over all I'm confused.

pfouque commented 3 months ago

Hi @enaut,

Have you tried with Django's EmailMessage?

from django.core.mail import EmailMessage
from django_mailbox.models import Message

def signal_handler(sender, message, **args):
    rpl = EmailMessage( subject="pong",  body="pongpong" )
    message.reply(rpl)
enaut commented 3 months ago

Thank you! Sometimes I miss the obvious! VsCode did not suggest that import, so I was not aware it might exist…

pfouque commented 3 months ago

It means it wasn't obvious enough! ;) We should improve the documentation of this function

pfouque commented 3 months ago

Adding this: https://github.com/coddingtonbear/django-mailbox/pull/293 Feel free to comment! I hope it will be more clear (after updating the documentation and releasing a new version)