Using Mailer to send email

187 views
Skip to first unread message

Christian Varas

unread,
May 29, 2022, 2:56:29 PM5/29/22
to py4web
Hi, how can I use Mailer to send emails from the controller ?
I tried something like this:

from py4web.utils.mailer import Mailer

def some_func():

    Mailer.send('xxx@ mail.com','Hola', 'hjhohoho')
return


ERROR:root:Traceback (most recent call last):
  File "/Users/chris/Library/Mobile Documents/com~apple~CloudDocs/develop/py4web/py4web/core.py", line 935, in wrapper
    ret = func(*func_args, **func_kwargs)
  File "/Users/chris/Library/Mobile Documents/com~apple~CloudDocs/develop/py4web/py4web/core.py", line 920, in wrapper
    raise context["exception"]
  File "/Users/chris/Library/Mobile Documents/com~apple~CloudDocs/develop/py4web/py4web/core.py", line 899, in wrapper
    context["output"] = func(*args, **kwargs)
  File "/Users/chris/Library/Mobile Documents/com~apple~CloudDocs/develop/py4web/apps/climbersoul/administracion.py", line 114, in contacto_admin
    Mailer.send('x...@mail.com','Hola', 'hjhohoho')
  File "/Users/chris/Library/Mobile Documents/com~apple~CloudDocs/develop/py4web/py4web/utils/mailer.py", line 372, in send
    sender = sender or self.settings.sender
AttributeError: 'str' object has no attribute 'settings'

How could I pass the settings to Mailer?


Cheers.
Chris.

Jim Steil

unread,
May 31, 2022, 1:47:27 PM5/31/22
to py4web
I have the following in common.py.  (I think it comes if you create your app from _scaffold)

#  configure email
if settings.SMTP_SERVER:
    auth.sender = Mailer(
        server=settings.SMTP_SERVER,
        sender=settings.SMTP_SENDER,
        login=settings.SMTP_LOGIN,
        tls=settings.SMTP_TLS,
        ssl=settings.SMTP_SSL,
    )

Then, in my controller I use auth.sender.send() - something like this

#
#  I like to use templates to build html formatted email bodies - the render function allows you to use the py4web templating system to craft your emails - 
#
body = render(
    filename=os.path.join(TEMPLATE_FOLDER, "email/default.html"),
    context=dict(
        template_folder=TEMPLATE_FOLDER,
        message=XML(message),
    ),
    delimiters="[[ ]]",
)

auth.sender.send(
    to=['list', 'of', 'email', 'addresses'],
    subject='This is my email subject',
    body=body,
    sender="sen...@mydomain.com",
)

-Jim

Christian Varas

unread,
May 31, 2022, 7:57:20 PM5/31/22
to Jim Steil, py4web
thanks Jim! 

cheers.
Chris.

--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/py4web/18c2908f-d95a-4ef8-9e26-a49a601aac6dn%40googlegroups.com.

klaus z

unread,
Jun 5, 2022, 2:53:49 AM6/5/22
to py4web
thanks Jim, 
it seems "message" has been exchanged through "body". Now it works with the labels (which I had not used before).
Klaus

Reply all
Reply to author
Forward
0 new messages