168 | | |
169 | | $mail = compact('to', 'subject', 'message', 'headers'); |
170 | | $mail = apply_filters('wp_mail', $mail); |
171 | | extract($mail); |
172 | | |
173 | | if ( $headers == '' ) { |
174 | | $headers = "MIME-Version: 1.0\n" . |
175 | | "From: " . apply_filters('wp_mail_from', "wordpress@" . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']))) . "\n" . |
176 | | "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n"; |
| 169 | |
| 170 | // Compact the input, apply the filters, and extract them back out |
| 171 | extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers' ) ) ); |
| 172 | |
| 173 | // Default headers |
| 174 | if ( empty( $headers ) ) { |
| 175 | $headers = array( |
| 176 | 'MIME-Version' => '1.0' |
| 177 | ); |
| 178 | } elseif ( !is_array( $headers ) ) { |
| 179 | // Explode the headers out, so this function can take both |
| 180 | // string headers and an array of headers. |
| 181 | $tempheaders = (array) explode( "\n", $headers ); |
| 182 | $headers = array(); |
| 183 | |
| 184 | // If it's actually got contents |
| 185 | if ( !empty( $tempheaders ) ) { |
| 186 | // Iterate through the raw headers |
| 187 | foreach ( $tempheaders as $header ) { |
| 188 | // Explode them out |
| 189 | list( $name, $content ) = explode( ':', trim( $header ), 2 ); |
| 190 | |
| 191 | // Cleanup crew |
| 192 | $name = trim( $name ); |
| 193 | $content = trim( $content ); |
| 194 | |
| 195 | // Mainly for legacy -- process a From: header if it's there |
| 196 | if ( $name == 'From' ) { |
| 197 | if ( strpos( '<', $content ) !== false ) { |
| 198 | // So... making my life hard again? |
| 199 | $from_name = substr( $content, 0, strpos( '<', $content ) - 1 ); |
| 200 | $from_name = str_replace( '"', '', $from_name ); |
| 201 | $from_name = trim( $from_name ); |
| 202 | |
| 203 | $from_email = substr( $content, strpos( '<', $content ) + 1 ); |
| 204 | $from_email = str_replace( '>', '', $from_email ); |
| 205 | $from_email = trim( $from_email ); |
| 206 | } else { |
| 207 | $from_name = trim( $content ); |
| 208 | } |
| 209 | } elseif ( $name == 'Content-Type' ) { |
| 210 | if ( strpos( ';', $content ) !== false ) { |
| 211 | list( $type, $charset ) = explode( ';', $content ); |
| 212 | $content_type = trim( $content_type ); |
| 213 | $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) ); |
| 214 | } else { |
| 215 | $content_type = trim( $content ); |
| 216 | } |
| 217 | } else { |
| 218 | // Add it to our grand headers array |
| 219 | $headers[trim( $name )] = trim( $content ); |
| 220 | } |
| 221 | } |
| 222 | } |
183 | | $phpmailer->ClearAllRecipients(); |
184 | | $phpmailer->ClearCustomHeaders(); |
185 | | |
186 | | $phpmailer->FromName = "WordPress"; |
187 | | $phpmailer->AddAddress("$to", ""); |
| 233 | |
| 234 | // From email and name |
| 235 | // If we don't have a name from the input headers |
| 236 | if ( !isset( $from_name ) ) { |
| 237 | $from_name = 'WordPress'; |
| 238 | } |
| 239 | |
| 240 | // If we don't have an email from the input headers |
| 241 | if ( !isset( $from_email ) ) { |
| 242 | // Get the site domain and get rid of www. |
| 243 | $sitename = strtolower( $_SERVER['SERVER_NAME'] ); |
| 244 | if ( substr( $sitename, 0, 4 ) == 'www.' ) { |
| 245 | $sitename = substr( $sitename, 4 ); |
| 246 | } |
| 247 | |
| 248 | $from_email = 'wordpress@' . $sitename; |
| 249 | } |
| 250 | |
| 251 | // Set the from name and email |
| 252 | $phpmailer->From = apply_filters( 'wp_mail_from', $from_email ); |
| 253 | $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name ); |
| 254 | |
| 255 | // Set destination address |
| 256 | $phpmailer->AddAddress( $to ); |
| 257 | |
| 258 | // Set mail's subject and body |
189 | | $phpmailer->Body = $message; |
190 | | $phpmailer->IsHTML(false); |
191 | | $phpmailer->IsMail(); // set mailer to use php mail() |
192 | | |
193 | | do_action_ref_array('phpmailer_init', array(&$phpmailer)); |
194 | | |
195 | | $mailheaders = (array) explode( "\n", $headers ); |
196 | | foreach ( $mailheaders as $line ) { |
197 | | $header = explode( ":", $line ); |
198 | | switch ( trim( $header[0] ) ) { |
199 | | case "From": |
200 | | $from = trim( str_replace( '"', '', $header[1] ) ); |
201 | | if ( strpos( $from, '<' ) ) { |
202 | | $phpmailer->FromName = str_replace( '"', '', substr( $header[1], 0, strpos( $header[1], '<' ) - 1 ) ); |
203 | | $from = trim( substr( $from, strpos( $from, '<' ) + 1 ) ); |
204 | | $from = str_replace( '>', '', $from ); |
205 | | } else { |
206 | | $phpmailer->FromName = $from; |
207 | | } |
208 | | $phpmailer->From = trim( $from ); |
209 | | break; |
210 | | default: |
211 | | if ( $line != '' && $header[0] != 'MIME-Version' && $header[0] != 'Content-Type' ) |
212 | | $phpmailer->AddCustomHeader( $line ); |
213 | | break; |
| 260 | $phpmailer->Body = $message; |
| 261 | |
| 262 | // Set to use PHP's mail() |
| 263 | $phpmailer->IsMail(); |
| 264 | |
| 265 | // Set Content-Type and charset |
| 266 | // If we don't have a content-type from the input headers |
| 267 | if ( !isset( $content_type ) ) { |
| 268 | $content_type = 'text/plain'; |
| 269 | } |
| 270 | |
| 271 | // Set whether it's plaintext or not, depending on $content_type |
| 272 | if ( $content_type == 'text/html' ) { |
| 273 | $phpmailer->IsHTML( true ); |
| 274 | } else { |
| 275 | $phpmailer->IsHTML( false ); |
| 276 | } |
| 277 | |
| 278 | // If we don't have a charset from the input headers |
| 279 | if ( !isset( $charset ) ) { |
| 280 | $charset = get_bloginfo( 'charset' ); |
| 281 | } |
| 282 | |
| 283 | // Set the content-type and charset |
| 284 | $phpmailer->ContentType = apply_filters( 'wp_mail_content_type', 'text/plain' ); |
| 285 | $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset ); |
| 286 | |
| 287 | // Set custom headers |
| 288 | if ( !empty( $headers ) ) { |
| 289 | foreach ( $headers as $name => $content ) { |
| 290 | $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) ); |