Gmail SMTP - Version 1.0.8

Version Description

  • Gmail SMTP now supports the "wp_mail_failed" hook which fires after a phpmailerException is caught.
Download this release

Release Info

Developer naa986
Plugin Icon 128x128 Gmail SMTP
Version 1.0.8
Comparing to
See all releases

Code changes from version 1.0.7 to 1.0.8

Files changed (2) hide show
  1. main.php +367 -350
  2. readme.txt +5 -2
main.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
  /*
3
  Plugin Name: Gmail SMTP
4
- Version: 1.0.7
5
  Plugin URI: http://wphowto.net/
6
  Author: naa986
7
  Author URI: http://wphowto.net/
@@ -16,7 +16,7 @@ if (!defined('ABSPATH')){
16
 
17
  class GMAIL_SMTP {
18
 
19
- var $plugin_version = '1.0.7';
20
  var $phpmailer_version = '5.2.14';
21
  var $plugin_url;
22
  var $plugin_path;
@@ -578,361 +578,378 @@ function is_gmail_smtp_configured() {
578
  $GLOBALS['gmail-smtp'] = new GMAIL_SMTP();
579
 
580
  if(!function_exists('wp_mail') && is_gmail_smtp_configured()){
581
- function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
582
- // Compact the input, apply the filters, and extract them back out
583
-
584
- /**
585
- * Filter the wp_mail() arguments.
586
- *
587
- * @since 2.2.0
588
- *
589
- * @param array $args A compacted array of wp_mail() arguments, including the "to" email,
590
- * subject, message, headers, and attachments values.
591
- */
592
- $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
593
-
594
- if ( isset( $atts['to'] ) ) {
595
- $to = $atts['to'];
596
- }
597
-
598
- if ( isset( $atts['subject'] ) ) {
599
- $subject = $atts['subject'];
600
- }
601
-
602
- if ( isset( $atts['message'] ) ) {
603
- $message = $atts['message'];
604
- }
605
-
606
- if ( isset( $atts['headers'] ) ) {
607
- $headers = $atts['headers'];
608
- }
609
-
610
- if ( isset( $atts['attachments'] ) ) {
611
- $attachments = $atts['attachments'];
612
- }
613
-
614
- if ( ! is_array( $attachments ) ) {
615
- $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
616
- }
617
-
618
- $options = gmail_smtp_get_option();
619
-
620
- $phpmailer = new PHPMailerOAuth; /* this must be the custom class we created */
621
 
622
- // Tell PHPMailer to use SMTP
623
- $phpmailer->isSMTP();
624
-
625
- // Set AuthType
626
- $phpmailer->AuthType = 'XOAUTH2';
627
 
628
- // Whether to use SMTP authentication
629
- $phpmailer->SMTPAuth = true;
 
630
 
631
- // Set the encryption system to use - ssl (deprecated) or tls
632
- $phpmailer->SMTPSecure = $options['type_of_encryption'];
 
633
 
634
- // Set the hostname of the mail server
635
- $phpmailer->Host = 'smtp.gmail.com';
 
636
 
637
- // Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
638
- $phpmailer->Port = $options['smtp_port'];
639
-
640
- $phpmailer->SMTPAutoTLS = false;
641
-
642
- //enable debug when sending a test mail
643
- if(isset($_POST['gmail_smtp_send_test_email'])){
644
- $phpmailer->SMTPDebug = 4;
645
- // Ask for HTML-friendly debug output
646
- $phpmailer->Debugoutput = 'html';
647
- }
648
-
649
- //disable ssl certificate verification if checked
650
- if(isset($options['disable_ssl_verification']) && !empty($options['disable_ssl_verification'])){
651
- $phpmailer->SMTPOptions = array(
652
- 'ssl' => array(
653
- 'verify_peer' => false,
654
- 'verify_peer_name' => false,
655
- 'allow_self_signed' => true
656
- )
657
- );
658
- }
659
- // User Email to use for SMTP authentication - Use the same Email used in Google Developer Console
660
- $phpmailer->oauthUserEmail = $options['oauth_user_email'];
661
 
