Guest User

Untitled

a guest
Sep 18th, 2018
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.46 KB | None | 0 0
  1. // Set $to to an hotmail.com or outlook.com email
  2.  
  3. $subject = 'wp_mail testing multipart';
  4.  
  5. $message = '------=_Part_18243133_1346573420.1408991447668
  6. Content-Type: text/plain; charset=UTF-8
  7.  
  8. Hello world! This is plain text...
  9.  
  10.  
  11. ------=_Part_18243133_1346573420.1408991447668
  12. Content-Type: text/html; charset=UTF-8
  13.  
  14. <html>
  15. <head>
  16. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  17. </head>
  18. <body>
  19.  
  20. <p>Hello World! This is HTML...</p>
  21.  
  22. </body>
  23. </html>
  24.  
  25.  
  26. ------=_Part_18243133_1346573420.1408991447668--';
  27.  
  28. $headers = "MIME-Version: 1.0rn";
  29. $headers .= "From: Foo <[email protected]>rn";
  30. $headers .= 'Content-Type: multipart/alternative;boundary="----=_Part_18243133_1346573420.1408991447668"';
  31.  
  32.  
  33. // send email
  34. wp_mail( $to, $subject, $message, $headers );
  35.  
  36. add_filter( 'wp_mail_content_type', 'set_content_type' );
  37. function set_content_type( $content_type ) {
  38. return 'multipart/alternative';
  39. }
  40.  
  41. MIME-Version: 1.0
  42. Content-Type: multipart/alternative;
  43. boundary="====f230673f9d7c359a81ffebccb88e5d61=="
  44. MIME-Version: 1.0
  45. Content-Type: multipart/alternative; charset=
  46.  
  47. // Set Content-Type and charset
  48. // If we don't have a content-type from the input headers
  49. if ( !isset( $content_type ) )
  50. $content_type = 'text/plain';
  51.  
  52. /**
  53. * Filter the wp_mail() content type.
  54. *
  55. * @since 2.3.0
  56. *
  57. * @param string $content_type Default wp_mail() content type.
  58. */
  59. $content_type = apply_filters( 'wp_mail_content_type', $content_type );
  60.  
  61. $phpmailer->ContentType = $content_type;
  62.  
  63. // Set whether it's plaintext, depending on $content_type
  64. if ( 'text/html' == $content_type )
  65. $phpmailer->IsHTML( true );
  66.  
  67. // If we don't have a charset from the input headers
  68. if ( !isset( $charset ) )
  69. $charset = get_bloginfo( 'charset' );
  70.  
  71. // Set the content-type and charset
  72.  
  73. /**
  74. * Filter the default wp_mail() charset.
  75. *
  76. * @since 2.3.0
  77. *
  78. * @param string $charset Default email charset.
  79. */
  80. $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
  81.  
  82. // Set custom headers
  83. if ( !empty( $headers ) ) {
  84. foreach( (array) $headers as $name => $content ) {
  85. $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
  86. }
  87.  
  88. if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
  89. $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;nt boundary="%s"", $content_type, $boundary ) );
  90. }
  91.  
  92. if ( !empty( $attachments ) ) {
  93. foreach ( $attachments as $attachment ) {
  94. try {
  95. $phpmailer->AddAttachment($attachment);
  96. } catch ( phpmailerException $e ) {
  97. continue;
  98. }
  99. }
  100. }
  101.  
  102. if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) ) {
  103. $phpmailer->ContentType = $content_type . "; boundary=" . $boundary;
  104. }
  105. else {
  106.  
  107. $content_type = apply_filters( 'wp_mail_content_type', $content_type );
  108.  
  109. $phpmailer->ContentType = $content_type;
  110.  
  111. // Set whether it's plaintext, depending on $content_type
  112. if ( 'text/html' == $content_type )
  113. $phpmailer->IsHTML( true );
  114.  
  115. // If we don't have a charset from the input headers
  116. if ( !isset( $charset ) )
  117. $charset = get_bloginfo( 'charset' );
  118. }
  119.  
  120. // Set the content-type and charset
  121.  
  122. /**
  123. * Filter the default wp_mail() charset.
  124. *
  125. * @since 2.3.0
  126. *
  127. * @param string $charset Default email charset.
  128. */
  129. $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
  130.  
  131. // Set custom headers
  132. if ( !empty( $headers ) ) {
  133. foreach( (array) $headers as $name => $content ) {
  134. $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
  135. }
  136.  
  137. }
  138.  
  139. /**
  140. * Send mail, similar to PHP's mail
  141. *
  142. * A true return value does not automatically mean that the user received the
  143. * email successfully. It just only means that the method used was able to
  144. * process the request without any errors.
  145. *
  146. * Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks allow from
  147. * creating a from address like 'Name <[email protected]>' when both are set. If
  148. * just 'wp_mail_from' is set, then just the email address will be used with no
  149. * name.
  150. *
  151. * The default content type is 'text/plain' which does not allow using HTML.
  152. * However, you can set the content type of the email by using the
  153. * 'wp_mail_content_type' filter.
  154. *
  155. * If $message is an array, the key of each is used to add as an attachment
  156. * with the value used as the body. The 'text/plain' element is used as the
  157. * text version of the body, with the 'text/html' element used as the HTML
  158. * version of the body. All other types are added as attachments.
  159. *
  160. * The default charset is based on the charset used on the blog. The charset can
  161. * be set using the 'wp_mail_charset' filter.
  162. *
  163. * @since 1.2.1
  164. *
  165. * @uses PHPMailer
  166. *
  167. * @param string|array $to Array or comma-separated list of email addresses to send message.
  168. * @param string $subject Email subject
  169. * @param string|array $message Message contents
  170. * @param string|array $headers Optional. Additional headers.
  171. * @param string|array $attachments Optional. Files to attach.
  172. * @return bool Whether the email contents were sent successfully.
  173. */
  174. function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
  175. // Compact the input, apply the filters, and extract them back out
  176.  
  177. /**
  178. * Filter the wp_mail() arguments.
  179. *
  180. * @since 2.2.0
  181. *
  182. * @param array $args A compacted array of wp_mail() arguments, including the "to" email,
  183. * subject, message, headers, and attachments values.
  184. */
  185. $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
  186.  
  187. if ( isset( $atts['to'] ) ) {
  188. $to = $atts['to'];
  189. }
  190.  
  191. if ( isset( $atts['subject'] ) ) {
  192. $subject = $atts['subject'];
  193. }
  194.  
  195. if ( isset( $atts['message'] ) ) {
  196. $message = $atts['message'];
  197. }
  198.  
  199. if ( isset( $atts['headers'] ) ) {
  200. $headers = $atts['headers'];
  201. }
  202.  
  203. if ( isset( $atts['attachments'] ) ) {
  204. $attachments = $atts['attachments'];
  205. }
  206.  
  207. if ( ! is_array( $attachments ) ) {
  208. $attachments = explode( "n", str_replace( "rn", "n", $attachments ) );
  209. }
  210. global $phpmailer;
  211.  
  212. // (Re)create it, if it's gone missing
  213. if ( ! ( $phpmailer instanceof PHPMailer ) ) {
  214. require_once ABSPATH . WPINC . '/class-phpmailer.php';
  215. require_once ABSPATH . WPINC . '/class-smtp.php';
  216. $phpmailer = new PHPMailer( true );
  217. }
  218.  
  219. // Headers
  220. if ( empty( $headers ) ) {
  221. $headers = array();
  222. } else {
  223. if ( !is_array( $headers ) ) {
  224. // Explode the headers out, so this function can take both
  225. // string headers and an array of headers.
  226. $tempheaders = explode( "n", str_replace( "rn", "n", $headers ) );
  227. } else {
  228. $tempheaders = $headers;
  229. }
  230. $headers = array();
  231. $cc = array();
  232. $bcc = array();
  233.  
  234. // If it's actually got contents
  235. if ( !empty( $tempheaders ) ) {
  236. // Iterate through the raw headers
  237. foreach ( (array) $tempheaders as $header ) {
  238. if ( strpos($header, ':') === false ) {
  239. if ( false !== stripos( $header, 'boundary=' ) ) {
  240. $parts = preg_split('/boundary=/i', trim( $header ) );
  241. $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
  242. }
  243. continue;
  244. }
  245. // Explode them out
  246. list( $name, $content ) = explode( ':', trim( $header ), 2 );
  247.  
  248. // Cleanup crew
  249. $name = trim( $name );
  250. $content = trim( $content );
  251.  
  252. switch ( strtolower( $name ) ) {
  253. // Mainly for legacy -- process a From: header if it's there
  254. case 'from':
  255. $bracket_pos = strpos( $content, '<' );
  256. if ( $bracket_pos !== false ) {
  257. // Text before the bracketed email is the "From" name.
  258. if ( $bracket_pos > 0 ) {
  259. $from_name = substr( $content, 0, $bracket_pos - 1 );
  260. $from_name = str_replace( '"', '', $from_name );
  261. $from_name = trim( $from_name );
  262. }
  263.  
  264. $from_email = substr( $content, $bracket_pos + 1 );
  265. $from_email = str_replace( '>', '', $from_email );
  266. $from_email = trim( $from_email );
  267.  
  268. // Avoid setting an empty $from_email.
  269. } elseif ( '' !== trim( $content ) ) {
  270. $from_email = trim( $content );
  271. }
  272. break;
  273. case 'content-type':
  274. if ( is_array($message) ) {
  275. // Multipart email, ignore the content-type header
  276. break;
  277. }
  278. if ( strpos( $content, ';' ) !== false ) {
  279. list( $type, $charset_content ) = explode( ';', $content );
  280. $content_type = trim( $type );
  281. if ( false !== stripos( $charset_content, 'charset=' ) ) {
  282. $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) );
  283. } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) {
  284. $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) );
  285. $charset = '';
  286. }
  287.  
  288. // Avoid setting an empty $content_type.
  289. } elseif ( '' !== trim( $content ) ) {
  290. $content_type = trim( $content );
  291. }
  292. break;
  293. case 'cc':
  294. $cc = array_merge( (array) $cc, explode( ',', $content ) );
  295. break;
  296. case 'bcc':
  297. $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
  298. break;
  299. default:
  300. // Add it to our grand headers array
  301. $headers[trim( $name )] = trim( $content );
  302. break;
  303. }
  304. }
  305. }
  306. }
  307.  
  308. // Empty out the values that may be set
  309. $phpmailer->ClearAllRecipients();
  310. $phpmailer->ClearAttachments();
  311. $phpmailer->ClearCustomHeaders();
  312. $phpmailer->ClearReplyTos();
  313.  
  314. $phpmailer->Body= '';
  315. $phpmailer->AltBody= '';
  316.  
  317. // From email and name
  318. // If we don't have a name from the input headers
  319. if ( !isset( $from_name ) )
  320. $from_name = 'WordPress';
  321.  
  322. /* If we don't have an email from the input headers default to wordpress@$sitename
  323. * Some hosts will block outgoing mail from this address if it doesn't exist but
  324. * there's no easy alternative. Defaulting to admin_email might appear to be another
  325. * option but some hosts may refuse to relay mail from an unknown domain. See
  326. * https://core.trac.wordpress.org/ticket/5007.
  327. */
  328.  
  329. if ( !isset( $from_email ) ) {
  330. // Get the site domain and get rid of www.
  331. $sitename = strtolower( $_SERVER['SERVER_NAME'] );
  332. if ( substr( $sitename, 0, 4 ) == 'www.' ) {
  333. $sitename = substr( $sitename, 4 );
  334. }
  335.  
  336. $from_email = 'wordpress@' . $sitename;
  337. }
  338.  
  339. /**
  340. * Filter the email address to send from.
  341. *
  342. * @since 2.2.0
  343. *
  344. * @param string $from_email Email address to send from.
  345. */
  346. $phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
  347.  
  348. /**
  349. * Filter the name to associate with the "from" email address.
  350. *
  351. * @since 2.3.0
  352. *
  353. * @param string $from_name Name associated with the "from" email address.
  354. */
  355. $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
  356.  
  357. // Set destination addresses
  358. if ( !is_array( $to ) )
  359. $to = explode( ',', $to );
  360.  
  361. foreach ( (array) $to as $recipient ) {
  362. try {
  363. // Break $recipient into name and address parts if in the format "Foo <[email protected]>"
  364. $recipient_name = '';
  365. if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
  366. if ( count( $matches ) == 3 ) {
  367. $recipient_name = $matches[1];
  368. $recipient = $matches[2];
  369. }
  370. }
  371. $phpmailer->AddAddress( $recipient, $recipient_name);
  372. } catch ( phpmailerException $e ) {
  373. continue;
  374. }
  375. }
  376.  
  377. // If we don't have a charset from the input headers
  378. if ( !isset( $charset ) )
  379. $charset = get_bloginfo( 'charset' );
  380.  
  381. // Set the content-type and charset
  382.  
  383. /**
  384. * Filter the default wp_mail() charset.
  385. *
  386. * @since 2.3.0
  387. *
  388. * @param string $charset Default email charset.
  389. */
  390. $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
  391.  
  392. // Set mail's subject and body
  393. $phpmailer->Subject = $subject;
  394.  
  395. if ( is_string($message) ) {
  396. $phpmailer->Body = $message;
  397.  
  398. // Set Content-Type and charset
  399. // If we don't have a content-type from the input headers
  400. if ( !isset( $content_type ) )
  401. $content_type = 'text/plain';
  402.  
  403. /**
  404. * Filter the wp_mail() content type.
  405. *
  406. * @since 2.3.0
  407. *
  408. * @param string $content_type Default wp_mail() content type.
  409. */
  410. $content_type = apply_filters( 'wp_mail_content_type', $content_type );
  411.  
  412. $phpmailer->ContentType = $content_type;
  413.  
  414. // Set whether it's plaintext, depending on $content_type
  415. if ( 'text/html' == $content_type )
  416. $phpmailer->IsHTML( true );
  417.  
  418. // For backwards compatibility, new multipart emails should use
  419. // the array style $message. This never really worked well anyway
  420. if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
  421. $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;nt boundary="%s"", $content_type, $boundary ) );
  422. }
  423. elseif ( is_array($message) ) {
  424. foreach ($message as $type => $bodies) {
  425. foreach ((array) $bodies as $body) {
  426. if ($type === 'text/html') {
  427. $phpmailer->Body = $body;
  428. }
  429. elseif ($type === 'text/plain') {
  430. $phpmailer->AltBody = $body;
  431. }
  432. else {
  433. $phpmailer->AddAttachment($body, '', 'base64', $type);
  434. }
  435. }
  436. }
  437. }
  438.  
  439. // Add any CC and BCC recipients
  440. if ( !empty( $cc ) ) {
  441. foreach ( (array) $cc as $recipient ) {
  442. try {
  443. // Break $recipient into name and address parts if in the format "Foo <[email protected]>"
  444. $recipient_name = '';
  445. if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
  446. if ( count( $matches ) == 3 ) {
  447. $recipient_name = $matches[1];
  448. $recipient = $matches[2];
  449. }
  450. }
  451. $phpmailer->AddCc( $recipient, $recipient_name );
  452. } catch ( phpmailerException $e ) {
  453. continue;
  454. }
  455. }
  456. }
  457.  
  458. if ( !empty( $bcc ) ) {
  459. foreach ( (array) $bcc as $recipient) {
  460. try {
  461. // Break $recipient into name and address parts if in the format "Foo <[email protected]>"
  462. $recipient_name = '';
  463. if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
  464. if ( count( $matches ) == 3 ) {
  465. $recipient_name = $matches[1];
  466. $recipient = $matches[2];
  467. }
  468. }
  469. $phpmailer->AddBcc( $recipient, $recipient_name );
  470. } catch ( phpmailerException $e ) {
  471. continue;
  472. }
  473. }
  474. }
  475.  
  476. // Set to use PHP's mail()
  477. $phpmailer->IsMail();
  478.  
  479. // Set custom headers
  480. if ( !empty( $headers ) ) {
  481. foreach ( (array) $headers as $name => $content ) {
  482. $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
  483. }
  484. }
  485.  
  486. if ( !empty( $attachments ) ) {
  487. foreach ( $attachments as $attachment ) {
  488. try {
  489. $phpmailer->AddAttachment($attachment);
  490. } catch ( phpmailerException $e ) {
  491. continue;
  492. }
  493. }
  494. }
  495.  
  496. /**
  497. * Fires after PHPMailer is initialized.
  498. *
  499. * @since 2.2.0
  500. *
  501. * @param PHPMailer &$phpmailer The PHPMailer instance, passed by reference.
  502. */
  503. do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
  504.  
  505. // Send!
  506. try {
  507. return $phpmailer->Send();
  508. } catch ( phpmailerException $e ) {
  509. return false;
  510. }
  511. }
  512.  
  513. // Set $to to an hotmail.com or outlook.com email
  514.  
  515. $subject = 'wp_mail testing multipart';
  516.  
  517. $message['text/plain'] = 'Hello world! This is plain text...';
  518. $message['text/html'] = '<html>
  519. <head>
  520. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  521. </head>
  522. <body>
  523.  
  524. <p>Hello World! This is HTML...</p>
  525.  
  526. </body>
  527. </html>';
  528.  
  529. add_filter( 'wp_mail_from', $from_func = function ( $from_email ) { return '[email protected]'; } );
  530. add_filter( 'wp_mail_from_name', $from_name_func = function ( $from_name ) { return 'Foo'; } );
  531.  
  532. // send email
  533. wp_mail( $to, $subject, $message );
  534.  
  535. remove_filter( 'wp_mail_from', $from_func );
  536. remove_filter( 'wp_mail_from_name', $from_name_func );
  537.  
  538. public function getMailMIME()
  539. {
  540. $result = '';
  541. $ismultipart = true;
  542. switch ($this->message_type) {
  543. case 'inline':
  544. $result .= $this->headerLine('Content-Type', 'multipart/related;');
  545. $result .= $this->textLine("tboundary="" . $this->boundary[1] . '"');
  546. break;
  547. case 'attach':
  548. case 'inline_attach':
  549. case 'alt_attach':
  550. case 'alt_inline_attach':
  551. $result .= $this->headerLine('Content-Type', 'multipart/mixed;');
  552. $result .= $this->textLine("tboundary="" . $this->boundary[1] . '"');
  553. break;
  554. case 'alt':
  555. case 'alt_inline':
  556. $result .= $this->headerLine('Content-Type', 'multipart/alternative;');
  557. $result .= $this->textLine("tboundary="" . $this->boundary[1] . '"');
  558. break;
  559. default:
  560. // Catches case 'plain': and case '':
  561. $result .= $this->textLine('Content-Type: ' . $this->ContentType . '; charset=' . $this->CharSet);
  562. $ismultipart = false;
  563. break;
  564. }
  565.  
  566. protected function setMessageType()
  567. {
  568. $type = array();
  569. if ($this->alternativeExists()) {
  570. $type[] = 'alt';
  571. }
  572. if ($this->inlineImageExists()) {
  573. $type[] = 'inline';
  574. }
  575. if ($this->attachmentExists()) {
  576. $type[] = 'attach';
  577. }
  578. $this->message_type = implode('_', $type);
  579. if ($this->message_type == '') {
  580. $this->message_type = 'plain';
  581. }
  582. }
  583.  
  584. public function alternativeExists()
  585. {
  586. return !empty($this->AltBody);
  587. }
  588.  
  589. add_action('phpmailer_init','wp_mail_set_text_body');
  590. function wp_mail_set_text_body($phpmailer) {
  591. if (empty($phpmailer->AltBody)) {$phpmailer->AltBody = strip_tags($phpmailer->Body);}
  592. }
  593.  
  594. $message ='<html>
  595. <head>
  596. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  597. </head>
  598. <body>
  599. <p>Hello World! This is HTML...</p>
  600. </body>
  601. </html>';
  602.  
  603. wp_mail($to,$subject,$message);
  604.  
  605. add_action( 'phpmailer_init', array($this->mailer, 'send_email' ) );
  606.  
  607. /**
  608. * Modify php mailer body with final email
  609. *
  610. * @since 1.0.0
  611. * @param object $phpmailer
  612. */
  613. function send_email( $phpmailer ) {
  614.  
  615. $message = $this->add_template( apply_filters( 'mailtpl/email_content', $phpmailer->Body ) );
  616. $phpmailer->AltBody = $this->replace_placeholders( strip_tags($phpmailer->Body) );
  617. $phpmailer->Body = $this->replace_placeholders( $message );
  618. }
  619.  
  620. add_action( 'phpmailer_init', 'phpmailer_init' );
  621.  
  622. //http://wordpress.stackexchange.com/a/191974
  623. //http://stackoverflow.com/a/2564472
  624. function phpmailer_init( $phpmailer )
  625. {
  626. if( $phpmailer->ContentType == 'text/html' ) {
  627. $phpmailer->AltBody = Html2TextHtml2Text::convert( $phpmailer->Body );
  628. }
  629. }
  630.  
  631. /* setting the message parts for wp_mail()*/
  632. $markup = array();
  633. $markup['html'] = '<html>some html</html>';
  634. $markup['plaintext'] = 'some plaintext';
  635. /* message we are sending */
  636. $message = maybe_serialize($markup);
  637.  
  638.  
  639. /* setting alt body distinctly */
  640. add_action('phpmailer_init', array($this, 'set_alt_mail_body'));
  641.  
  642. function set_alt_mail_body($phpmailer){
  643. if( $phpmailer->ContentType == 'text/html' ) {
  644. $body_parts = maybe_unserialize($phpmailer->Body);
  645.  
  646. if(!empty($body_parts['html'])){
  647. $phpmailer->MsgHTML($body_parts['html']);
  648. }
  649.  
  650. if(!empty($body_parts['plaintext'])){
  651. $phpmailer->AltBody = $body_parts['plaintext'];
  652. }
  653. }
  654. }
  655.  
  656. <?php
  657.  
  658. $to = '';
  659. $subject = '';
  660. $from = '';
  661. $body = 'The text html content, <html>...';
  662.  
  663. $headers = "FROM: {$from}";
  664.  
  665. add_action( 'phpmailer_init', function () {
  666. global $phpmailer;
  667. $phpmailer->AltBody = 'The text plain content of your original text html content.';
  668. } );
  669.  
  670. wp_mail($to, $subject, $body, $headers);
Add Comment
Please, Sign In to add comment