axllent / mailpit

An email and SMTP testing tool with API for developers
https://mailpit.axllent.org
MIT License
5.57k stars 138 forks source link

SMTP relay error when one of multiple recipients doesn't exist #132

Closed luomoxu closed 1 year ago

luomoxu commented 1 year ago

when send mail to multiple recipients,one of them dosn't exist,the reply will faild

[smtp] error sending message: error response to RCPT command for abc@example.com: 554 RCPT (abc@example.com) dosn't exist
axllent commented 1 year ago

Maybe I do not understand the problem or question here, or what you expect to happen when an email address is invalid.

When Mailpit "releases" an email, it takes the original email, passes it to the relay SMTP server, and then tells the SMTP server who to send it to (an array of email addresses). Normally the relay server will accept the message, and then bounce and errors "back" to the sender, however I suspect Alibaba (based on your previous reports) immediately rejects the email to the client (Mailpit), and doesn't bounce and emails like other mail servers.

[smtp] error sending message: error response to RCPT command for abc@example.com: 554 RCPT (abc@example.com) dosn't exist

This error is being returned by your relay SMTP server.

Can you please tell me what you expected to happen?

luomoxu commented 1 year ago

when I use postfix,the mail will send sucess,the not exist email will be ignore I don't know It a smtp parameter or postfix auto remove the not exist email ?

axllent commented 1 year ago

Yes, that is because postfix handles the email different to Alibaba - Mailpit is not postfix. I think what you need to understand here is the difference between the two. Mailpit acts like an email client when sending email. Postfix and Alibaba are email servers, however they handle errors obviously very differently. 1) Postfix first accepts the emails, then bounces errors 2) Alibaba rejects errors immediately

So when you say postfix -> Alibaba works, what is actually happening is postfix accepts your message, and then bounces the error to the sender. If the sender cannot receive a message, then it is auto-deleted internally. If you check the postfix mail log you will see it there.

Again, I cannot change how Alibaba handles emails. If this is an issue for you then you will need to use another SMTP server unfortunately.

luomoxu commented 1 year ago

I check the code,when one of recipients rcpt get error,the smtprelay will stop ,can ignore the error ,continue to execute https://github.com/axllent/mailpit/blob/70b604e028a3d09d1ad89850047d9160e751c1fc/server/smtpd/smtp.go#L89-L93

axllent commented 1 year ago

In your case yes, changing this would possibly allow the message to be sent (assuming Alibaba allows the rest to go through). The issue I have is this: how would the web UI user know there had been an issue if I don't return an error? Ignoring rejrcted recipients would just mean the message just never arrives. Let me think about it a bit more before making a decision as this change affects everyone, not just you.

Did you check the postfix logs to see what it said about that rejected message? If so, could you please show me that log message?

luomoxu commented 1 year ago

postfix log

Jun 25 22:00:43 example postfix/smtpd[434]: B1F834E003A: client=172-30-7-200.service.svc.cluster.local[172.30.7.200]
Jun 25 22:00:43 example postfix/cleanup[438]: B1F834E003A: message-id=<Z3RC1y29p9qInqMTv2Pfg.1687730443714@postal-5796678c46-6chxt>
Jun 25 22:00:43 example postfix/qmgr[102]: B1F834E003A: from=<noreply@example.com>, size=232167, nrcpt=4 (queue active)
Jun 25 22:00:43 example postfix/smtpd[434]: disconnect from 172-30-7-200.service.svc.cluster.local[172.30.7.200] ehlo=1 mail=1 rcpt=4 data=1 quit=1 commands=8
Jun 25 22:00:44 example postfix/smtp[439]: B1F834E003A: to=<user1@example.com>, relay=smtp.qiye.aliyun.com[59.82.44.168]:465, delay=0.81, delays=0.04/0.01/0.64/0.12, dsn=5.0.0, status=bounced (host smtp.qiye.aliyun.com[59.82.44.168] said: 554 RCPT (user1@example.com) dosn't exist (in reply to RCPT TO command))
Jun 25 22:00:44 example postfix/smtp[439]: B1F834E003A: to=<user2@example.com>, relay=smtp.qiye.aliyun.com[59.82.44.168]:465, delay=0.82, delays=0.04/0.01/0.64/0.13, dsn=5.0.0, status=bounced (host smtp.qiye.aliyun.com[59.82.44.168] said: 554 RCPT (user2@example.com) dosn't exist (in reply to RCPT TO command))
Jun 25 22:00:44 example postfix/smtp[439]: B1F834E003A: replace: header From: noreply@example.com: From: noreply@example.com
Jun 25 22:00:45 example postfix/smtp[439]: B1F834E003A: to=<user3@example.com>, relay=smtp.qiye.aliyun.com[59.82.44.168]:465, delay=2, delays=0.04/0.01/0.64/1.3, dsn=2.0.0, status=sent (250 Data Ok: queued as freedom)
Jun 25 22:00:45 example postfix/smtp[439]: B1F834E003A: to=<user4@example.com>, relay=smtp.qiye.aliyun.com[59.82.44.168]:465, delay=2, delays=0.04/0.01/0.64/1.3, dsn=2.0.0, status=sent (250 Data Ok: queued as freedom)
Jun 25 22:00:45 example postfix/cleanup[438]: B2BF94E0041: message-id=<20230625220045.B2BF94E0041@example.com>
Jun 25 22:00:45 example postfix/bounce[440]: B1F834E003A: sender non-delivery notification: B2BF94E0041
Jun 25 22:00:45 example postfix/qmgr[102]: B2BF94E0041: from=<>, size=3090, nrcpt=1 (queue active)
Jun 25 22:00:45 example postfix/qmgr[102]: B1F834E003A: removed
axllent commented 1 year ago

Thanks, this confirms my theory: The emails actually bounce (status=bounced), except postfix actually deletes them because they cant send to @example.com. So in this case you would never know unless you looked in your postfix logs.

I will make the change that will print out the error in the Mailpit log if the RCPT command fails, but it will still try and send anyway. If anything else fails in the SMTP transaction then that will return an error. Please note that there is no guarantee this will fix your issue, as from what I have seen Alibaba's SMTP server likes to close the connection when it doesn't like what it gets. I'll update you later once I have made the change.

axllent commented 1 year ago

@luomoxu I have just added this in the latest release (v1.7.0). Please can you test and let me know if it solves your issue? Thank you.

luomoxu commented 1 year ago

@luomoxu I have just added this in the latest release (v1.7.0). Please can you test and let me know if it solves your issue? Thank you.

work fine for me

axllent commented 1 year ago

Great, thanks.