I have a python script which uses smtplib to send out an email. I am not sure what to use for mail server name in the smtplib.SMTP initialization.

On my test server I use the below and it works fine.

server = smtplib.SMTP('localhost')
server.sendmail( sender, to_addrs, msg.as_string() ) #msg is a MIMEText object

In the actual deployment, mail is setup more explicitly and the name that works is of the form smtp.a-domain-name. As an example, "smtp.mycompany.net". Temporarily I have added a parameter to my script and am now looking for a generic fix.

Is there a way to find out at run time what is the mail server name? Perhaps a Linux command that will return this name, or some readable system file that keeps track of it. (how does Linux mail command figure it out?)

Thanks a lot.

  • 1
    The mail command uses /usr/lib/sendmail, which is basically the same as using localhost. – jordanm Feb 29 '16 at 2:22
  • @jordanm thanks! I created an application-specific env var, and then also modified mail invokes to include -S smtp=${MYAPP_MAIL_SERVER:-localhost} – Dinesh Mar 1 '16 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.