YaWK  24.1
Yet another WebKit
email-send.php
Go to the documentation of this file.
1 <!-- Bootstrap 3.3.5 -->
2 <link rel="stylesheet" href="../../system/engines/bootstrap3/dist/css/bootstrap.min.css">
3 <link rel="stylesheet" href="../../system/engines/font-awesome/css/font-awesome.min.css">
4 <!-- Animate CSS -->
5 <link rel="stylesheet" href="../../system/engines/animateCSS/animate.min.css">
6 <!-- jQuery 2.1.4 -->
7 <script type="text/javascript" src="../../system/engines/jquery/jquery-3.6.4.min.js"></script>
8 <script src="../../system/engines/bootstrap3/dist/js/bootstrap.min.js"></script>
9 
10 <?php
11 // include smtp mailer classes
12 require_once('../../system/engines/phpMailer/class.phpmailer.php');
13 require_once('../../system/engines/phpMailer/class.smtp.php');
14 require_once('../../system/classes/db.php');
15 require_once('../../system/classes/settings.php');
16 /* set database object */
17 if (!isset($db))
18 { // create new db object
19  $db = new \YAWK\db();
20 }
21 // get all webmail settings into array
23 
24 // create new smtp mail object
25 $mail = new PHPMailer(true);
26 // set smtp config
27 try
28 {
29  // Enable verbose debug output
30  $mail->SMTPDebug = 0;
31  // Set mailer to use SMTP
32  $mail->isSMTP();
33  //SMTP Host name
34  $mail->Host = $webmailSettings['webmail_smtp_server'];
35  // Auth required
36  $mail->SMTPAuth = true;
37  // SMTP Login
38  $mail->Username = $webmailSettings['webmail_smtp_username'];
39  // SMTP Password
40  $mail->Password = $webmailSettings['webmail_smtp_pwd'];
41  // Enable TLS encryption, `ssl` also accepted
42  $mail->SMTPSecure = $webmailSettings['webmail_smtp_encrypt'];
43  // TCP port to connect to
44  $mail->Port = $webmailSettings['webmail_smtp_port'];
45 
46  // check, if sender name is set
47  if (!isset($webmailSettings['webmail_smtp_sender']) || (empty($webmailSettings['webmail_smtp_sender'])))
48  { // no sender name, set smtp username (email) as sender instead
49  $webmailSettings['webmail_smtp_sender'] = $webmailSettings['webmail_smtp_username'];
50  }
51 
52  // from address
53  $mail->SetFrom($webmailSettings['webmail_smtp_username'], $webmailSettings['webmail_smtp_sender']);
54 
55  // check, if CC is set
56  if (isset($_POST['ccField']) && (!empty($_POST['ccField'])))
57  { // add CC recipients to email
58  $mail->addCC($_POST['ccField']);
59  }
60 
61  // check, if BCC is set
62  if (isset($_POST['bccField']) && (!empty($_POST['bccField'])))
63  { // add BCC recipients to email
64  $mail->addBCC($_POST['bccField']);
65  }
66 
67 
68  // to address
69  $mail->addAddress($_POST['to'], $_POST['to']); // Add a recipient
70 
71  // Attachments
72  // NO ATTACHMENT
73  if (!empty($_FILES))
74  {
75  if ($_FILES['files']['error'] == 4)
76  {
77  // files empty or upload error
78 
79  echo "<div class=\"row\">";
80  echo "<div class=\"col-md-2\"></div>";
81  echo "<div class=\"col-md-8 text-center\"><br><br><br><h2><i>...sending...</i><br><br><br><i class=\"fa fa-spinner fa-spin\"></i></h2></div>";
82  echo "<div class=\"col-md-2\"></div>";
83  echo "</div>";
84  }
85  else
86  {
87  $uploadDir = "../../media/mailbox/sent";
88  // PROCESS w/ attachments
89  foreach ($_FILES["files"]["tmp_name"] as $key => $error)
90  {
91  // echo "attachment $key<br>";
92  if ($error == UPLOAD_ERR_OK)
93  {
94  // temp upload filename
95  $tmp_name = $_FILES["files"]["tmp_name"][$key];
96  // original filename
97  $name = basename($_FILES["files"]["name"][$key]);
98  $newName = utf8_decode($name);
99  // add attachment
100  $mail->addAttachment($tmp_name, $newName);
101 
102  }
103  else
104  {
105  // echo "Unable to upload - errocode: ".$error;
106 
107  }
108  }
109  }
110  }
111  else
112  {
113  // no files attached...
114  echo "<div class=\"row\">";
115  echo "<div class=\"col-md-2\"></div>";
116  echo "<div class=\"col-md-8 text-center\"><br><br><br><h2><i class=\"fa fa-spinner fa-spin text-muted\"></i><br><br><br><small>...sending...</small><br><br><br></h2></div>";
117  echo "<div class=\"col-md-2\"></div>";
118  echo "</div>";
119  }
120 
121  // Content
122  // Set email format to HTML
123  $mail->isHTML(true);
124  // set subject
125  $mail->Subject = $_POST['subject'];
126  // set message body
127  $mail->Body = $_POST['body'];
128  // This is the body in plain text for non-HTML mail clients
129  $mail->AltBody = $_POST['body'];
130  // send email
131  $mail->send();
132 
133  $server = "{".$webmailSettings['webmail_imap_server'].":".$webmailSettings['webmail_imap_port']."/imap/".$webmailSettings['webmail_imap_encrypt']."}";
134  // open imap connection
135  $imapStream = imap_open($server, $mail->Username, $mail->Password);
136  // store email in folder + mark as seen
137  $result = imap_append($imapStream, $server.'Sent', $mail->getSentMIMEMessage(), "\\Seen");
138  // close imap connection
139  imap_close($imapStream);
140  // Message has been sent
141  echo "<script>window.location.replace('../index.php?page=webmail');</script>";
142 }
143 catch (Exception $e)
144  {
145  // Message could not be sent. Mailer Error: {$mail->ErrorInfo}
146  }
147 
148 
149 ?>
$name
Definition: add-comment.php:11
static getValueSettingsArray($db, $property)
Returns an array containing property as key and values corresponding to this property where p like $p...
Definition: settings.php:41
$result
Definition: email-send.php:137
$imapStream
Definition: email-send.php:135
if(!isset($db)) $webmailSettings
Definition: email-send.php:22
$mail
Definition: email-send.php:25
$server
Definition: email-send.php:133
type
Definition: menu-new.php:35