Answers.Body: <p>I would use the alterMailParams hook and replace the to, cc, bcc in $params</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-09T06:02:40.453Z
Answers.Text: Thank you for this hint - I did not see this hook. It seems to do exactly what I want... I'll test it!
Answers.UserDisplayName: Tobias Krause
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-11T11:20:03.930Z
Answers.Text: Thank you again... with this hook it works!!!
Answers.UserDisplayName: Tobias Krause
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-06T15:59:19.460Z
Answers.LastActivityDate: 2022-05-06T15:59:19.460Z
Answers.UserDisplayName: Pradeep Nayak
Answers.Body: <p>You probably want to connect your dev environment to something like mailtrap. That way you'll be able to see the emails in an inbox. The alternative is to set all emails to write to the database instead, theres a setting to do that.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-09T06:05:25.233Z
Answers.Text: I'd like to avoid something like this as my solution would assure that the e-mails would not be sent no matter if a developer has locally installed a tool like mailtrap. Writing the e-mails to the database is no alternative as it is not possible to test the e-mails in real e-mail clients.
Answers.UserDisplayName: Tobias Krause
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-12T08:39:33.810Z
Answers.Text: Thanks for raising this Tobias - I am looking at exactly this issue.  We cannot have a 'dev' first approach (e.g. I use MailHog on my local Docker dev environment) beacuse we need ordinary users to UAT and see the emails in the various clients they use themselves.  I'll check out that hook.
Answers.UserDisplayName: ChumKui
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-06T11:22:24.687Z
Answers.LastActivityDate: 2022-05-06T11:22:24.687Z
Answers.UserDisplayName: Parvez Saleh
Body: <p>I want to create a module which alters the recipients of all outgoing emails so that in dev environment email are only sent to developers or testers and not to real users. So the recipients should be altered before sending the email in defined circumstances.</p>
  <p>I tried <code>hook_civicrm_alterMailingRecipients()</code> but this hooks gets called when I create a mailing and add recipients to the corresponding field. This is not what I need - I don't want to alter the recipients on creation of a mass/bulk mailing.</p>
  <p>Is there any way to alter recipients for all outgoing email (indiviual emails, mailings, system mails - if there are any) in any way? In Drupal we have <code>hook_mail_alter()</code> which allows to alter just everything in an email and which I used for this functionality already.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-05-07T04:01:35.393Z
Comments.Text: any reason setting it to 'go to db' (as Parvez mentions) and then using the 'mail log viewer' wouldn't give you what you need? https://civicrm.org/extensions/civicrm-log-viewer
Comments.UserDisplayName: petednz - fuzion
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-05-09T06:01:04.890Z
Comments.Text: You cannot test your emails in e-mail clients...
Comments.UserDisplayName: Tobias Krause
Tags: <civimail><email><mailing>
Title: How to redirect all outgoing emails / change recipients before email sending
ThreadText: # How to redirect all outgoing emails / change recipients before email sending
  
  (Asked by: username_0 on 2022-05-06)
  
  I want to create a module which alters the recipients of all outgoing emails so that in dev environment email are only sent to developers or testers and not to real users. So the recipients should be altered before sending the email in defined circumstances.
  
  I tried `hook_civicrm_alterMailingRecipients()` but this hooks gets called when I create a mailing and add recipients to the corresponding field. This is not what I need - I don't want to alter the recipients on creation of a mass/bulk mailing.
  
  Is there any way to alter recipients for all outgoing email (indiviual emails, mailings, system mails - if there are any) in any way? In Drupal we have `hook_mail_alter()` which allows to alter just everything in an email and which I used for this functionality already.
  
  **Tags:** civimail, email, mailing
  
  > **Comments:**
  > **username_1**: any reason setting it to 'go to db' (as Parvez mentions) and then using the 'mail log viewer' wouldn't give you what you need? https://civicrm.org/extensions/civicrm-log-viewer  
  > **username_0**: You cannot test your emails in e-mail clients...  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2022-05-06. Score: 2 | Accepted answer
  
  I would use the alterMailParams hook and replace the to, cc, bcc in $params
  
  > **Comments:**
  > **username_0**: Thank you for this hint - I did not see this hook. It seems to do exactly what I want... I'll test it!  
  > **username_0**: Thank you again... with this hook it works!!!  
  
  ---
  
  ### Answer by username_3 on 2022-05-06. Score: 0
  
  You probably want to connect your dev environment to something like mailtrap. That way you'll be able to see the emails in an inbox. The alternative is to set all emails to write to the database instead, theres a setting to do that.
  
  > **Comments:**
  > **username_0**: I'd like to avoid something like this as my solution would assure that the e-mails would not be sent no matter if a developer has locally installed a tool like mailtrap. Writing the e-mails to the database is no alternative as it is not possible to test the e-mails in real e-mail clients.  
  > **username_4**: Thanks for raising this Tobias - I am looking at exactly this issue.  We cannot have a 'dev' first approach (e.g. I use MailHog on my local Docker dev environment) beacuse we need ordinary users to UAT and see the emails in the various clients they use themselves.  I'll check out that hook.  
  
  ---
  
  