186 lines
6.6 KiB
PHP
186 lines
6.6 KiB
PHP
<?php
|
|
|
|
// use PHPMailer\PHPMailer\Exception;
|
|
// use PHPMailer\PHPMailer\PHPMailer;
|
|
|
|
// require 'phpmailer/src/Exception.php';
|
|
// require 'phpmailer/src/PHPMailer.php';
|
|
// require 'phpmailer/src/SMTP.php';
|
|
// $mail = new PHPMailer();
|
|
|
|
// $to = $_POST['to'];
|
|
// $judul = $_POST['judul'];
|
|
// $content = $_POST['content'];
|
|
|
|
// if (isset($_POST['from'])) {
|
|
// $from = $_POST['from'];
|
|
// $fromText = $from;
|
|
// } else {
|
|
// $from = 'system@spota.untan.ac.id';
|
|
// $fromText = 'SPOTA Informatika UNTAN';
|
|
// }
|
|
|
|
// $ch = curl_init();
|
|
// $emailParams = [];
|
|
// $emailParams['to'] = $to;
|
|
// $emailParams['judul'] = $judul;
|
|
// $emailParams['content'] = $content;
|
|
// $emailParams['from'] = $from;
|
|
|
|
// $postdata = $emailParams;
|
|
// curl_setopt($ch, CURLOPT_URL, 'https://informatika.untan.ac.id/API/sendMail.php');
|
|
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
|
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
|
// curl_setopt($ch, CURLOPT_POST, 1);
|
|
// curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
|
|
// curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
// curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
|
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
// curl_exec($ch);
|
|
|
|
// echo 'ok';
|
|
|
|
use PHPMailer\PHPMailer\Exception;
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
|
|
include '../conf/class.server.php';
|
|
include '../conf/function.php';
|
|
|
|
require 'phpmailer/src/Exception.php';
|
|
require 'phpmailer/src/PHPMailer.php';
|
|
require 'phpmailer/src/SMTP.php';
|
|
$mail = new PHPMailer();
|
|
|
|
header('Content-Type: application/json');
|
|
requireRobotSecret();
|
|
|
|
$to = isset($_POST['to']) ? $_POST['to'] : '[]';
|
|
$judul = isset($_POST['judul']) ? $_POST['judul'] : '';
|
|
$content = isset($_POST['content']) ? $_POST['content'] : '';
|
|
|
|
if ($judul === '' || $content === '') {
|
|
jsonResponse(['status' => 0, 'msg' => 'Parameter tidak lengkap'], 422);
|
|
}
|
|
|
|
if (isset($_POST['from'])) {
|
|
$from = $_POST['from'];
|
|
$fromText = $from;
|
|
} else {
|
|
$from = 'system@spota.untan.ac.id';
|
|
$fromText = 'SPOTA Informatika UNTAN';
|
|
}
|
|
|
|
try {
|
|
//Server settings
|
|
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
|
|
$mail->isSMTP(); // Send using SMTP
|
|
$mail->Host = (string) (getenv('SPOTA_SMTP_HOST') ?: 'smtp.gmail.com');
|
|
$mail->SMTPAuth = true;
|
|
$mail->SMTPDebug = 0;
|
|
//$mail->SMTPSecure = 'tls'; // Enable SMTP authentication
|
|
//gmail
|
|
// $mail->Username = 'mail.informatika.untan@gmail.com'; // SMTP username
|
|
// $mail->Password = 'informatikauntan247mail'; // SMTP password
|
|
$mail->Username = (string) (getenv('SPOTA_SMTP_USERNAME') ?: 'tu1@informatika.untan.ac.id');
|
|
$mail->Password = (string) (getenv('SPOTA_SMTP_PASSWORD') ?: 'IFuntanpnk-2020');
|
|
|
|
$mail->Port = (int) (getenv('SPOTA_SMTP_PORT') ?: 587);
|
|
|
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
|
|
|
|
// $mail->SMTPOptions = [
|
|
// 'ssl' => [
|
|
// 'verify_peer' => false,
|
|
// 'verify_peer_name' => false,
|
|
// 'allow_self_signed' => true,
|
|
// ],
|
|
// ];
|
|
//Recipients
|
|
$mail->setFrom($from, $fromText);
|
|
$toArr = json_decode($to, 1);
|
|
|
|
if (!is_array($toArr) || empty($toArr)) {
|
|
jsonResponse(['status' => 0, 'msg' => 'Penerima email tidak valid'], 422);
|
|
}
|
|
|
|
foreach ($toArr as $key => $val) {
|
|
$mail->addAddress(trim($val), $val); // Add a recipient
|
|
}
|
|
//$mail->addReplyTo('info@example.com', 'Information');
|
|
//$mail->addCC('cc@example.com');
|
|
//$mail->addBCC('bcc@example.com');
|
|
|
|
// Attachments
|
|
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
|
|
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
|
|
|
|
// Content
|
|
$mail->isHTML(true); // Set email format to HTML
|
|
$mail->Subject = $judul;
|
|
$mail->Body = $content;
|
|
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
|
|
|
|
if ($mail->send()) {
|
|
echo json_encode(['status' => 1, 'msg' => 'Message has been sent']);
|
|
} else {
|
|
echo json_encode(['status' => 0, 'msg' => 'Mailer Error: '.$mail->ErrorInfo]);
|
|
}
|
|
//;
|
|
} catch (Exception $e) {
|
|
echo json_encode(['status' => 0, 'msg' => "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"]);
|
|
}
|
|
|
|
// try {
|
|
// //Server settings
|
|
// //$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
|
|
// $mail->isSMTP(); // Send using SMTP
|
|
// $mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
|
|
// $mail->SMTPAuth = true;
|
|
// $mail->SMTPDebug = 4;
|
|
// //$mail->SMTPSecure = 'tls'; // Enable SMTP authentication
|
|
// //gmail
|
|
// $mail->Username = 'mail.informatika.untan@gmail.com'; // SMTP username
|
|
// $mail->Password = 'informatikauntan247mail'; // SMTP password
|
|
// $mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` abov
|
|
|
|
// $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
|
|
|
|
// // $mail->SMTPOptions = [
|
|
// // 'ssl' => [
|
|
// // 'verify_peer' => false,
|
|
// // 'verify_peer_name' => false,
|
|
// // 'allow_self_signed' => true,
|
|
// // ],
|
|
// // ];
|
|
// //Recipients
|
|
// $mail->setFrom($from, $fromText);
|
|
// $toArr = json_decode($to, 1);
|
|
|
|
// foreach ($toArr as $key => $val) {
|
|
// $mail->addAddress(trim($val), $val); // Add a recipient
|
|
// }
|
|
// //$mail->addReplyTo('info@example.com', 'Information');
|
|
// //$mail->addCC('cc@example.com');
|
|
// //$mail->addBCC('bcc@example.com');
|
|
|
|
// // Attachments
|
|
// //$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
|
|
// //$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
|
|
|
|
// // Content
|
|
// $mail->isHTML(true); // Set email format to HTML
|
|
// $mail->Subject = $judul;
|
|
// $mail->Body = $content;
|
|
// //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
|
|
|
|
// if ($mail->send()) {
|
|
// echo 'Message has been sent';
|
|
// } else {
|
|
// echo 'error message';
|
|
// echo 'Mailer Error: '.$mail->ErrorInfo;
|
|
// }
|
|
// //;
|
|
// } catch (Exception $e) {
|
|
// echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
|
|
// }
|