662
- //Obtained From Google Developer Console
663
- $phpmailer->oauthClientId = $options['oauth_client_id'];
664
 
665
- //Obtained From Google Developer Console
666
- $phpmailer->oauthClientSecret = $options['oauth_client_secret'];
667
 
668
- $gmail_token = json_decode($options['oauth_access_token'], true);
 
669
 
670
- //Obtained By running get_oauth_token.php after setting up APP in Google Developer Console.
671
- //Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
672
- // eg: http://localhost/phpmail/get_oauth_token.php
673
- $phpmailer->oauthRefreshToken = $gmail_token['refresh_token'];
674
-
675
- // Headers
676
- if ( empty( $headers ) ) {
677
- $headers = array();
678
- } else {
679
- if ( !is_array( $headers ) ) {
680
- // Explode the headers out, so this function can take both
681
- // string headers and an array of headers.
682
- $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
683
- } else {
684
- $tempheaders = $headers;
685
- }
686
- $headers = array();
687
- $cc = array();
688
- $bcc = array();
689
-
690
- // If it's actually got contents
691
- if ( !empty( $tempheaders ) ) {
692
- // Iterate through the raw headers
693
- foreach ( (array) $tempheaders as $header ) {
694
- if ( strpos($header, ':') === false ) {
695
- if ( false !== stripos( $header, 'boundary=' ) ) {
696
- $parts = preg_split('/boundary=/i', trim( $header ) );
697
- $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
698
- }
699
- continue;
700
- }
701
- // Explode them out
702
- list( $name, $content ) = explode( ':', trim( $header ), 2 );
703
-
704
- // Cleanup crew
705
- $name = trim( $name );
706
- $content = trim( $content );
707
-
708
- switch ( strtolower( $name ) ) {
709
- // Mainly for legacy -- process a From: header if it's there
710
- case 'from':
711
- $bracket_pos = strpos( $content, '<' );
712
- if ( $bracket_pos !== false ) {
713
- // Text before the bracketed email is the "From" name.
714
- if ( $bracket_pos > 0 ) {
715
- $from_name = substr( $content, 0, $bracket_pos - 1 );
716
- $from_name = str_replace( '"', '', $from_name );
717
- $from_name = trim( $from_name );
718
- }
719
-
720
- $from_email = substr( $content, $bracket_pos + 1 );
721
- $from_email = str_replace( '>', '', $from_email );
722
- $from_email = trim( $from_email );
723
-
724
- // Avoid setting an empty $from_email.
725
- } elseif ( '' !== trim( $content ) ) {
726
- $from_email = trim( $content );
727
- }
728
- break;
729
- case 'content-type':
730
- if ( strpos( $content, ';' ) !== false ) {
731
- list( $type, $charset_content ) = explode( ';', $content );
732
- $content_type = trim( $type );
733
- if ( false !== stripos( $charset_content, 'charset=' ) ) {
734
- $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) );
735
- } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) {
736
- $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) );
737
- $charset = '';
738
- }
739
-
740
- // Avoid setting an empty $content_type.
741
- } elseif ( '' !== trim( $content ) ) {
742
- $content_type = trim( $content );
743
- }
744
- break;
745
- case 'cc':
746
- $cc = array_merge( (array) $cc, explode( ',', $content ) );
747
- break;
748
- case 'bcc':
749
- $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
750
- break;
751
- default:
752
- // Add it to our grand headers array
753
- $headers[trim( $name )] = trim( $content );
754
- break;
755
- }
756
- }
757
- }
758
- }
759
-
760
- // Empty out the values that may be set
761
- $phpmailer->ClearAllRecipients();
762
- $phpmailer->ClearAttachments();
763
- $phpmailer->ClearCustomHeaders();
764
- $phpmailer->ClearReplyTos();
765
-
766
- // From email and name
767
- // If we don't have a name from the input headers
768
- if ( !isset( $from_name ) ){
769
- $from_name = $options['from_name'];//'WordPress';
770
- }
771
- /* If we don't have an email from the input headers default to wordpress@$sitename
772
- * Some hosts will block outgoing mail from this address if it doesn't exist but
773
- * there's no easy alternative. Defaulting to admin_email might appear to be another
774
- * option but some hosts may refuse to relay mail from an unknown domain. See
775
- * https://core.trac.wordpress.org/ticket/5007.
776
- */
777
-
778
- if ( !isset( $from_email ) ) {
779
- // Get the site domain and get rid of www.
780
- $sitename = strtolower( $_SERVER['SERVER_NAME'] );
781
- if ( substr( $sitename, 0, 4 ) == 'www.' ) {
782
- $sitename = substr( $sitename, 4 );
783
- }
784
-
785
- $from_email = $options['from_email'];//'wordpress@' . $sitename;
786
- }
787
-
788
- /**
789
- * Filter the email address to send from.
790
- *
791
- * @since 2.2.0
792
- *
793
- * @param string $from_email Email address to send from.
794
- */
795
- $phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
796
-
797
- /**
798
- * Filter the name to associate with the "from" email address.
799
- *
800
- * @since 2.3.0
801
- *
802
- * @param string $from_name Name associated with the "from" email address.
803
- */
804
- $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
805
-
806
- // Set destination addresses
807
- if ( !is_array( $to ) )
808
- $to = explode( ',', $to );
809
-
810
- foreach ( (array) $to as $recipient ) {
811
- try {
812
- // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
813
- $recipient_name = '';
814
- if ( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
815
- if ( count( $matches ) == 3 ) {
816
- $recipient_name = $matches[1];
817
- $recipient = $matches[2];
818
- }
819
- }
820
- $phpmailer->AddAddress( $recipient, $recipient_name);
821
- } catch ( phpmailerException $e ) {
822
- continue;
823
- }
824
- }
825
-
826
- // Set mail's subject and body
827
- $phpmailer->Subject = $subject;
828
- $phpmailer->Body = $message;
829
-
830
- // Add any CC and BCC recipients
831
- if ( !empty( $cc ) ) {
832
- foreach ( (array) $cc as $recipient ) {
833
- try {
834
- // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
835
- $recipient_name = '';
836
- if ( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
837
- if ( count( $matches ) == 3 ) {
838
- $recipient_name = $matches[1];
839
- $recipient = $matches[2];
840
- }
841
- }
842
- $phpmailer->AddCc( $recipient, $recipient_name );
843
- } catch ( phpmailerException $e ) {
844
- continue;
845
- }
846
- }
847
- }
848
-
849
- if ( !empty( $bcc ) ) {
850
- foreach ( (array) $bcc as $recipient) {
851
- try {
852
- // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
853
- $recipient_name = '';
854
- if ( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
855
- if ( count( $matches ) == 3 ) {
856
- $recipient_name = $matches[1];
857
- $recipient = $matches[2];
858
- }
859
- }
860
- $phpmailer->AddBcc( $recipient, $recipient_name );
861
- } catch ( phpmailerException $e ) {
862
- continue;
863
- }
864
- }
865
- }
866
-
867
- // Set Content-Type and charset
868
- // If we don't have a content-type from the input headers
869
- if ( !isset( $content_type ) )
870
- $content_type = 'text/plain';
871
-
872
- /**
873
- * Filter the wp_mail() content type.
874
- *
875
- * @since 2.3.0
876
- *
877
- * @param string $content_type Default wp_mail() content type.
878
- */
879
- $content_type = apply_filters( 'wp_mail_content_type', $content_type );
880
-
881
- $phpmailer->ContentType = $content_type;
882
-
883
- // Set whether it's plaintext, depending on $content_type
884
- if ( 'text/html' == $content_type )
885
- $phpmailer->IsHTML( true );
886
-
887
- // If we don't have a charset from the input headers
888
- if ( !isset( $charset ) )
889
- $charset = get_bloginfo( 'charset' );
890
-
891
- // Set the content-type and charset
892
-
893
- /**
894
- * Filter the default wp_mail() charset.
895
- *
896
- * @since 2.3.0
897
- *
898
- * @param string $charset Default email charset.
899
- */
900
- $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
901
-
902
- // Set custom headers
903
- if ( !empty( $headers ) ) {
904
- foreach( (array) $headers as $name => $content ) {
905
- $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
906
- }
907
-
908
- if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
909
- $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
910
- }
911
-
912
- if ( !empty( $attachments ) ) {
913
- foreach ( $attachments as $attachment ) {
914
- try {
915
- $phpmailer->AddAttachment($attachment);
916
- } catch ( phpmailerException $e ) {
917
- continue;
918
- }
919
- }
920
- }
921
-
922
- /**
923
- * Fires after PHPMailer is initialized.
924
- *
925
- * @since 2.2.0
926
- *
927
- * @param PHPMailer &$phpmailer The PHPMailer instance, passed by reference.
928
- */
929
- //do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
930
-
931
- // Send!
932
- try {
933
- return $phpmailer->Send();
934
- } catch ( phpmailerException $e ) {
935
- return false;
936
- }
937
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
938
  }
