I want to know if there is a way to replace a character by another in a variable. For example replacing every dots with underscores in a string variable.
-
1What language are you using? That would help. – clabe45 Aug 22 '17 at 17:48
-
2Sieve is the name of the langage – dblouis Aug 22 '17 at 19:04
-
1If you are looking for a way to store e-mails in folders per recipient address or something like that, perhaps you don't actually want a generic character replace function but some way to create mailboxes with dots in their names. In the case of dovecot, there seem to be a solution: [\[Dovecot\] . (dot) in maildir folder names](https://www.dovecot.org/list/dovecot/2013-May/090577.html "[Dovecot] . (dot) in maildir folder names") – Stéphane Gourichon Sep 24 '18 at 18:15
-
@StéphaneGourichon Your assumption is correct for me at least. I tried using Listescape without success though :-/ – Tigerware Jun 06 '21 at 17:19
2 Answers
I haven't tried it, but based on the Variables specification, the way I'd try to approach this would be to try to match on the text before and after a dot, and then make new variables based on the matches. Something like:
set "value" "abc.def";
if string :matches "${value}" "*.*" {
set "newvalue" "${1}_${2}
}
This will, of course, only match on a single period because Sieve doesn't include any looping structures. While there's a regex match option, I'm not aware of any regex replacement Sieve extensions.
Another approach to complex mail filtering you can do with Dovecot (if you do need loops and have full access to the mail server) is their Dovecot-specific extensions like vnd.dovecot.pipe
which allows the mail administrator to define full programs (written in whatever language one wishes) to process mail on its way through.

- 1
- 1
Following @BluE's comment, if your use case is to store e-mails in folders per recipient address or something like that, perhaps you don't actually want a generic character replace function but some way to create mailboxes with dots in their names. In the case of dovecot, there seem to be a solution: [Dovecot] . (dot) in maildir folder names
https://wiki2.dovecot.org/Plugins/Listescape
Ensure one of the files in /etc/dovecot/conf.d
contains this line:
mail_plugins = listescape
Then you can filter mailing lists into separate boxes based on their IDs.
This Sieve script snippet picks the ID from the x-list-id
header:
if exists "x-list-id" {
if header :regex "x-list-id" "<([\.@a-z_0-9-]+)" {
set :lower "listname" "${1}";
fileinto :create "mailing_list\\${listname}";
} else {
keep;
}
stop;
}

- 6,493
- 4
- 37
- 48