Mail.php (4096B)
1 <?php 2 3 /** 4 * Class Mail 5 * 6 * Handles everything regarding mail-sending. 7 */ 8 class Mail 9 { 10 /** @var mixed variable to collect errors */ 11 private $error; 12 13 /** 14 * Try to send a mail by using PHP's native mail() function. 15 * Please note that not PHP itself will send a mail, it's just a wrapper for Linux's sendmail or other mail tools 16 * 17 * Good guideline on how to send mails natively with mail(): 18 * @see http://stackoverflow.com/a/24644450/1114320 19 * @see http://www.php.net/manual/en/function.mail.php 20 */ 21 public function sendMailWithNativeMailFunction() 22 { 23 // no code yet, so we just return something to make IDEs and code analyzer tools happy 24 return false; 25 } 26 27 /** 28 * Try to send a mail by using SwiftMailer. 29 * Make sure you have loaded SwiftMailer via Composer. 30 * 31 * @return bool 32 */ 33 public function sendMailWithSwiftMailer() 34 { 35 // no code yet, so we just return something to make IDEs and code analyzer tools happy 36 return false; 37 } 38 39 /** 40 * Try to send a mail by using PHPMailer. 41 * Make sure you have loaded PHPMailer via Composer. 42 * Depending on your EMAIL_USE_SMTP setting this will work via SMTP credentials or via native mail() 43 * 44 * @param $user_email 45 * @param $from_email 46 * @param $from_name 47 * @param $subject 48 * @param $body 49 * 50 * @return bool 51 * @throws Exception 52 * @throws phpmailerException 53 */ 54 public function sendMailWithPHPMailer($user_email, $from_email, $from_name, $subject, $body) 55 { 56 $mail = new PHPMailer; 57 58 // if you want to send mail via PHPMailer using SMTP credentials 59 if (Config::get('EMAIL_USE_SMTP')) { 60 // set PHPMailer to use SMTP 61 $mail->IsSMTP(); 62 // 0 = off, 1 = commands, 2 = commands and data, perfect to see SMTP errors 63 $mail->SMTPDebug = 0; 64 // enable SMTP authentication 65 $mail->SMTPAuth = Config::get('EMAIL_SMTP_AUTH'); 66 // encryption 67 if (Config::get('EMAIL_SMTP_ENCRYPTION')) { 68 $mail->SMTPSecure = Config::get('EMAIL_SMTP_ENCRYPTION'); 69 } 70 // set SMTP provider's credentials 71 $mail->Host = Config::get('EMAIL_SMTP_HOST'); 72 $mail->Username = Config::get('EMAIL_SMTP_USERNAME'); 73 $mail->Password = Config::get('EMAIL_SMTP_PASSWORD'); 74 $mail->Port = Config::get('EMAIL_SMTP_PORT'); 75 } else { 76 $mail->IsMail(); 77 } 78 79 // fill mail with data 80 $mail->From = $from_email; 81 $mail->FromName = $from_name; 82 $mail->AddAddress($user_email); 83 $mail->Subject = $subject; 84 $mail->Body = $body; 85 86 // try to send mail, put result status (true/false into $wasSendingSuccessful) 87 // I'm unsure if mail->send really returns true or false every time, tis method in PHPMailer is quite complex 88 $wasSendingSuccessful = $mail->Send(); 89 90 if ($wasSendingSuccessful) { 91 return true; 92 } else { 93 // if not successful, copy errors into Mail's error property 94 $this->error = $mail->ErrorInfo; 95 return false; 96 } 97 } 98 99 /** 100 * The main mail sending method, this simply calls a certain mail sending method depending on which mail provider 101 * you've selected in the application's config. 102 * 103 * @param $user_email string email 104 * @param $from_email string sender's email 105 * @param $from_name string sender's name 106 * @param $subject string subject 107 * @param $body string full mail body text 108 * @return bool the success status of the according mail sending method 109 */ 110 public function sendMail($user_email, $from_email, $from_name, $subject, $body) 111 { 112 if (Config::get('EMAIL_USED_MAILER') == "phpmailer") { 113 // returns true if successful, false if not 114 return $this->sendMailWithPHPMailer( 115 $user_email, $from_email, $from_name, $subject, $body 116 ); 117 } 118 119 if (Config::get('EMAIL_USED_MAILER') == "swiftmailer") { 120 return $this->sendMailWithSwiftMailer(); 121 } 122 123 if (Config::get('EMAIL_USED_MAILER') == "native") { 124 return $this->sendMailWithNativeMailFunction(); 125 } 126 } 127 128 /** 129 * The different mail sending methods write errors to the error property $this->error, 130 * this method simply returns this error / error array. 131 * 132 * @return mixed 133 */ 134 public function getError() 135 { 136 return $this->error; 137 } 138 }