matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 1 | <?php |
| 2 | require_once "Mail.php"; |
| 3 | include('Mail/mime.php'); |
| 4 | include 'config.php'; |
Marc Kupietz | ce67631 | 2023-03-10 08:29:41 +0100 | [diff] [blame] | 5 | include_once 'User.php'; |
| 6 | use \User as User; |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 7 | |
Marc Kupietz | ce67631 | 2023-03-10 08:29:41 +0100 | [diff] [blame] | 8 | function send_mail(string $email, object $smtp, object $message, User $user = null) { |
Marc Kupietz | efd7cc2 | 2023-03-04 19:55:56 +0100 | [diff] [blame] | 9 | include 'config.php'; |
| 10 | |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 11 | $crlf = "\r\n"; |
| 12 | |
| 13 | $headers = array( |
| 14 | 'From' => $smtp->from, |
| 15 | 'To' => $email, |
| 16 | 'Subject' => $message->subject |
| 17 | ); |
| 18 | |
Marc Kupietz | efd7cc2 | 2023-03-04 19:55:56 +0100 | [diff] [blame] | 19 | if (isset($CC_MAIL)) { |
| 20 | $headers['Cc'] = $CC_MAIL; |
| 21 | $email .= ', ' . $CC_MAIL; |
| 22 | } |
| 23 | |
| 24 | if (isset($BCC_MAIL)) { |
| 25 | $email .= ', ' . $BCC_MAIL; |
| 26 | } |
| 27 | |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 28 | $smtp = Mail::factory('smtp', array( |
| 29 | 'host' => $smtp->host, |
| 30 | 'port' => $smtp->port, |
| 31 | 'auth' => true, |
| 32 | 'username' => $smtp->username, //your gmail account |
| 33 | 'password' => $smtp->password // your password |
| 34 | )); |
| 35 | |
| 36 | // Creating the Mime message |
Marc Kupietz | 0aba836 | 2023-03-09 20:39:47 +0100 | [diff] [blame] | 37 | $mime = new Mail_mime(array( |
| 38 | "text_charset" => "utf-8", |
| 39 | "html_charset" => "utf-8", |
| 40 | "eol" => $crlf |
| 41 | )); |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 42 | |
| 43 | // Setting the body of the email |
| 44 | $mime->setTXTBody($message->text); |
| 45 | $mime->setHTMLBody($message->html); |
Marc Kupietz | ce67631 | 2023-03-10 08:29:41 +0100 | [diff] [blame] | 46 | if ($user && $user->invoice) { |
Marc Kupietz | 4de9f68 | 2023-03-10 13:54:21 +0100 | [diff] [blame^] | 47 | $mime->addAttachment ( $user->invoice , 'application/pdf' , "invoice-".$SERVICE_ACRONYM."-" . $user->id . ".pdf" , false, "base64" ); |
Marc Kupietz | ce67631 | 2023-03-10 08:29:41 +0100 | [diff] [blame] | 48 | } |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 49 | |
| 50 | $body = $mime->get(); |
| 51 | $headers = $mime->headers($headers); |
| 52 | |
| 53 | // Send the mail |
| 54 | $mail = $smtp->send($email, $headers, $body); |
| 55 | |
| 56 | //check mail sent or not |
| 57 | if (PEAR::isError($mail)) { |
| 58 | return false; |
| 59 | } else { |
| 60 | return true; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /* send_mail("mattf@tilde.club", $SMTP, (object) [ */ |
| 65 | /* "subject" => "Please confirm your email", */ |
| 66 | /* "text" => "Plain tet", */ |
| 67 | /* "html" => "<html><body><p>HTML message</p><h2>This is not mere text</h2></body></html>" */ |
| 68 | /* ]) */ |
| 69 | ?> |