add option to specify HELO name/identity in SMTP client

51 views
Skip to first unread message

David Miguel Susano Pinto

unread,
Sep 7, 2022, 10:21:15 AM9/7/22
to Django developers (Contributions to Django itself)
After connecting to a SMTP server, a client identifies themselves with
a HELO/EHLO command, like so:

    HELO my-domain.example

While typically this is the client FQDN, that's not actually required
and in some setups the client needs to use some other name (it just
so happens that I'm in that situation).

Django's SMTP client defaults to `socket.getfqdn()`.

    https://github.com/django/django/blob/main/django/core/mail/backends/smtp.py#L69
    https://github.com/django/django/blob/main/django/core/mail/utils.py#L16

This is a very good default but there is no option to set it to
something else.  I would propose a new option for it.  Some possible
names are:

- `MAIL_LOCAL_HOSTNAME`: because `local_hostname` is the name of the
  argument used by `smtplib` for this.

- `MAIL_HELO_DATA`: a clearer name, `helo_data` is used in the exim
  configuration.

- `MAIL_HELO_NAME`: even more clear, `helo_name` is used in the
  postfix configuration.

A workaround for this is to setup a SMTP server locally, such as Exim
or Postfix, that simply forwards the emails from Django.

Best wishes
David

אורי

unread,
Sep 7, 2022, 10:37:35 AM9/7/22
to django-d...@googlegroups.com
Hi David,

Does and should Django connect directly to a remote SMTP server? Isn't it better to use the local mail server which will receive the email and then send it to a remote SMTP server? In my opinion, it's always better to send emails via a local mail server on the same machine. Do you have other experiences?

Thanks,
Uri Rodberg, Speedy Net.


--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/eaa81e50-cfd0-4cbd-ac75-3ad36fc50c51n%40googlegroups.com.

David Pinto

unread,
Sep 9, 2022, 8:07:15 AM9/9/22
to django-d...@googlegroups.com
‪On Wed, 7 Sept 2022 at 15:37, ‫אורי‬‎ <u...@speedy.net> wrote:‬
>
> Hi David,
>
> Does and should Django connect directly to a remote SMTP server? Isn't it better to use the local mail server which will receive the email and then send it to a remote SMTP server? In my opinion, it's always better to send emails via a local mail server on the same machine. Do you have other experiences?
>
> Thanks,
> Uri Rodberg, Speedy Net.
> אורי
> u...@speedy.net

Hi Uri

Thank you for the reply.

The mail server I'm connecting to, while not on localhost, is on an
internal network and so, somewhat local. I don't have the experience
that sending mail through such a "remote" server is any worse. I
don't have the experience that it's any better either, I just never
had any issues. It does save me from having to setup a mail server.
Anyway, as I mentioned on my proposal, having a mail server on
localhost is exactly the kind of workaround I suggest.

I understand that implementing a full-featured SMTP client may be
outside the scope of Django's mail module. I also understand that
being able to specify the identifier for the HELO command may be
behind the line of what defines a simple SMTP client. But because
this is an option exposed by Python's smtplib, I thought it would be
accepted. However, it's been 13 years since Django's added mail
support and if no one needed the ability to specify this, maybe it is
not worth the extra complication.

Either way, let me know. I can submit a simple PR that adds this
option or document that Django's SMTP client should be connecting to a
full-featured SMTP server on localhost if more complex connections are
required.

Many thanks
David
Reply all
Reply to author
Forward
0 new messages