<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

function websiteSMTP($fromMail, $fromName, $replyMail, $mailTo, $mailToName, $mailCC, $mailBCC, $subject="No Subject", $body="", $attachFiles="", $debug="0", $smtpData, $useHTML="Y"){
	date_default_timezone_set('Asia/Seoul');

	// Load Composer's autoloader
	require $_SERVER["DOCUMENT_ROOT"].'/common/lib/PHPMailer/vendor/autoload.php';

	// Instantiation and passing `true` enables exceptions
	$mail = new PHPMailer(true);

	try {
		//Server settings
		if($debug=="1"){
			$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
		}else if($debug=="2"){
			$mail->SMTPDebug = 4;
		}
		//$mail->SMTPDebug = 4;
		$mail->isSMTP();                                            // Send using SMTP
		//$mail->Host       = 'smtp.daum.net';                    // Set the SMTP server to send through
		//$mail->Username   = 'daum@daum.net';                     // SMTP username
		//$mail->Password   = 'daum@daum.net password';                               // SMTP password
		//$mail->Port       = 587;                                    // TCP port to connect to
		$mail->Host       = $smtpData["HOST"];                    // Set the SMTP server to send through
		$mail->Username   = $smtpData["USERNAME"];                     // SMTP username
		$mail->Password   = $smtpData["PASSWORD"];                               // SMTP password
		$mail->Port       = $smtpData["PORT"];                                    // TCP port to connect to
		$mail->SMTPAuth   = true;                                   // Enable SMTP authentication
		if($smtpData["SSL"]=="Y"){
			$mail->SMTPSecure = "ssl";         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
		}else{
			$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
		}

		$mail->setLanguage('ko', $_SERVER["DOCUMENT_ROOT"].'/common/lib/PHPMailer/vendor/phpmailer/phpmailer/language/');
		$mail->CharSet = "UTF-8";


		//$mail->addAddress('ellen@example.com');               // Name is optional
		//$mail->addReplyTo('info@example.com', 'Information');
		//$mail->addCC('phpclub@hanmail.net');
		//$mail->addBCC('bcc@example.com');

		$mail->setFrom($fromMail, '"'.$fromName.'"');
		
		//받는사람 (메일주소)
		/*
		$arrMailTo = preg_split("/[,;]/",$mailTo);
		foreach($arrMailTo AS $key=>$val){
			$mail->addAddress(trim($val));     // Add a recipient
		}
		*/
		//받는 사람은 한명만
		$mail->addAddress(trim($mailTo), '"'.$mailToName.'"');     // Add a recipient

		//회신 메일주소
		$mail->addReplyTo($replyMail);

		//받는사람 CC (메일주소)
		if($mailCC){
			$arrCC = preg_split("/[,;]/", $mailCC);
			foreach($arrCC AS $key=>$val){
				$mail->addCC(trim($val));     // Add a recipient
			}
		}

		//받는사람 BCC (메일주소)
		if($mailBCC){
			$arrBCC = preg_split("/[,;]/", $mailBCC);
			foreach($arrBCC AS $key=>$val){
				$mail->addBCC(trim($val));     // Add a recipient
			}
		}

		//첨부파일
		// Attachments array(array('/src/filename', '첨부파일명'),array('/src/filename', '첨부파일명'))
		if($attachFiles){
			foreach($attachFiles AS $src=>$name){
				$mail->addAttachment($name[0], $name[1]);    // Optional name
			}
		}

		// Content
		if($useHTML=="Y"){
			$mail->isHTML(true);                                  // Set email format to HTML
		}else{
			$mail->isHTML(false);                                  // Set email format to TEXT
			$body = str_replace("\\","",$body);
			$body = htmlspecialchars($body);
			$body = nl2br($body);
		}
		$mail->Subject = $subject;
		$mail->Body    = $body;
		//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

		//Send the message, check for errors
		if ($mail->send()) {
			//echo 'Mailer Error: ' . $mail->ErrorInfo;
			return true;
		} else {
			return false;
			//return $mail->ErrorInfo;
		}
	} catch (Exception $e) {
		return false;
		//return $mail->ErrorInfo;
		//echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
	}
}
?>
