9

I am having error when trying to send an email with python sendmail. Here is my python code: (Pas d’objet)

#! /usr/bin/python

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "my@email.com"
you = "email@gmail.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="[http://www.python.org">link</a]http://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()

When I send this email, I have this error:

Traceback (most recent call last):
  File "./mail.py", line 47, in <module>
    s.sendmail(me, you, msg.as_string())
  File "/usr/lib/python2.7/smtplib.py", line 747, in sendmail
    raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'email@gmail.com': (454, '4.7.1 <email@gmail.com>: Relay access denied')}

I really don't know why this is not working in production since it is working in a test environment. Can you help understand, please?

Dror Av.
  • 1,184
  • 5
  • 14
dmx
  • 1,862
  • 3
  • 26
  • 48
  • Are you trying to send an email via gmail? or do you actually have a `local` SMTP server? See this answer - https://stackoverflow.com/questions/10147455/how-to-send-an-email-with-gmail-as-provider-using-python – Dror Av. Aug 14 '17 at 13:31
  • @droravr No, I just want to be able to send an email, to Gmail or others – dmx Aug 14 '17 at 13:34
  • You need an SMTP server to send emails, either use Gmail if that's where you have an account or something else of the same nature. – Dror Av. Aug 14 '17 at 13:38
  • @droravr Gmail is just an example. In my script, I use a local mail server of my company. I have a test machine and this is working correctly on it. But not in production env. Basically, I just need to send HTML email with `sendmail`. This seems to be the easiest way to do this. – dmx Aug 14 '17 at 13:44
  • Oh, you got me confused as you answered "No" when I asked if you have a local server. On the test machine, are you also sending it to `email@gmail.com` ? – Dror Av. Aug 14 '17 at 13:51
  • @droravr sorry for confusing you. In my test env, it is working for Gmail and my company mail. – dmx Aug 14 '17 at 13:59
  • It doesn't seem like a python problem, you can see answers here - https://stackoverflow.com/questions/34830169/sending-mails-via-python for the same problem. – Dror Av. Aug 14 '17 at 14:06
  • Perhaps more information is required. This error is possibly caused by domain configuration. - Is your domain for email-sending identical on TEST and PROD? - Depending on what handles your email sending on PROD vs TEST, it could be a permissions issue – Luke X Aug 14 '17 at 15:14
  • normally it is identical. – dmx Aug 16 '17 at 12:43

1 Answers1

2

The Relay access denied message is important: It sounds like you're successfully making an SMTP connection (to localhost in your sample code), but that machine is refusing to accept the message: You likely need to adjust either,

  • your mail server configuration on localhost
  • your code to send email in such a way that your localhost mail server will accept it.

In particular note that mail to port 25 (the default port for SMTP) is typically for incoming email. In your case you want the mail server to accept the message and send it on elsewhere ("relaying", thus the error message). It seems that your mail server (localhost here) isn't setup to accept messages for non-local domains... at least not on port 25.

The other important use of SMTP is "mail submission", and this is typically setup on a different port (normally 587, or 465 if SSL encrypted). See if this is the case by using telnet on the command line; also use EHLO <some_hostname> to see what the server offers (interesting for your issue are AUTH and STARTTLS), if it responds on the port, for example,

$ telnet localhost 587
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 localhost.example.org ESMTP Postfix (Ubuntu)
ehlo localhost.example.org
250-localhost.example.org
250-VARIOUS_FEATURES_ON_LINES_LIKE_THIS
250-STARTTLS
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
QUIT
221 2.0.0 Bye
Connection closed by foreign host.

Do this for port 25 too and compare the results: That should give you clues on how to proceed - maybe you need to AUTH for example, or use port 587, or you may need to adjust your (localhost) mail server configuration so it acts as a "smarthost" to forward email out to the 'net.

Andrew Richards
  • 1,392
  • 11
  • 18