Postfix rejects an outgoing email with multiple destinations due to one invalid address

postfix

I'm sending my emails using Postfix, and I've got a considerable list of emails to send newsletters to. So I prefer to pack my SMTP requests into one by setting one of emails as To and a bunch more as Bcc (in PHP).

My problem is that if one of the emails is invalid, Postfix rejects the whole request prompting the following error:

5.1.1 : Recipient address rejected: User unknown in local recipient table

It would be great if I could configure Postfix in a way so it dumps the invalid one but sends the rest. I know I can validate the addresses myself before using them in an email but I prefer it if it is done by Postfix.

[UPDATE]

Here's the maillog's content:

Nov 18 16:20:28 mail postfix/smtpd[12922]: 14A5FB812E1: client=localhost[127.0.0.1]
Nov 18 16:20:28 mail postfix/smtpd[12922]: 14A5FB812E1: reject: RCPT from localhost[127.0.0.1]: 550 5.1.1 <mehran_hotmail.com>: Recipient address rejected: User unknown in local recipient table; from=<mehran@localhost> to=<mehran_hotmail.com> proto=ESMTP helo=<localhost>

This error is generated while I tried to send an email with mehran@hotmail.com as the main recipient and mehran_hotmail.com as the Bcc.

[UPDATE]

Here's the complete log associated with the request:

Nov 19 09:47:01 mail postfix/smtpd[20947]: connect from localhost[127.0.0.1]
Nov 19 09:47:01 mail postfix/smtpd[20947]: EC281B81BCE: client=localhost[127.0.0.1]
Nov 19 09:47:01 mail postfix/smtpd[20947]: warning: Illegal address syntax from localhost[127.0.0.1] in RCPT command: <mehran@?????.com>
Nov 19 09:47:01 mail postfix/smtpd[20947]: disconnect from localhost[127.0.0.1]

This is a new scenario (leading to the same problem) in which I used a correct email address (both To and Bcc hold correct email addresses but not the same address) but this time the email address is rejected because Postfix does not recognize it. I used non-ASCII characters as for the domain name (mehran@مهران.com). I don't care whether Postfix supports non-ASCII addresses or not, it's just that when it rejects one address, the whole request is rejected!! And no email is sent to mehran@hotmail.com (which is named in To)!

Now the returned error message to my code (PHP made MUA) is:

5.1.3 Bad recipient address syntax

As for my PHP code which interacts with SMTP server:

$mail = new \Zend_Mail('utf-8');
$mail->setMessageId($mail->createMessageId());

foreach ($array_to as $to) {
    $mail->addTo($to['email'], $to['name']);
}
foreach ($array_cc as $to) {
    $mail->addCc($to['email'], $to['name']);
}
foreach ($array_bcc as $to) {
    $mail->addBcc($to['email'], $to['name']);
}

$mail->setSubject($subject);
$mail->setBodyHtml($body);
$mail->setFrom($current_user_email, $current_user_name);
$mail->setDate(new \Zend_Date());

$smtp = createSmtpConnection();
$smtp->send($mail);

As you can see I used Zend Framework (v1) and my code is rather simple. The $array_* variables are simple arrays holding the email addresses. In my test scenario there are only two email addresses, one for To and one for Bcc.

Best Answer

TLDR: Your case is abnormal behavior mail client in proper SMTP transaction. Maybe something wrong in your PHP code. See this thread on postfix mailing list. A quote from Postfix author in that thread

You are mistaken. You have no evidence whatsoever that Postfix rejects the entire message.

It is known that SOME SMTP CLIENT programs will give up delivering a message when one recipient is not accepted, even when the other recipients are good.


How is SMTP rejection works

First, we will take the tour of SMTP transaction. Below is how SMTP works in low level. You can always try it via telnet/netcat.

Case 1 This is the transaction when there is a single recipient.

S: 220 smtp.example.net Simple Mail Transfer Service Ready
C: HELO client.example.com
S: 250 Hello client.example.com
C: MAIL FROM:<mail@example.com>
S: 250 OK
C: RCPT TO:<john@example.net>
S: 250 OK
C: DATA
S: 354 Send message content; end with <CRLF>.<CRLF>
C: The message data (body text, subject, e-mail header, attachments etc) is sent
C: .
S: 250 2.0.0 Ok: queued as D7D3E84403
C: QUIT
S: 221 Bye

So, SMTP is chatty protocol, every time the client issuing command (HELO/MAIL/RCPT/DATA/QUIT), the server should answer it before the transaction continue. In this case all the answer has code 250, or in human languange I accepted it.

Case 2 SMTP transaction for multiple recipients

S: 220 smtp.example.net Simple Mail Transfer Service Ready
C: HELO client.example.com
S: 250 Hello client.example.com
C: MAIL FROM:<mail@example.com>
S: 250 OK
C: RCPT TO:<john@example.net>
S: 250 OK
C: RCPT TO:<doe@example.net>
S: 250 OK
C: RCPT TO:<alice@example.net>
S: 250 OK
C: DATA
S: 354 Send message content; end with <CRLF>.<CRLF>
C: The message data (body text, subject, e-mail header, attachments etc) is sent
C: .
S: 250 2.0.0 Ok: queued as D7D3E84403
C: QUIT
S: 221 Bye

In this example there are three recipient. We are using multiple RCPT command in single transaction. RCPT command is special command. This command can be repeated multiple times for a given e-mail message in order to deliver a single e-mail message to multiple recipients.

Case 3 If some recipients rejected (but not all), the transaction was continued. Here the sample transaction.

