Guest User

Untitled

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