Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have read A LOT of places of people trying to solve this problem and none of them have worked for me. I have the following script

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

msg = MIMEMultipart('alternative')
msg['Subject'] = "[Test Email]"
msg['From'] = ""
msg['To'] = ""

text = "sample Email"
html = "<html><body>sampleEmail</body></html>"
textPart = MIMEText(text, 'plain')
htmlPart = MIMEText(html, 'html')

s = smtplib.SMTP("localhost")
s.sendmail("","", msg.as_string())

When I run the script on server a (call it, everything works as expected. When I run the script on server b (call it, I get Relay Access Denied. On server b I tried to set up my own postfix instance for (and wasn't entirely successful), and I something might be messed up from that.

Everything I have read says that the local SMTP Server is not allowing to relay to (or from?) this address. Someone suggested I make sure smtpd_recipient_restrictions includes permit_mynetworks:

sudo postconf smtpd_recipient_restrictions

which printed:

smtpd_recipient_restrictions = permit_mynetworks, reject_unauth_destination

So that looks good (Could the problem be reject_unauth_destination?).

Then they said make sure the ip address is included in mynetworks so:

sudo postconf mynetworks

which prints:

mynetworks =,,,,,,

As you can see I added every domain I could think of. (note is not the real IP).

The error message says:

send: 'ehlo localhost.localdomain\r\n'
reply: '\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 10240000\r\n'
reply: '250-VRFY\r\n'
reply: '250-ETRN\r\n'
reply: '250-8BITMIME\r\n'
reply: '250 DSN\r\n'
reply: retcode (250); Msg:
SIZE 10240000
send: 'mail FROM:<> size=565\r\n'
reply: '250 2.1.0 Ok\r\n'
reply: retcode (250); Msg: 2.1.0 Ok
send: 'rcpt TO:<>\r\n'
reply: '554 5.7.1 <>: Relay access denied\r\n'
reply: retcode (554); Msg: 5.7.1 <>: Relay access denied
send: 'rset\r\n'
reply: '250 2.0.0 Ok\r\n'
reply: retcode (250); Msg: 2.0.0 Ok
Traceback (most recent call last):
  File "", line 122, in <module>
    s.sendmail("","", msg.as_string())
  File "/usr/local/lib/python2.7/", line 734, in sendmail
    raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'': (554, '5.7.1 <>: Relay access denied')}

Additional info:

> sudo postconf mydestination
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, mail.$mydomain, www.$mydomain, ftp.$mydomain
> sudo postconf relay_domains
relay_domains = $mydestination
> sudo postconf mynetworks_style
mynetworks_style = subnet
> sudo postconf relayhost
relayhost = 
>sudo postconf mydomain
mydomain =

I also tried:

sudo postconf relayhost
relayhost = [mail.$mydomain]

Postfix log says:

> sudo tail -10 /var/log/maillog
Jan 21 12:26:36 ip-50-62-42-49 postfix/smtpd[3875]: connect from localhost[::1]
Jan 21 12:26:36 ip-50-62-42-49 postfix/trivial-rewrite[3877]: warning: do not list domain in BOTH mydestination and virtual_alias_domains
Jan 21 12:26:36 ip-50-62-42-49 postfix/smtpd[3875]: NOQUEUE: reject: RCPT from localhost[::1]: 554 5.7.1 <>: Relay access denied; from=<> to=<> proto=ESMTP helo=<localhost.localdomain>
Jan 21 12:26:36 ip-50-62-42-49 postfix/smtpd[3875]: lost connection after RSET from localhost[::1]
Jan 21 12:26:36 ip-50-62-42-49 postfix/smtpd[3875]: disconnect from localhost[::1]

sudo postconf -n
alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
command_directory = /usr/sbin
config_directory = /etc/postfix
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
debug_peer_level = 1
html_directory = no
inet_interfaces = all
inet_protocols = all
mail_owner = postfix
mailq_path = /usr/bin/mailq.postfix
manpage_directory = /usr/share/man
masquerade_domains = $mydomain
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, mail.$mydomain, www.$mydomain, ftp.$mydomain
mydomain =
myhostname =
mynetworks =,,,,,,
myorigin = $mydomain
newaliases_path = /usr/bin/newaliases.postfix
queue_directory = /var/spool/postfix
readme_directory = /usr/share/doc/postfix-2.6.6/README_FILES
relayhost = [mail.$mydomain]
sample_directory = /usr/share/doc/postfix-2.6.6/samples
sendmail_path = /usr/sbin/sendmail.postfix
setgid_group = postdrop
unknown_local_recipient_reject_code = 550
virtual_alias_domains =
virtual_alias_maps = hash:/etc/postfix/virtual

I changed the IP addresses and the ipaddresses.


After Weeks of trying to get this working and thanks to everyone's assistance I found the solution. I was trying to use localhost as the relayhost, but because this is a godaddy hosted server all relays must go through a designated relayhost. All I needed to do was change the location of the SMTP. The line is now:

s = smtplib.SMTP("")
share|improve this question
What's your relay host configuration in your My past experience in similar issues point to the fact that the relay server is expecting credentials (possibly sasl authentication) from your server. It doesn't find anything and hence it is rejecting access. – Mukul Jan 21 '14 at 19:09
What's you /etc/postfix/ look like? Check out to see how to setup your relays. By default postfix is usually configured to send directly. – Russell Shingleton Jan 21 '14 at 19:09
@RussellShingleton See the end of the post. I added info from Looks like this is related to relayhost being empty. What should it be? – ajon Jan 21 '14 at 19:20
and what does postfix log say ? – Bartłomiej Zarzecki Jan 21 '14 at 19:24
@BartłomiejZarzecki I added the log to the bottom of the post. Obviously I replaced email with the address and domain with the real domain. – ajon Jan 21 '14 at 19:30
up vote 1 down vote accepted

There are few areas you (may) need to setup in the /etc/postfix/ If you are using you web host's smtp server as a relay for instance, you would want to look for the section with "relayhost =".

According to What delivery method: direct or indirect:

By default, Postfix tries to deliver mail directly to the Internet. Depending on your local conditions this may not be possible or desirable. For example, your system may be turned off outside office hours, it may be behind a firewall, or it may be connected via a provider who does not allow direct mail to the Internet. In those cases you need to configure Postfix to deliver mail indirectly via a relay host.

Examples (specify only one of the following):


relayhost =                   (default: direct delivery to Internet)
relayhost = $mydomain         (deliver via local mailhub)
relayhost = [mail.$mydomain]  (deliver via local mailhub)
relayhost = [mail.isp.tld]    (deliver via provider mailhub)

The form enclosed with [ ] eliminates DNS MX lookups. Don't worry if you don't know what that means. Just be sure to specify the [ ] around the mailhub hostname that your ISP gave to you, otherwise mail may be mis-delivered.

Your default config should have something like the above. Choose a valid config based on your setup and comment out the others.

I would also suggest reviewing other sections regarding relays and network config to see if your setup is up to spec: Postfix Basic Configuration

share|improve this answer
This prompted me to talk to GoDaddy who hosts the VPS and they informed me all relays must go through their designated servers, so I can't use localhost. Thanks for the help!!! – ajon Jan 21 '14 at 21:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.