1 | <?php |
||
2 | |||
3 | require_once dirname(__FILE__) . '/config.php'; |
||
4 | require_once dirname(__FILE__) . '/base.php'; |
||
5 | |||
6 | function checkConfiguration () { |
||
7 | 5 | global $config; |
|
8 | |||
9 | 5 | if (is_null ($config['cops_mail_configuration']) || |
|
10 | 4 | !is_array ($config['cops_mail_configuration']) || |
|
11 | 3 | empty ($config['cops_mail_configuration']["smtp.host"]) || |
|
12 | 5 | empty ($config['cops_mail_configuration']["address.from"])) { |
|
13 | 4 | return "NOK. bad configuration."; |
|
14 | } |
||
15 | 1 | return False; |
|
16 | } |
||
17 | |||
18 | function checkRequest ($idData, $emailDest) { |
||
19 | 3 | if (empty ($idData)) { |
|
20 | 1 | return 'No data sent.'; |
|
21 | } |
||
22 | 2 | if (empty ($emailDest)) { |
|
23 | 1 | return 'No email sent.'; |
|
24 | } |
||
25 | 1 | return False; |
|
26 | } |
||
27 | |||
28 | if (php_sapi_name() === 'cli') { return; } |
||
29 | |||
30 | global $config; |
||
31 | |||
32 | if ($error = checkConfiguration ()) { |
||
33 | echo $error; |
||
34 | exit; |
||
35 | } |
||
36 | |||
37 | $idData = $_REQUEST["data"]; |
||
38 | $emailDest = $_REQUEST["email"]; |
||
39 | if ($error = checkRequest ($idData, $emailDest)) { |
||
40 | echo $error; |
||
41 | exit; |
||
42 | } |
||
43 | |||
44 | $book = Book::getBookByDataId($idData); |
||
45 | $data = $book->getDataById ($idData); |
||
46 | |||
47 | if (filesize ($data->getLocalPath ()) > 10 * 1024 * 1024) { |
||
48 | echo 'Attachment too big'; |
||
49 | exit; |
||
50 | } |
||
51 | |||
52 | $mail = new PHPMailer; |
||
53 | |||
54 | $mail->IsSMTP(); |
||
55 | $mail->Timeout = 30; // 30 seconds as some files can be big |
||
56 | $mail->Host = $config['cops_mail_configuration']["smtp.host"]; |
||
57 | if (!empty ($config['cops_mail_configuration']["smtp.secure"])) { |
||
58 | $mail->SMTPSecure = $config['cops_mail_configuration']["smtp.secure"]; |
||
59 | $mail->Port = 465; |
||
60 | } |
||
61 | $mail->SMTPAuth = !empty ($config['cops_mail_configuration']["smtp.username"]); |
||
62 | if (!empty ($config['cops_mail_configuration']["smtp.username"])) $mail->Username = $config['cops_mail_configuration']["smtp.username"]; |
||
63 | if (!empty ($config['cops_mail_configuration']["smtp.password"])) $mail->Password = $config['cops_mail_configuration']["smtp.password"]; |
||
64 | if (!empty ($config['cops_mail_configuration']["smtp.secure"])) $mail->SMTPSecure = $config['cops_mail_configuration']["smtp.secure"]; |
||
65 | if (!empty ($config['cops_mail_configuration']["smtp.port"])) $mail->Port = $config['cops_mail_configuration']["smtp.port"]; |
||
66 | |||
67 | $mail->From = $config['cops_mail_configuration']["address.from"]; |
||
68 | $mail->FromName = $config['cops_title_default']; |
||
69 | |||
70 | foreach (explode (";", $emailDest) as $emailAddress) { |
||
71 | if (empty ($emailAddress)) { continue; } |
||
72 | $mail->AddAddress($emailAddress); |
||
73 | } |
||
74 | |||
75 | $mail->AddAttachment($data->getLocalPath ()); |
||
76 | |||
77 | $mail->IsHTML(true); |
||
78 | $mail->CharSet = "UTF-8"; |
||
79 | $mail->Subject = 'Sent by COPS : '; |
||
80 | if (!empty ($config['cops_mail_configuration']["subject"])) { |
||
81 | $mail->Subject = $config['cops_mail_configuration']["subject"]; |
||
82 | } |
||
83 | $mail->Subject .= $data->getUpdatedFilename (); |
||
84 | $mail->Body = "<h1>" . $book->title . "</h1><h2>" . $book->getAuthorsName () . "</h2>" . $book->getComment (); |
||
85 | $mail->AltBody = "Sent by COPS"; |
||
86 | |||
87 | if (!$mail->Send()) { |
||
88 | echo localize ("mail.messagenotsent"); |
||
89 | echo 'Mailer Error: ' . $mail->ErrorInfo; |
||
90 | exit; |
||
91 | } |
||
92 | |||
93 | echo localize ("mail.messagesent"); |
||
94 | |||
95 |