@@ -42,6 +42,20 @@ $email_from = getenv_required('VORTEX_NOTIFY_EMAIL_FROM', 'DRUPAL_SITE_EMAIL');
4242// Example: "[email protected] |Jane Doe, [email protected] |John Doe". 4343$ email_recipients = getenv_required ('VORTEX_NOTIFY_EMAIL_RECIPIENTS ' );
4444
45+ // Email notification CC recipients.
46+ //
47+ // Multiple names can be specified as a comma-separated list of email addresses
48+ // with optional names in the format "email|name".
49+ // Example: "[email protected] |Jane Doe, [email protected] |John Doe". 50+ $ email_cc = getenv_default ('VORTEX_NOTIFY_EMAIL_CC ' , '' );
51+
52+ // Email notification BCC recipients.
53+ //
54+ // Multiple names can be specified as a comma-separated list of email addresses
55+ // with optional names in the format "email|name".
56+ // Example: "[email protected] |Jane Doe, [email protected] |John Doe". 57+ $ email_bcc = getenv_default ('VORTEX_NOTIFY_EMAIL_BCC ' , '' );
58+
4559// Email notification subject template.
4660//
4761// Available tokens:
@@ -62,7 +76,37 @@ $email_subject = getenv_default('VORTEX_NOTIFY_EMAIL_SUBJECT', '%project% deploy
6276// - %login_url% - Login URL.
6377$ email_message = getenv_default ('VORTEX_NOTIFY_EMAIL_MESSAGE ' , "## This is an automated message ## \nSite %project% %label% has been deployed at %timestamp% and is available at %environment_url%. \nLogin at: %login_url% " );
6478
65- // ------------------------------------------------------------------------------
79+ // -----------------------------------------------------------------------------
80+
81+ /**
82+ * Parse email recipients from a comma-separated string.
83+ *
84+ * @param string $recipients
85+ * Comma-separated list of recipients. Format: "email1, email2|Name, email3".
86+ *
87+ * @return array<string>
88+ * Array of formatted email addresses.
89+ */
90+ function parse_email_recipients (string $ recipients ): array {
91+ if (empty ($ recipients )) {
92+ return [];
93+ }
94+
95+ $ parsed = [];
96+ $ recipient_list = array_map (trim (...), explode (', ' , $ recipients ));
97+
98+ foreach ($ recipient_list as $ recipient ) {
99+ $ parts = array_map (trim (...), explode ('| ' , $ recipient , 2 ));
100+ $ email = $ parts [0 ];
101+ $ name = $ parts [1 ] ?? '' ;
102+
103+ $ parsed [] = empty ($ name ) ? $ email : sprintf ('"%s" <%s> ' , $ name , $ email );
104+ }
105+
106+ return $ parsed ;
107+ }
108+
109+ // -----------------------------------------------------------------------------
66110
67111info ('Started email notification. ' );
68112
@@ -95,28 +139,42 @@ note('Environment URL: ' . $notify_env_url);
95139note ('Login URL : ' . $ notify_login_url );
96140note ('From : ' . $ email_from );
97141note ('Recipients : ' . $ email_recipients );
142+ if (!empty ($ email_cc )) {
143+ note ('CC : ' . $ email_cc );
144+ }
145+ if (!empty ($ email_bcc )) {
146+ note ('BCC : ' . $ email_bcc );
147+ }
98148note ('Subject : ' . $ email_subject );
99149note ('Content : ' . $ email_message );
100150
101151task ('Sending email notification ' );
102152
103- $ email_recipients = array_map (trim (...), explode (', ' , $ email_recipients ));
153+ // Parse recipients.
154+ $ to_list = parse_email_recipients ($ email_recipients );
155+ $ cc_list = parse_email_recipients ($ email_cc );
156+ $ bcc_list = parse_email_recipients ($ email_bcc );
104157
105158$ sent = [];
106- foreach ($ email_recipients as $ email_recipient ) {
107- $ parts = array_map (trim (...), explode ('| ' , $ email_recipient , 2 ));
108- $ email = $ parts [0 ];
109- $ name = $ parts [1 ] ?? '' ;
110-
111- $ to = empty ($ name ) ? $ email : sprintf ('"%s" <%s> ' , $ name , $ email );
112-
159+ foreach ($ to_list as $ to ) {
113160 $ headers = [
114161 'From: ' . $ email_from ,
115162 'Content-Type: text/plain; charset=UTF-8 ' ,
116163 ];
117164
165+ // Add CC header if there are CC recipients.
166+ if (!empty ($ cc_list )) {
167+ $ headers [] = 'Cc: ' . implode (', ' , $ cc_list );
168+ }
169+
170+ // Add BCC header if there are BCC recipients.
171+ if (!empty ($ bcc_list )) {
172+ $ headers [] = 'Bcc: ' . implode (', ' , $ bcc_list );
173+ }
174+
118175 if (mail ($ to , $ email_subject , $ email_message , $ headers )) {
119- $ sent [] = $ email ;
176+ // Extract just the email address from formatted string for tracking.
177+ $ sent [] = preg_match ('/<(.+?)>/ ' , $ to , $ matches ) ? $ matches [1 ] : $ to ;
120178 }
121179}
122180
0 commit comments