S: 220 smtp.example.net Simple Mail Transfer Service Ready
C: HELO client.example.com
S: 250 Hello client.example.com
C: MAIL FROM:<mail@example.com>
S: 250 OK
C: RCPT TO:<john@example.net>
S: 250 OK
C: RCPT TO:<invaliduser@example.net>
S: 550 5.1.1 <invaliduser@example.net>: Recipient address rejected: User unknown in local recipient table
C: RCPT TO:<alice@example.net>
S: 250 OK
C: DATA
S: 354 Send message content; end with <CRLF>.<CRLF>
C: The message data (body text, subject, e-mail header, attachments etc) is sent
C: .
S: 250 2.0.0 Ok: queued as D7D3E84403
C: QUIT
S: 221 Bye

Why the server still accept the email? Because there are two valid recipients beside one invalid one.

Disclaimer: Above resources taken from here.


(unsuccessful) Attempt to Reproduce the Problem

OK, I have tried to reproduce your problem in my box. This is my PHP code with library PHPMailer. (I don't familiar with zend Framework)

<?php
require '../PHPMailerAutoload.php';

$internaldomain = 'in.example.com';
$externaldomain = 'ex.example.com';

$mail = new PHPMailer;

$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "smtp6.example.com";
$mail->Port = 25;
$mail->SMTPAuth = false;

$mail->setFrom('from@' . $internaldomain, 'First Last');
$mail->addAddress('valid@' . $externaldomain, 'valid 1');
$mail->AddBCC('bounce@' . $internaldomain, 'valid 3');
$mail->AddBCC('invaliduser@' . $internaldomain, 'invalid user');
$mail->AddBCC('root@' . $internaldomain, 'valid 4');

$mail->Subject = 'PHPMailer SMTP test';
$mail->IsHTML(false);
$mail->Body    = "This is test";

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

Because SMTPDebug has enabled, the output contains full SMTP transaction. This output similar with above example.

SERVER -> CLIENT: 220 smtp6.example.net ESMTP at your service
CLIENT -> SERVER: EHLO web.example.net
SERVER -> CLIENT: 250-smtp6.example.net
                  250-PIPELINING
                  250-SIZE 10240000
                  250-VRFY
                  250-ETRN
                  250-ENHANCEDSTATUSCODES
                  250-8BITMIME
                  250 DSN
CLIENT -> SERVER: MAIL FROM:<from@example.net>
SERVER -> CLIENT: 250 2.1.0 Ok
CLIENT -> SERVER: RCPT TO:<valid@example.org>
SERVER -> CLIENT: 250 2.1.5 Ok
CLIENT -> SERVER: RCPT TO:<bounce@example.net>
SERVER -> CLIENT: 250 2.1.5 Ok
CLIENT -> SERVER: RCPT TO:<invaliduser@example.net>
SERVER -> CLIENT: 550 5.1.1 <invaliduser@example.net>: Recipient address rejected: User unknown in local recipient table
SMTP ERROR: RCPT TO command failed: 550 5.1.1 <invaliduser@example.net>: Recipient address rejected: User unknown in local recipient table
CLIENT -> SERVER: RCPT TO:<root@example.net>
SERVER -> CLIENT: 250 2.1.5 Ok
CLIENT -> SERVER: DATA
SERVER -> CLIENT: 354 End data with <CR><LF>.<CR><LF>
CLIENT -> SERVER: Date: Wed, 19 Nov 2014 09:28:16 +0700
CLIENT -> SERVER: To: valid 1 <valid@example.org>
CLIENT -> SERVER: From: First Last <from@example.net>
CLIENT -> SERVER: Subject: PHPMailer SMTP test
CLIENT -> SERVER: Message-ID: <b67f817cacdff282e92dc17f9c4f406b@web.example.net>
CLIENT -> SERVER: X-Priority: 3
CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.9 (https://github.com/PHPMailer/PHPMailer/)
CLIENT -> SERVER: MIME-Version: 1.0
CLIENT -> SERVER: Content-Type: text/plain; charset=iso-8859-1
CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
CLIENT -> SERVER:
CLIENT -> SERVER: This is test
CLIENT -> SERVER:
CLIENT -> SERVER: .
SERVER -> CLIENT: 250 2.0.0 Ok: queued as D7D3E84403
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 Bye

and the maillog entry

Nov 19 09:28:16 cache postfix/smtpd[14865]: D7D3E84403: client=unknown[192.168.192.100]
Nov 19 09:28:16 cache postfix/smtpd[14865]: D7D3E84403: reject: RCPT from unknown[192.168.192.100]: 550 5.1.1 <invaliduser@example.net>: Recipient address rejected: User unknown in local recipient table; from=<from@example.net> to=<invaliduser@example.net> proto=ESMTP helo=<web.example.net>
Nov 19 09:28:16 cache postfix/cleanup[14867]: D7D3E84403: message-id=<b67f817cacdff282e92dc17f9c4f406b@web.example.net>
Nov 19 09:28:17 cache postfix/qmgr[1200]: D7D3E84403: from=<from@example.net>, size=617, nrcpt=3 (queue active)
Nov 19 09:28:17 cache postfix/local[14870]: D7D3E84403: to=<root@example.net>, relay=local, delay=0.21, delays=0.21/0/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
Nov 19 09:28:17 cache postfix/smtp[14869]: D7D3E84403: to=<valid@example.org>, relay=example.org[192.168.3.3]:25, delay=0.22, delays=0.21/0/0/0.01, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 3jj7Hj0pM3z5Twh)
Nov 19 09:28:17 cache postfix/local[14868]: D7D3E84403: to=<zbounce@example.net>, relay=local, delay=0.23, delays=0.21/0/0/0.02, dsn=2.0.0, status=sent (forwarded as 165E084404)
Nov 19 09:28:17 cache postfix/qmgr[1200]: D7D3E84403: removed

Well looks like in my box, postfix and PHPMailer behave in normal circumstances. You can use this to compare with maillog in your box :)