1
  <?php
2
  /*
3
  Plugin Name: Gmail SMTP
4
+ Version: 1.0.8
5
  Plugin URI: http://wphowto.net/
6
  Author: naa986
7
  Author URI: http://wphowto.net/
16
 
17
  class GMAIL_SMTP {
18
 
19
+ var $plugin_version = '1.0.8';
20
  var $phpmailer_version = '5.2.14';
21
  var $plugin_url;
22
  var $plugin_path;
578
  $GLOBALS['gmail-smtp'] = new GMAIL_SMTP();
579
 
580
  if(!function_exists('wp_mail') && is_gmail_smtp_configured()){
581
+
582
+ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
583
+ // Compact the input, apply the filters, and extract them back out
584
+
585
+ /**
586
+ * Filters the wp_mail() arguments.
587
+ *
588
+ * @since 2.2.0
589
+ *
590
+ * @param array $args A compacted array of wp_mail() arguments, including the "to" email,
591
+ * subject, message, headers, and attachments values.
592
+ */
593
+ $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
594
+
595
+ if ( isset( $atts['to'] ) ) {
596
+ $to = $atts['to'];
597
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
598
 
599
+ if ( isset( $atts['subject'] ) ) {
600
+ $subject = $atts['subject'];
601
+ }
 
 
602
 
603
+ if ( isset( $atts['message'] ) ) {
604
+ $message = $atts['message'];
605
+ }
606
 
607
+ if ( isset( $atts['headers'] ) ) {
608
+ $headers = $atts['headers'];
609
+ }
610
 
611
+ if ( isset( $atts['attachments'] ) ) {
612
+ $attachments = $atts['attachments'];
613
+ }
614
 
615
+ if ( ! is_array( $attachments ) ) {
616
+ $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
617
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
618
 
619
+ $options = gmail_smtp_get_option();
 
620
 
621
+ $phpmailer = new PHPMailerOAuth; /* this must be the custom class we created */
 
622
 
623
+ // Tell PHPMailer to use SMTP
624
+ $phpmailer->isSMTP();
625
 
626
+ // Set AuthType
627
+ $phpmailer->AuthType = 'XOAUTH2';
628
+
629
+ // Whether to use SMTP authentication
630
+ $phpmailer->SMTPAuth = true;
631
+
632
+ // Set the encryption system to use - ssl (deprecated) or tls
633
+ $phpmailer->SMTPSecure = $options['type_of_encryption'];
634
+
635
+ // Set the hostname of the mail server
636
+ $phpmailer->Host = 'smtp.gmail.com';
637
+
638
+ // Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
639
+ $phpmailer->Port = $options['smtp_port'];
640
+
641
+ $phpmailer->SMTPAutoTLS = false;
642
+
643
+ //enable debug when sending a test mail
644
+ if(isset($_POST['gmail_smtp_send_test_email'])){
645
+ $phpmailer->SMTPDebug = 4;
646
+ // Ask for HTML-friendly debug output
647
+ $phpmailer->Debugoutput = 'html';
648
+ }
649
+
650
+ //disable ssl certificate verification if checked
651
+ if(isset($options['disable_ssl_verification']) && !empty($options['disable_ssl_verification'])){
652
+ $phpmailer->SMTPOptions = array(
653
+ 'ssl' => array(
654
+ 'verify_peer' => false,
655
+ 'verify_peer_name' => false,
656
+ 'allow_self_signed' => true
657
+ )
658
+ );
659
+ }
660
+ // User Email to use for SMTP authentication - Use the same Email used in Google Developer Console
661
+ $phpmailer->oauthUserEmail = $options['oauth_user_email'];
662
+
663
+ //Obtained From Google Developer Console
664
+ $phpmailer->oauthClientId = $options['oauth_client_id'];
665
+
666
+ //Obtained From Google Developer Console
667
+ $phpmailer->oauthClientSecret = $options['oauth_client_secret'];
668
+
669
+ $gmail_token = json_decode($options['oauth_access_token'], true);
670
+
671
+ //Obtained By running get_oauth_token.php after setting up APP in Google Developer Console.
672
+ //Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
673
+ // eg: http://localhost/phpmail/get_oauth_token.php
674
+ $phpmailer->oauthRefreshToken = $gmail_token['refresh_token'];
675
+
676
+ // Headers
677
+ $cc = $bcc = $reply_to = array();
678
+
679
+ if ( empty( $headers ) ) {
680
+ $headers = array();
681
+ } else {
682
+ if ( !is_array( $headers ) ) {
683
+ // Explode the headers out, so this function can take both
684
+ // string headers and an array of headers.
685
+ $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
686
+ } else {
687
+ $tempheaders = $headers;
688
+ }
689
+ $headers = array();
690
+ $cc = array();
691
+ $bcc = array();
692
+
693
+ // If it's actually got contents
694
+ if ( !empty( $tempheaders ) ) {
695
+ // Iterate through the raw headers
696
+ foreach ( (array) $tempheaders as $header ) {
697
+ if ( strpos($header, ':') === false ) {
698
+ if ( false !== stripos( $header, 'boundary=' ) ) {
699
+ $parts = preg_split('/boundary=/i', trim( $header ) );
700
+ $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
701
+ }
702
+ continue;
703
+ }
704
+ // Explode them out
705
+ list( $name, $content ) = explode( ':', trim( $header ), 2 );
706
+
707
+ // Cleanup crew
708
+ $name = trim( $name );
709
+ $content = trim( $content );
710
+
711
+ switch ( strtolower( $name ) ) {
712
+ // Mainly for legacy -- process a From: header if it's there
713
+ case 'from':
714
+ $bracket_pos = strpos( $content, '<' );
715
+ if ( $bracket_pos !== false ) {
716
+ // Text before the bracketed email is the "From" name.
717
+ if ( $bracket_pos > 0 ) {
718
+ $from_name = substr( $content, 0, $bracket_pos - 1 );
719
+ $from_name = str_replace( '"', '', $from_name );
720
+ $from_name = trim( $from_name );
721
+ }
722
+
723
+ $from_email = substr( $content, $bracket_pos + 1 );
724
+ $from_email = str_replace( '>', '', $from_email );
725
+ $from_email = trim( $from_email );
726
+
727
+ // Avoid setting an empty $from_email.
728
+ } elseif ( '' !== trim( $content ) ) {
729
+ $from_email = trim( $content );
730
+ }
731
+ break;
732
+ case 'content-type':
733
+ if ( strpos( $content, ';' ) !== false ) {
734
+ list( $type, $charset_content ) = explode( ';', $content );
735
+ $content_type = trim( $type );
736
+ if ( false !== stripos( $charset_content, 'charset=' ) ) {
737
+ $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) );
738
+ } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) {
739
+ $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) );
740
+ $charset = '';
741
+ }
742
+
743
+ // Avoid setting an empty $content_type.
744
+ } elseif ( '' !== trim( $content ) ) {
745
+ $content_type = trim( $content );
746
+ }
747
+ break;
748
+ case 'cc':
749
+ $cc = array_merge( (array) $cc, explode( ',', $content ) );
750
+ break;
751
+ case 'bcc':
752
+ $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
753
+ break;
754
+ default:
755
+ // Add it to our grand headers array
756
+ $headers[trim( $name )] = trim( $content );
757
+ break;
758
+ }
759
+ }
760
+ }
761
+ }
762
+
763
+ // Empty out the values that may be set
764
+ $phpmailer->ClearAllRecipients();
765
+ $phpmailer->ClearAttachments();
766
+ $phpmailer->ClearCustomHeaders();
767
+ $phpmailer->ClearReplyTos();
768
+
769
+ // From email and name
770
+ // If we don't have a name from the input headers
771
+ if ( !isset( $from_name ) ){
772
+ $from_name = $options['from_name'];//'WordPress';
773
+ }
774
+ /* If we don't have an email from the input headers default to wordpress@$sitename
775
+ * Some hosts will block outgoing mail from this address if it doesn't exist but
776
+ * there's no easy alternative. Defaulting to admin_email might appear to be another
777
+ * option but some hosts may refuse to relay mail from an unknown domain. See
778
+ * https://core.trac.wordpress.org/ticket/5007.
779
+ */
780
+
781
+ if ( !isset( $from_email ) ) {
782
+ // Get the site domain and get rid of www.
783
+ $sitename = strtolower( $_SERVER['SERVER_NAME'] );
784
+ if ( substr( $sitename, 0, 4 ) == 'www.' ) {
785
+ $sitename = substr( $sitename, 4 );
786
+ }
787
+
788
+ $from_email = $options['from_email'];//'wordpress@' . $sitename;
789
+ }
790
+
791
+ /**
792
+ * Filter the email address to send from.
793
+ *
794
+ * @since 2.2.0
795
+ *
796
+ * @param string $from_email Email address to send from.
797
+ */
798
+ $phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
799
+
800
+ /**
801
+ * Filter the name to associate with the "from" email address.
802
+ *
803
+ * @since 2.3.0
804
+ *
805
+ * @param string $from_name Name associated with the "from" email address.
806
+ */
807
+ $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
808
+
809
+ // Set destination addresses
810
+ if ( !is_array( $to ) )
811
+ $to = explode( ',', $to );
812
+
813
+ foreach ( (array) $to as $recipient ) {
814
+ try {
815
+ // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
816
+ $recipient_name = '';
817
+ if ( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
818
+ if ( count( $matches ) == 3 ) {
819
+ $recipient_name = $matches[1];
820
+ $recipient = $matches[2];
821
+ }
822
+ }
823
+ $phpmailer->AddAddress( $recipient, $recipient_name);
824
+ } catch ( phpmailerException $e ) {
825
+ continue;
826
+ }
827
+ }
828
+
829
+ // Set mail's subject and body
830
+ $phpmailer->Subject = $subject;
831
+ $phpmailer->Body = $message;
832
+
833
+ // Add any CC and BCC recipients
834
+ if ( !empty( $cc ) ) {
835
+ foreach ( (array) $cc as $recipient ) {
836
+ try {
837
+ // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
838
+ $recipient_name = '';
839
+ if ( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
840
+ if ( count( $matches ) == 3 ) {
841
+ $recipient_name = $matches[1];
842
+ $recipient = $matches[2];
843
+ }
844
+ }
845
+ $phpmailer->AddCc( $recipient, $recipient_name );
846
+ } catch ( phpmailerException $e ) {
847
+ continue;
848
+ }
849
+ }
850
+ }
851
+
852
+ if ( !empty( $bcc ) ) {
853
+ foreach ( (array) $bcc as $recipient) {
854
+ try {
855
+ // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
856
+ $recipient_name = '';
857
+ if ( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
858
+ if ( count( $matches ) == 3 ) {
859
+ $recipient_name = $matches[1];
860
+ $recipient = $matches[2];
861
+ }
862
+ }
863
+ $phpmailer->AddBcc( $recipient, $recipient_name );
864
+ } catch ( phpmailerException $e ) {
865
+ continue;
866
+ }
867
+ }
868
+ }
869
+
870
+ // Set Content-Type and charset
871
+ // If we don't have a content-type from the input headers
872
+ if ( !isset( $content_type ) )
873
+ $content_type = 'text/plain';
874
+
875
+ /**
876
+ * Filter the wp_mail() content type.
877
+ *
878
+ * @since 2.3.0
879
+ *
880
+ * @param string $content_type Default wp_mail() content type.
881
+ */
882
+ $content_type = apply_filters( 'wp_mail_content_type', $content_type );
883
+
884
+ $phpmailer->ContentType = $content_type;
885
+
886
+ // Set whether it's plaintext, depending on $content_type
887
+ if ( 'text/html' == $content_type )
888
+ $phpmailer->IsHTML( true );
889
+
890
+ // If we don't have a charset from the input headers
891
+ if ( !isset( $charset ) )
892
+ $charset = get_bloginfo( 'charset' );
893
+
894
+ // Set the content-type and charset
895
+
896
+ /**
897
+ * Filter the default wp_mail() charset.
898
+ *
899
+ * @since 2.3.0
900
+ *
901
+ * @param string $charset Default email charset.
902
+ */
903
+ $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
904
+
905
+ // Set custom headers
906
+ if ( !empty( $headers ) ) {
907
+ foreach( (array) $headers as $name => $content ) {
908
+ $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
909
+ }
910
+
911
+ if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
912
+ $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
913
+ }
914
+
915
+ if ( !empty( $attachments ) ) {
916
+ foreach ( $attachments as $attachment ) {
917
+ try {
918
+ $phpmailer->AddAttachment($attachment);
919
+ } catch ( phpmailerException $e ) {
920
+ continue;
921
+ }
922
+ }
923
+ }
924
+
925
+ /**
926
+ * Fires after PHPMailer is initialized.
927
+ *
928
+ * @since 2.2.0
929
+ *
930
+ * @param PHPMailer &$phpmailer The PHPMailer instance, passed by reference.
931
+ */
932
+ //do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
933
+
934
+ // Send!
935
+ try {
936
+ return $phpmailer->Send();
937
+ } catch ( phpmailerException $e ) {
938
+
939
+ $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
940
+ $mail_error_data['phpmailer_exception_code'] = $e->getCode();
941
+
942
+ /**
943
+ * Fires after a phpmailerException is caught.
944
+ *
945
+ * @since 4.4.0
946
+ *
947
+ * @param WP_Error $error A WP_Error object with the phpmailerException message, and an array
948
+ * containing the mail recipient, subject, message, headers, and attachments.
949
+ */
950
+ do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) );
951
+
952
+ return false;
953
+ }
954
+ }
955
  }
