Assuming settings.EMAIL_HOST_USER and/or settings.EMAIL_HOST_PASSWORD is non-empty, then something like this won't work:
connection = get_connection(host='mail.host.com', port=1234, username='', password='')
send_mail(..., connection=connection)
Because EmailBackend.__init__ does this:
self.username = username or settings.EMAIL_HOST_USER
self.password = password or settings.EMAIL_HOST_PASSWORD
and therefore will default to settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD.
And then EmailBackend.open() will attempt SMTP authentication:
if self.username and self.password:
self.connection.login(self.username, self.password)
A workaround to prevent the login then would be:
connection = get_connection(host='mail.host.com', port=1234)
connection.username = '' # or None
connection.password = '' # or None
send_mail(..., connection=connection)
A possible fix might be in EmailBackend.__init__:
if username is None:
self.username = settings.EMAIL_HOST_USER
else:
self.username = username
if password is None:
self.password = settings.EMAIL_HOST_PASSWORD
else:
self.password = password
I'm not sure whether an empty string as username and password in order to prevent SMTP authentication is frowned upon and only None should be used. If that's the case, then perhaps EmailBackend shouldn't use any defaults if, for instance, a different host is passed.
Allow for empty username and/or password.