Ticket #15448: 15448-multi.diff

File 15448-multi.diff, 4.3 KB (added by rmccue, 2 years ago)

Add support for multiple attachments of the same type

Line 
1Index: pluggable.php
2===================================================================
3--- pluggable.php       (revision 16879)
4+++ pluggable.php       (working copy)
5@@ -247,6 +247,11 @@
6  * However, you can set the content type of the email by using the
7  * 'wp_mail_content_type' filter.
8  *
9+ * If $message is an array, the key of each is used to add as an attachment
10+ * with the value used as the body. The 'text/plain' element is used as the
11+ * text version of the body, with the 'text/html' element used as the HTML
12+ * version of the body. All other types are added as attachments.
13+ *
14  * The default charset is based on the charset used on the blog. The charset can
15  * be set using the 'wp_mail_charset' filter.
16  *
17@@ -263,7 +268,7 @@
18  *
19  * @param string|array $to Array or comma-separated list of email addresses to send message.
20  * @param string $subject Email subject
21- * @param string $message Message contents
22+ * @param string|array $message Message contents
23  * @param string|array $headers Optional. Additional headers.
24  * @param string|array $attachments Optional. Files to attach.
25  * @return bool Whether the email contents were sent successfully.
26@@ -332,6 +337,10 @@
27                                                }
28                                                break;
29                                        case 'content-type':
30+                                               if ( is_array($message) ) {
31+                                                       // Multipart email, ignore the content-type header
32+                                                       break;
33+                                               }
34                                                if ( strpos( $content, ';' ) !== false ) {
35                                                        list( $type, $charset ) = explode( ';', $content );
36                                                        $content_type = trim( $type );
37@@ -403,10 +412,53 @@
38                $phpmailer->AddAddress( trim( $recipient ) );
39        }
40 
41+       // If we don't have a charset from the input headers
42+       if ( !isset( $charset ) )
43+               $charset = get_bloginfo( 'charset' );
44+
45+       // Set the content-type and charset
46+       $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
47+
48        // Set mail's subject and body
49        $phpmailer->Subject = $subject;
50-       $phpmailer->Body    = $message;
51 
52+       if ( is_string($message) ) {
53+               $phpmailer->Body    = $message;
54+
55+               // Set Content-Type and charset
56+               // If we don't have a content-type from the input headers
57+               if ( !isset( $content_type ) )
58+                       $content_type = 'text/plain';
59+
60+               $content_type = apply_filters( 'wp_mail_content_type', $content_type );
61+
62+               $phpmailer->ContentType = $content_type;
63+
64+               // Set whether it's plaintext, depending on $content_type
65+               if ( 'text/html' == $content_type )
66+                       $phpmailer->IsHTML( true );
67+
68+               // For backwards compatibility, new multipart emails should use
69+               // the array style $message. This never really worked well anyway
70+               if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
71+                       $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
72+       }
73+       elseif ( is_array($message) ) {
74+               foreach ($message as $type => $bodies) {
75+                       foreach ((array) $bodies as $body) {
76+                               if ($type === 'text/html') {
77+                                       $phpmailer->Body = $body;
78+                               }
79+                               elseif ($type === 'text/plain') {
80+                                       $phpmailer->AltBody = $body;
81+                               }
82+                               else {
83+                                       $phpmailer->AddAttachment($body, '', 'base64', $type);
84+                               }
85+                       }
86+               }
87+       }
88+
89        // Add any CC and BCC recipients
90        if ( !empty( $cc ) ) {
91                foreach ( (array) $cc as $recipient ) {
92@@ -423,34 +475,11 @@
93        // Set to use PHP's mail()
94        $phpmailer->IsMail();
95 
96-       // Set Content-Type and charset
97-       // If we don't have a content-type from the input headers
98-       if ( !isset( $content_type ) )
99-               $content_type = 'text/plain';
100-
101-       $content_type = apply_filters( 'wp_mail_content_type', $content_type );
102-
103-       $phpmailer->ContentType = $content_type;
104-
105-       // Set whether it's plaintext, depending on $content_type
106-       if ( 'text/html' == $content_type )
107-               $phpmailer->IsHTML( true );
108-
109-       // If we don't have a charset from the input headers
110-       if ( !isset( $charset ) )
111-               $charset = get_bloginfo( 'charset' );
112-
113-       // Set the content-type and charset
114-       $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
115-
116        // Set custom headers
117        if ( !empty( $headers ) ) {
118                foreach( (array) $headers as $name => $content ) {
119                        $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
120                }
121-
122-               if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
123-                       $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
124        }
125 
126        if ( !empty( $attachments ) ) {