readme.txt CHANGED
@@ -2,9 +2,9 @@
2
  Contributors: naa986
3
  Donate link: https://wphowto.net/
4
  Tags: smtp, gmail, mail, mailer, phpmailer, wp_mail, email, oauth2
5
- Requires at least: 4.3
6
  Tested up to: 4.7
7
- Stable tag: 1.0.7
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -102,6 +102,9 @@ none
102
 
103
  == Changelog ==
104
 
 
 
 
105
  = 1.0.7 =
106
  * Added more requirements to the Server Info tab to help with troubleshooting.
107
  * Compatible with WooCommerce email in HTML format.
2
  Contributors: naa986
3
  Donate link: https://wphowto.net/
4
  Tags: smtp, gmail, mail, mailer, phpmailer, wp_mail, email, oauth2
5
+ Requires at least: 4.7
6
  Tested up to: 4.7
7
+ Stable tag: 1.0.8
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
102
 
103
  == Changelog ==
104
 
105
+ = 1.0.8 =
106
+ * Gmail SMTP now supports the "wp_mail_failed" hook which fires after a phpmailerException is caught.
107
+
108
  = 1.0.7 =
109
  * Added more requirements to the Server Info tab to help with troubleshooting.
110
  * Compatible with WooCommerce email in HTML format.