foxcpp / maddy

✉️ Composable all-in-one mail server.
https://maddy.email
GNU General Public License v3.0
4.95k stars 239 forks source link

Feature request - replace_rcpt sql_query {} should handle multiple results #638

Closed pkejval closed 10 months ago

pkejval commented 10 months ago

Use case

What problem you are trying to solve?

_replacercpt file /etc/maddy/aliases file can have multiple aliases for one address like it's said in docs:

# Comma-separated aliases in multiple lines
cat3: dog , mouse
cat3@example.org: cat@example.com , cat@example.net

I'd like to have table for this mapping in database and query for them like this:

replace_rcpt sql_query {
  import pg_con
  lookup "SELECT STRING_AGG(address, ', ') AS addresses FROM aliases WHERE alias = $1"
}

Actually maddy returns this message when multiple addreses are set for alias: smtp: MAIL FROM error (deferred) {"msg_id":"","rcpt":"alias@domain.tld","reason":"Unable to normalize the sender address","smtp_code":553,"smtp_enchcode":"5.1.7","smtp_msg":"Unable to normalize the sender address"}

Your idea for a solution

Parse SQL column like you're parsing text file. Allow multiple comma-separated values.

foxcpp commented 10 months ago

Try

replace_rcpt sql_query {
  import pg_con
  lookup "SELECT address AS addresses FROM aliases WHERE alias = $1"
}

It is not necessary to use STRING_AGG, sql_query already handles multiple rows in query result as multiple values.

pkejval commented 10 months ago

Yes, its working, thank you! Reason for opening this request was this sentence in documentation:

https://maddy.email/reference/table/sql_query/

lookup query
Required.

SQL query to use to obtain the lookup result.

It will get one named argument containing the lookup key. Use :key placeholder to access it in SQL. 
The result row set should contain one row, one column with the string that will be used as a lookup result. 
If there are more rows, they will be ignored. If there are more columns, lookup will fail.
If there are no rows, lookup returns "no results". If there are any error - lookup will fail.

hence I must admit I didn't try it without STRING_AGG() 😵‍💫

Sorry for bothering you with nonexistent issue!

foxcpp commented 10 months ago

Woops, that's outdated and should be fixed.