@@ -744,50 +744,35 @@ public function send($recipients) {
744744 }
745745
746746 if ($ this ->use_smtp == true ) {
747- if (!$ this ->_logged_in || !$ this ->_smtp_conn ) {
748- $ result = $ this ->_smtp_login ();
749- if (!$ result ) return false ;
750- }
751747 $ bcc_cc_sent = false ;
752748 foreach ($ recipients as $ recipname => $ recip ) {
753- if ($ this ->_sent_mails >= $ this ->smtp_max_mails ) {
754- // close connection to smtp and reconnect
755- $ this ->_sent_mails = 0 ;
756- $ this ->_smtp_close ();
757- $ result = $ this ->_smtp_login ();
758- if (!$ result ) return false ;
759- }
760- $ this ->_sent_mails += 1 ;
749+ // Build mail headers, content etc.
750+
751+ $ m_recipients = array ();
752+ $ m_mail_content = '' ;
761753
762754 $ recipname = trim (str_replace ('" ' , '' , $ recipname ));
763755 $ recip = $ this ->_encodeHeader ($ recip , $ this ->mail_charset );
764756 $ recipname = $ this ->_encodeHeader ($ recipname , $ this ->mail_charset );
765757
766758 //Email From
767- fputs ($ this ->_smtp_conn , 'MAIL FROM: < ' . $ this ->_mail_sender . '> ' . $ this ->_crlf );
768- $ response = fgets ($ this ->_smtp_conn , 515 );
759+ $ m_from = $ this ->_mail_sender ;
769760
770761 //Email To
771- fputs ($ this ->_smtp_conn , 'RCPT TO: < ' . $ recip . '> ' . $ this ->_crlf );
772- $ response = fgets ($ this ->_smtp_conn , 515 );
762+ $ m_recipients [] = $ recip ;
773763
774764 if ($ bcc_cc_sent == false ) {
775765 $ add_recips = array ();
776766 if ($ this ->getHeader ('Cc ' ) != '' ) $ add_recips = array_merge ($ add_recips , $ this ->_extract_names ($ this ->getHeader ('Cc ' )));
777767 if ($ this ->getHeader ('Bcc ' ) != '' ) $ add_recips = array_merge ($ add_recips , $ this ->_extract_names ($ this ->getHeader ('Bcc ' )));
778768 foreach ($ add_recips as $ add_recip ) {
779769 if (!$ add_recip ['mail ' ]) continue ;
780- fputs ($ this ->_smtp_conn , 'RCPT TO: < ' . $ this ->_encodeHeader ($ add_recip ['mail ' ], $ this ->mail_charset ) . '> ' . $ this ->_crlf );
781- $ response = fgets ($ this ->_smtp_conn , 515 );
770+ $ m_recipients [] = $ this ->_encodeHeader ($ add_recip ['mail ' ], $ this ->mail_charset );
782771 }
783772 unset($ add_recips );
784773 $ bcc_cc_sent = true ;
785774 }
786775
787- //The Email
788- fputs ($ this ->_smtp_conn , 'DATA ' . $ this ->_crlf );
789- $ response = fgets ($ this ->_smtp_conn , 515 );
790-
791776 //Construct Headers
792777 if ($ recipname && !is_numeric ($ recipname )) $ this ->setHeader ('To ' , $ recipname . ' < ' . $ recip . '> ' );
793778 else $ this ->setHeader ('To ' , $ recip );
@@ -797,10 +782,32 @@ public function send($recipients) {
797782 if ($ this ->getHeader ('Cc ' ) != '' ) $ mail_content .= 'Cc: ' . $ this ->_encodeHeader ($ this ->getHeader ('Cc ' ), $ this ->mail_charset ) . $ this ->_crlf ;
798783 $ mail_content .= implode ($ this ->_crlf , $ headers ) . $ this ->_crlf . ($ this ->_is_signed == false ? $ this ->_crlf : '' ) . $ this ->body ;
799784
800- fputs ($ this ->_smtp_conn , $ mail_content . $ this ->_crlf . '. ' . $ this ->_crlf );
801- $ response = fgets ($ this ->_smtp_conn , 515 );
785+ $ m_mail_content = $ mail_content ;
786+
787+ // Attempt SMTP login:
788+
789+ if (!$ this ->_logged_in || !$ this ->_smtp_conn ) {
790+ $ result = $ this ->_smtp_login ();
791+ }
792+
793+ if ($ this ->_sent_mails >= $ this ->smtp_max_mails ) {
794+ // close connection to smtp and reconnect
795+ $ this ->_sent_mails = 0 ;
796+ $ this ->_smtp_close ();
797+ $ result = $ this ->_smtp_login ();
798+ }
799+ $ this ->_sent_mails += 1 ;
800+
801+ // Send mail or queue it
802+
803+ if ($ result ) {
804+ $ this ->send_smtp ($ m_from , $ m_recipients , $ m_mail_content );
805+ } else {
806+ $ this ->add_to_queue ($ m_from , $ m_recipients , $ m_mail_content );
807+ }
808+
809+ // hopefully message was correctly sent or queued now
802810
803- // hopefully message was correctly sent now
804811 $ result = true ;
805812 }
806813 } else {
@@ -828,7 +835,87 @@ public function send($recipients) {
828835 return $ result ;
829836 }
830837
838+ /**
839+ * Send all mails in queue (usually called from cron)
840+ */
841+ public function send_queue () {
842+ global $ app ;
843+
844+ $ mails = $ app ->db ->queryAllRecords ('SELECT * FROM sys_mailqueue ' );
845+
846+ if (is_array ($ mails )) {
847+ foreach ($ mails as $ mail ) {
848+ // Open SMTP connections if not open:
849+
850+ if (!$ this ->_logged_in || !$ this ->_smtp_conn ) {
851+ $ result = $ this ->_smtp_login ();
852+ }
853+
854+ if ($ this ->_sent_mails >= $ this ->smtp_max_mails ) {
855+ // close connection to smtp and reconnect
856+ $ this ->_sent_mails = 0 ;
857+ $ this ->_smtp_close ();
858+ $ result = $ this ->_smtp_login ();
859+ }
860+ $ this ->_sent_mails += 1 ;
861+
862+ if (!$ result ) {
863+ return false ;
864+ }
865+
866+ // Send mail:
867+
868+ $ id = $ mail ['id ' ];
869+ $ from = $ mail ['from ' ];
870+ $ recipients = explode ("\n" , $ mail ['recipients ' ]);
871+ $ mail_content = $ mail ['mail_content ' ];
872+
873+ $ this ->send_smtp ($ from , $ recipients , $ mail_content );
874+
875+ // Delete from queue:
876+
877+ $ app ->db ->query ('DELETE FROM sys_mailqueue WHERE id = ? ' , $ id );
878+ }
879+ }
880+ }
881+
882+ /**
883+ * Send mail via SMTP
884+ */
885+ private function send_smtp ($ from , $ recipients , $ mail_content ) {
886+ // from:
887+
888+ fputs ($ this ->_smtp_conn , 'MAIL FROM: < ' . $ from . '> ' . $ this ->_crlf );
889+ $ response = fgets ($ this ->_smtp_conn , 515 );
890+
891+ // recipients (To, Cc or Bcc):
892+
893+ foreach ($ recipients as $ recipient ) {
894+ fputs ($ this ->_smtp_conn , 'RCPT TO: < ' . $ recipient . '> ' . $ this ->_crlf );
895+ $ response = fgets ($ this ->_smtp_conn , 515 );
896+ }
897+
898+ // mail content:
831899
900+ fputs ($ this ->_smtp_conn , 'DATA ' . $ this ->_crlf );
901+ $ response = fgets ($ this ->_smtp_conn , 515 );
902+
903+ fputs ($ this ->_smtp_conn , $ mail_content . $ this ->_crlf . '. ' . $ this ->_crlf );
904+ $ response = fgets ($ this ->_smtp_conn , 515 );
905+
906+ // hopefully message was correctly sent now
907+ $ result = true ;
908+
909+ return $ result ;
910+ }
911+
912+ /**
913+ * Add mail to queue for sending later
914+ */
915+ private function add_to_queue ($ from , $ recipients , $ mail_content ) {
916+ global $ app ;
917+ $ app ->db ->query ('INSERT INTO `sys_mailqueue` (`from`, `recipients`, `mail_content`) VALUES (?,?,?) ' , $ from , implode ("\n" , $ recipients ), $ mail_content );
918+ }
832919
833920 /**
834921 * Close mail connections
0 commit comments