Strip incoming email subject?

Dear forum,

I’m polling emails from a mailinglist to discourse, the subjects of the mails generate new topics. That’s working fine.
Now I need to remove the first part of the subject, as it’s just the name of the mailinglist
“[mailinglist] actual topic” and I want to extract just the “actual topic”

I couldn’t find the right solution online so far - anyone?
Thank you,

1 Like

I thought that there was code in there to strip that, but it’s been 3 years since I’ve done an mbox migration.

The title gets set in ...mbox/importer.rb`:

    def map_first_post(row)
      mapped = map_post(row)
      mapped[:category] = category_id_from_imported_category_id(row['category'])
      mapped[:title] = row['subject'].strip[0...255]

You can change the title line to something like:

  mapped[:title] = row['subject'].strip[0...255].gsub(/^[.*?\]/,"")

I rarely get regex’s right the first time, so you’ll want to do a bit of testing (and maybe experiment at


Thanks for pointing this out.
I assume the RegEx would be like /^\[(.*?)\]\s/ to match everything at the beginning of the line like "[…] "

Could you please elaborate where to find the importer.rb on my installation (I used “under 30 min install” guide on a local Ubuntu VM

If you’re inside the container

cd /var/discourse
./launcher enter app

from wherever you’re running the script try:

find . -name importer.rb
1 Like

I changed the mbox/importer.rb using the corrected RegEx and also tried a static replace string without any change of the incoming topics.

I’ll try to clarify:

  • I subscribed the email-adress of the related dicourse category (custom address for incoming e-mails; √ category mirrors mailing-list) to the existing mailing-list
  • discourse receives this mails and creates according topics
  • as the mailing-list adds a prefix to the email-subject, I want to remove it before discourse creates the topic

My questions now

  1. is ./script/import_scripts/mbox/importer.rb the correct importer?
  2. is the .gsub("remove this prefix ","") syntaxt right?

I tried
mapped[:title] = row['subject'].strip[0...255].gsub(/^\[(.*?)\]\s/,"")

So far it didn’t change anything…
Thanks again.

The importer has nothing to do with this. You will need a plugin (shouldn’t be more than a couple lines of code) or a consumer for the topic_created webhook which could use the API to modify the topic title.


I totally misunderstood. I thought that you had mbox files that you were importing. Everything I said I wrong for your situation. Sorry.

1 Like