blob: 0564237716572c487c0f23c88638e56bcaa29883 [file] [log] [blame]
matheusfillipeabd513e2021-05-11 03:29:11 -03001<?php
2require_once "Mail.php";
3include('Mail/mime.php');
4include 'config.php';
Marc Kupietzce676312023-03-10 08:29:41 +01005include_once 'User.php';
6use \User as User;
matheusfillipeabd513e2021-05-11 03:29:11 -03007
Marc Kupietzce676312023-03-10 08:29:41 +01008function send_mail(string $email, object $smtp, object $message, User $user = null) {
Marc Kupietzefd7cc22023-03-04 19:55:56 +01009 include 'config.php';
10
matheusfillipeabd513e2021-05-11 03:29:11 -030011 $crlf = "\r\n";
12
13 $headers = array(
14 'From' => $smtp->from,
15 'To' => $email,
16 'Subject' => $message->subject
17 );
18
Marc Kupietzefd7cc22023-03-04 19:55:56 +010019 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
matheusfillipeabd513e2021-05-11 03:29:11 -030028 $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 Kupietz0aba8362023-03-09 20:39:47 +010037 $mime = new Mail_mime(array(
38 "text_charset" => "utf-8",
39 "html_charset" => "utf-8",
40 "eol" => $crlf
41 ));
matheusfillipeabd513e2021-05-11 03:29:11 -030042
43 // Setting the body of the email
44 $mime->setTXTBody($message->text);
45 $mime->setHTMLBody($message->html);
Marc Kupietzce676312023-03-10 08:29:41 +010046 if ($user && $user->invoice) {
Marc Kupietz4de9f682023-03-10 13:54:21 +010047 $mime->addAttachment ( $user->invoice , 'application/pdf' , "invoice-".$SERVICE_ACRONYM."-" . $user->id . ".pdf" , false, "base64" );
Marc Kupietzce676312023-03-10 08:29:41 +010048 }
matheusfillipeabd513e2021-05-11 03:29:11 -030049
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?>