---
title: "Mail sent via 127.0.0.1 rejects with "Relaying Denied"
date: "2016-10-24"
category: "email"
slug: "mail-sent-via-127-0-0-1-rejects-relaying-denied"
---

## Overview

Email sent over TCP via 127.0.0.1 or the server's IP address is rejected with a "521: Relaying Denied" error.

## Cause

All email sent over TCP must be authenticated using SASL-compatible credentials ([[accessing-e-mail]]) -- the same login/password used to access email on the server.

## Why Authentication Is Required

SASL authentication enables abuse tracking and prevents unauthenticated users from relaying mail that the SMTP service cannot trace. Mail sent through sendmail (e.g., via the PHP [mail()](http://php.net/mail) function) does not require this because the UID and originating script are logged automatically.

## Authentication Examples

### PHPMailer

$mail \= new PHPMailer;

$mail\->isSMTP();                                      // Set mailer to use SMTP
$mail\->Host \= '127.0.0.1';                            // Specify main and backup SMTP servers
$mail\->SMTPAuth \= true;                               // Enable SMTP authentication
$mail\->Username \= 'user@example.com';                 // SMTP username
$mail\->Password \= 'secret';                           // SMTP password
$mail\->SMTPSecure \= 'tls';                            // Enable TLS encryption, \`ssl\` also accepted
$mail\->Port \= 587;                                    // TCP port to connect to

### WordPress

Use [WP SMTP Mail](https://wordpress.org/plugins/wp-mail-smtp/) or continue using WordPress' built-in mailer (which uses sendmail and does not require SMTP authentication).

### Ruby on Rails

Via config/environments/$RAILS\_ENV.rb:

`config.action_mailer.delivery_method =` `:smtp`

`config.action_mailer.smtp_settings = {`

`address:` `'127.0.0.1'``,`

`port:` `587``,`

`domain:` `'example.com'``,`

`user_name:` `'user@example.com'``,`

`password:` `'secret'``,`

`authentication:` `'plain'``,`

`enable_starttls_auto:` `true``}`

## See Also

- [[accessing-e-mail]]
