Merged in feature/PMC-1332 (pull request #7136)
PMC-1332 PMCORE-1018 Approved-by: Julio Cesar Laura Avendaño <contact@julio-laura.com> Approved-by: Paula Quispe <paula.quispe@processmaker.com>
This commit is contained in:
@@ -5,6 +5,7 @@ use AppMessage;
|
||||
use Bootstrap;
|
||||
use Exception;
|
||||
use G;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use ProcessMaker\Core\System;
|
||||
use SpoolRun;
|
||||
use TemplatePower;
|
||||
@@ -14,7 +15,7 @@ class EmailServer
|
||||
{
|
||||
private $arrayFieldDefinition = array(
|
||||
"MESS_UID" => array("type" => "string", "required" => false, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "emailServerUid"),
|
||||
"MESS_ENGINE" => array("type" => "string", "required" => true, "empty" => false, "defaultValues" => array("PHPMAILER", "MAIL", "IMAP"), "fieldNameAux" => "emailServerEngine"),
|
||||
"MESS_ENGINE" => array("type" => "string", "required" => true, "empty" => false, "defaultValues" => array("PHPMAILER", "MAIL", "IMAP", "GMAILAPI"), "fieldNameAux" => "emailServerEngine"),
|
||||
"MESS_SERVER" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "emailServerServer"),
|
||||
"MESS_PORT" => array("type" => "int", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "emailServerPort"),
|
||||
"MESS_INCOMING_SERVER" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "emailServerIncomingServer"),
|
||||
@@ -809,6 +810,13 @@ class EmailServer
|
||||
$arrayData["MESS_PASSWORD"] = G::encrypt($arrayData["MESS_PASSWORD"], "EMAILENCRYPT");
|
||||
}
|
||||
|
||||
$arrayData["OAUTH_CLIENT_ID"] = !empty($arrayData["OAUTH_CLIENT_ID"]) ?
|
||||
Crypt::encryptString($arrayData["OAUTH_CLIENT_ID"]) : "";
|
||||
$arrayData["OAUTH_CLIENT_SECRET"] = !empty($arrayData["OAUTH_CLIENT_SECRET"]) ?
|
||||
Crypt::encryptString($arrayData["OAUTH_CLIENT_SECRET"]) : "";
|
||||
$arrayData["OAUTH_REFRESH_TOKEN"] = !empty($arrayData["OAUTH_REFRESH_TOKEN"]) ?
|
||||
Crypt::encryptString($arrayData["OAUTH_REFRESH_TOKEN"]) : "";
|
||||
|
||||
$emailServer->fromArray($arrayData, \BasePeer::TYPE_FIELDNAME);
|
||||
|
||||
$emailServerUid = \ProcessMaker\Util\Common::generateUID();
|
||||
@@ -981,6 +989,13 @@ class EmailServer
|
||||
}
|
||||
}
|
||||
|
||||
$arrayData["OAUTH_CLIENT_ID"] = !empty($arrayData["OAUTH_CLIENT_ID"]) ?
|
||||
Crypt::encryptString($arrayData["OAUTH_CLIENT_ID"]) : "";
|
||||
$arrayData["OAUTH_CLIENT_SECRET"] = !empty($arrayData["OAUTH_CLIENT_SECRET"]) ?
|
||||
Crypt::encryptString($arrayData["OAUTH_CLIENT_SECRET"]) : "";
|
||||
$arrayData["OAUTH_REFRESH_TOKEN"] = !empty($arrayData["OAUTH_REFRESH_TOKEN"]) ?
|
||||
Crypt::encryptString($arrayData["OAUTH_REFRESH_TOKEN"]) : "";
|
||||
|
||||
$emailServer->fromArray($arrayData, \BasePeer::TYPE_FIELDNAME);
|
||||
|
||||
if ($emailServer->validate()) {
|
||||
@@ -1106,6 +1121,9 @@ class EmailServer
|
||||
$criteria->addSelectColumn(\EmailServerPeer::MESS_TRY_SEND_INMEDIATLY);
|
||||
$criteria->addSelectColumn(\EmailServerPeer::MAIL_TO);
|
||||
$criteria->addSelectColumn(\EmailServerPeer::MESS_DEFAULT);
|
||||
$criteria->addSelectColumn(\EmailServerPeer::OAUTH_CLIENT_ID);
|
||||
$criteria->addSelectColumn(\EmailServerPeer::OAUTH_CLIENT_SECRET);
|
||||
$criteria->addSelectColumn(\EmailServerPeer::OAUTH_REFRESH_TOKEN);
|
||||
|
||||
return $criteria;
|
||||
} catch (Exception $e) {
|
||||
@@ -1124,7 +1142,7 @@ class EmailServer
|
||||
public function getEmailServerDataFromRecord(array $record)
|
||||
{
|
||||
try {
|
||||
return array(
|
||||
return [
|
||||
$this->getFieldNameByFormatFieldName("MESS_UID") => $record["MESS_UID"],
|
||||
$this->getFieldNameByFormatFieldName("MESS_ENGINE") => $record["MESS_ENGINE"],
|
||||
$this->getFieldNameByFormatFieldName("MESS_SERVER") => $record["MESS_SERVER"],
|
||||
@@ -1143,8 +1161,11 @@ class EmailServer
|
||||
$this->getFieldNameByFormatFieldName("MESS_BACKGROUND") => '',
|
||||
$this->getFieldNameByFormatFieldName("MESS_PASSWORD_HIDDEN") => '',
|
||||
$this->getFieldNameByFormatFieldName("MESS_EXECUTE_EVERY") => '',
|
||||
$this->getFieldNameByFormatFieldName("MESS_SEND_MAX") => ''
|
||||
);
|
||||
$this->getFieldNameByFormatFieldName("MESS_SEND_MAX") => '',
|
||||
$this->getFieldNameByFormatFieldName("OAUTH_CLIENT_ID") => $record["OAUTH_CLIENT_ID"],
|
||||
$this->getFieldNameByFormatFieldName("OAUTH_CLIENT_SECRET") => $record["OAUTH_CLIENT_SECRET"],
|
||||
$this->getFieldNameByFormatFieldName("OAUTH_REFRESH_TOKEN") => $record["OAUTH_REFRESH_TOKEN"]
|
||||
];
|
||||
} catch (Exception $e) {
|
||||
throw $e;
|
||||
}
|
||||
@@ -1191,6 +1212,12 @@ class EmailServer
|
||||
$arrayData["MESS_PASSWORD_HIDDEN"] = '';
|
||||
$arrayData["MESS_EXECUTE_EVERY"] = '';
|
||||
$arrayData["MESS_SEND_MAX"] = '';
|
||||
$arrayData["OAUTH_CLIENT_ID"] = !empty($row["OAUTH_CLIENT_ID"]) ?
|
||||
Crypt::decryptString($row["OAUTH_CLIENT_ID"]) : '';
|
||||
$arrayData["OAUTH_CLIENT_SECRET"] = !empty($row["OAUTH_CLIENT_SECRET"]) ?
|
||||
Crypt::decryptString($row["OAUTH_CLIENT_SECRET"]) : '';
|
||||
$arrayData["OAUTH_REFRESH_TOKEN"] = !empty($row["OAUTH_REFRESH_TOKEN"]) ?
|
||||
Crypt::decryptString($row["OAUTH_REFRESH_TOKEN"]) : '';
|
||||
}
|
||||
|
||||
//Return
|
||||
@@ -1287,6 +1314,9 @@ class EmailServer
|
||||
|
||||
while ($rsCriteria->next()) {
|
||||
$row = $rsCriteria->getRow();
|
||||
$row['OAUTH_CLIENT_ID'] = !empty($row['OAUTH_CLIENT_ID']) ? Crypt::decryptString($row['OAUTH_CLIENT_ID']) : '';
|
||||
$row['OAUTH_CLIENT_SECRET'] = !empty($row['OAUTH_CLIENT_SECRET']) ? Crypt::decryptString($row['OAUTH_CLIENT_SECRET']) : '';
|
||||
$row['OAUTH_REFRESH_TOKEN'] = !empty($row['OAUTH_REFRESH_TOKEN']) ? Crypt::decryptString($row['OAUTH_REFRESH_TOKEN']) : '';
|
||||
$arrayEmailServer[] = $this->getEmailServerDataFromRecord($row);
|
||||
}
|
||||
|
||||
@@ -1340,6 +1370,9 @@ class EmailServer
|
||||
$row["MESS_PASSWORD_HIDDEN"] = '';
|
||||
$row["MESS_EXECUTE_EVERY"] = '';
|
||||
$row["MESS_SEND_MAX"] = '';
|
||||
$row["OAUTH_CLIENT_ID"] = !empty($row["OAUTH_CLIENT_ID"]) ? Crypt::decryptString($row["OAUTH_CLIENT_ID"]) : '';
|
||||
$row["OAUTH_CLIENT_SECRET"] = !empty($row["OAUTH_CLIENT_SECRET"]) ? Crypt::decryptString($row["OAUTH_CLIENT_SECRET"]) : '';
|
||||
$row["OAUTH_REFRESH_TOKEN"] = !empty($row["OAUTH_REFRESH_TOKEN"]) ? Crypt::decryptString($row["OAUTH_REFRESH_TOKEN"]) : '';
|
||||
|
||||
//Return
|
||||
return (!$flagGetRecord)? $this->getEmailServerDataFromRecord($row) : $row;
|
||||
|
||||
@@ -1016,7 +1016,10 @@ class System
|
||||
"MESS_BACKGROUND" => "",
|
||||
"MESS_PASSWORD_HIDDEN" => "",
|
||||
"MESS_EXECUTE_EVERY" => "",
|
||||
"MESS_SEND_MAX" => ""
|
||||
"MESS_SEND_MAX" => "",
|
||||
"OAUTH_CLIENT_ID" => $arrayEmailServerDefault["OAUTH_CLIENT_ID"],
|
||||
"OAUTH_CLIENT_SECRET" => $arrayEmailServerDefault["OAUTH_CLIENT_SECRET"],
|
||||
"OAUTH_REFRESH_TOKEN" => $arrayEmailServerDefault["OAUTH_REFRESH_TOKEN"]
|
||||
);
|
||||
|
||||
//Return
|
||||
|
||||
482
workflow/engine/src/ProcessMaker/GmailOAuth/GmailOAuth.php
Normal file
482
workflow/engine/src/ProcessMaker/GmailOAuth/GmailOAuth.php
Normal file
@@ -0,0 +1,482 @@
|
||||
<?php
|
||||
|
||||
namespace ProcessMaker\GmailOAuth;
|
||||
|
||||
use AppMessage;
|
||||
use Bootstrap;
|
||||
use G;
|
||||
use Google_Client;
|
||||
use Google_Service_Gmail;
|
||||
use Google_Service_Gmail_Message;
|
||||
use PHPMailerOAuth;
|
||||
use ProcessMaker\BusinessModel\EmailServer;
|
||||
use ProcessMaker\Core\System;
|
||||
use TemplatePower;
|
||||
use WsBase;
|
||||
|
||||
class GmailOAuth
|
||||
{
|
||||
private $emailServerUid;
|
||||
private $emailEngine;
|
||||
private $clientID;
|
||||
private $clientSecret;
|
||||
private $fromAccount;
|
||||
private $senderEmail;
|
||||
private $senderName;
|
||||
private $sendTestMail;
|
||||
private $mailTo;
|
||||
private $setDefaultConfiguration;
|
||||
private $redirectURI;
|
||||
private $refreshToken;
|
||||
|
||||
/**
|
||||
* Set $emailServerUid property.
|
||||
* @param string $emailServerUid
|
||||
* @return void
|
||||
*/
|
||||
public function setEmailServerUid($emailServerUid): void
|
||||
{
|
||||
$this->emailServerUid = $emailServerUid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set $clientID property.
|
||||
* @param string $clientID
|
||||
* @return void
|
||||
*/
|
||||
public function setClientID($clientID): void
|
||||
{
|
||||
$this->clientID = $clientID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set $clientSecret property.
|
||||
* @param string $clientSecret
|
||||
* @return void
|
||||
*/
|
||||
public function setClientSecret($clientSecret): void
|
||||
{
|
||||
$this->clientSecret = $clientSecret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set $redirectURI property.
|
||||
* @param string $redirectURI
|
||||
* @return void
|
||||
*/
|
||||
public function setRedirectURI($redirectURI): void
|
||||
{
|
||||
$this->redirectURI = $redirectURI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set $emailEngine property.
|
||||
* @param string $emailEngine
|
||||
* @return void
|
||||
*/
|
||||
public function setEmailEngine($emailEngine): void
|
||||
{
|
||||
$this->emailEngine = $emailEngine;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set $fromAccount property.
|
||||
* @param string $fromAccount
|
||||
* @return void
|
||||
*/
|
||||
public function setFromAccount($fromAccount): void
|
||||
{
|
||||
$this->fromAccount = $fromAccount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set $senderEmail property.
|
||||
* @param string $senderEmail
|
||||
* @return void
|
||||
*/
|
||||
public function setSenderEmail($senderEmail): void
|
||||
{
|
||||
$this->senderEmail = $senderEmail;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set $senderName property.
|
||||
* @param string $senderName
|
||||
* @return void
|
||||
*/
|
||||
public function setSenderName($senderName): void
|
||||
{
|
||||
$this->senderName = $senderName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set $sendTestMail property.
|
||||
* @param string $sendTestMail
|
||||
* @return void
|
||||
*/
|
||||
public function setSendTestMail($sendTestMail): void
|
||||
{
|
||||
$this->sendTestMail = $sendTestMail;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set $mailTo property.
|
||||
* @param string $mailTo
|
||||
* @return void
|
||||
*/
|
||||
public function setMailTo($mailTo): void
|
||||
{
|
||||
$this->mailTo = $mailTo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set $setDefaultConfiguration property.
|
||||
* @param string $setDefaultConfiguration
|
||||
* @return void
|
||||
*/
|
||||
public function setSetDefaultConfiguration($setDefaultConfiguration): void
|
||||
{
|
||||
$this->setDefaultConfiguration = $setDefaultConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set $refreshToken property.
|
||||
* @param string $refreshToken
|
||||
* @return void
|
||||
*/
|
||||
public function setRefreshToken($refreshToken): void
|
||||
{
|
||||
$this->refreshToken = $refreshToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $emailServerUid property.
|
||||
* @return string
|
||||
*/
|
||||
public function getEmailServerUid()
|
||||
{
|
||||
return $this->emailServerUid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $clientID property.
|
||||
* @return string
|
||||
*/
|
||||
public function getClientID()
|
||||
{
|
||||
return $this->clientID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $clientSecret property.
|
||||
* @return string
|
||||
*/
|
||||
public function getClientSecret()
|
||||
{
|
||||
return $this->clientSecret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $redirectURI property.
|
||||
* @return string
|
||||
*/
|
||||
public function getRedirectURI()
|
||||
{
|
||||
return $this->redirectURI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $emailEngine property.
|
||||
* @return string
|
||||
*/
|
||||
public function getEmailEngine()
|
||||
{
|
||||
return $this->emailEngine;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $fromAccount property.
|
||||
* @return string
|
||||
*/
|
||||
public function getFromAccount()
|
||||
{
|
||||
return $this->fromAccount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $senderEmail property.
|
||||
* @return string
|
||||
*/
|
||||
public function getSenderEmail()
|
||||
{
|
||||
return $this->senderEmail;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $senderName property.
|
||||
* @return string
|
||||
*/
|
||||
public function getSenderName()
|
||||
{
|
||||
return $this->senderName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $sendTestMail property.
|
||||
* @return string
|
||||
*/
|
||||
public function getSendTestMail()
|
||||
{
|
||||
return $this->sendTestMail;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $mailTo property.
|
||||
* @return string
|
||||
*/
|
||||
public function getMailTo()
|
||||
{
|
||||
return $this->mailTo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $defaultConfiguration property.
|
||||
* @return string
|
||||
*/
|
||||
public function getSetDefaultConfiguration()
|
||||
{
|
||||
return $this->setDefaultConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get $refreshToken property.
|
||||
* @return string
|
||||
*/
|
||||
public function getRefreshToken()
|
||||
{
|
||||
return $this->refreshToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Google_Client object, this may vary depending on the service provider.
|
||||
* @return Google_Client
|
||||
*/
|
||||
public function getGoogleClient(): Google_Client
|
||||
{
|
||||
$googleClient = new Google_Client();
|
||||
$googleClient->setClientId($this->clientID);
|
||||
$googleClient->setClientSecret($this->clientSecret);
|
||||
$googleClient->setRedirectUri($this->redirectURI);
|
||||
$googleClient->setAccessType('offline');
|
||||
$googleClient->setApprovalPrompt('force');
|
||||
$googleClient->addScope(Google_Service_Gmail::MAIL_GOOGLE_COM);
|
||||
return $googleClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the data in the EmailServer table, and return the stored fields.
|
||||
* @return array
|
||||
*/
|
||||
public function saveEmailServer(): array
|
||||
{
|
||||
$result = [];
|
||||
$data = [
|
||||
"MESS_ENGINE" => $this->emailEngine,
|
||||
"OAUTH_CLIENT_ID" => $this->clientID,
|
||||
"OAUTH_CLIENT_SECRET" => $this->clientSecret,
|
||||
"OAUTH_REFRESH_TOKEN" => $this->refreshToken,
|
||||
"MESS_ACCOUNT" => $this->fromAccount,
|
||||
"MESS_FROM_MAIL" => $this->senderEmail,
|
||||
"MESS_FROM_NAME" => $this->senderName,
|
||||
"MESS_TRY_SEND_INMEDIATLY" => $this->sendTestMail,
|
||||
"MAIL_TO" => $this->mailTo,
|
||||
"MESS_DEFAULT" => $this->setDefaultConfiguration,
|
||||
"MESS_RAUTH" => 1,
|
||||
"SMTPSECURE" => "No",
|
||||
"MESS_PASSWORD" => "",
|
||||
"MESS_SERVER" => "smtp.gmail.com",
|
||||
"MESS_PORT" => "",
|
||||
"MESS_PASSWORD" => "",
|
||||
"MESS_INCOMING_SERVER" => "",
|
||||
"MESS_INCOMING_PORT" => ""
|
||||
];
|
||||
$emailServer = new EmailServer();
|
||||
if (empty($this->emailServerUid)) {
|
||||
$result = $emailServer->create($data);
|
||||
} else {
|
||||
$result = $emailServer->update($this->emailServerUid, $data);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This sends a test email with Google_Service_Gmail_Message object, as long
|
||||
* as the test flag is activated.
|
||||
* @return Google_Service_Gmail_Message
|
||||
*/
|
||||
public function sendTestEmailWithGoogleServiceGmail(): Google_Service_Gmail_Message
|
||||
{
|
||||
$googleServiceGmailMessage = new Google_Service_Gmail_Message();
|
||||
if (!filter_var($this->fromAccount, FILTER_VALIDATE_EMAIL)) {
|
||||
return $googleServiceGmailMessage;
|
||||
}
|
||||
if (!filter_var($this->mailTo, FILTER_VALIDATE_EMAIL)) {
|
||||
return $googleServiceGmailMessage;
|
||||
}
|
||||
if ($this->sendTestMail === 0) {
|
||||
return $googleServiceGmailMessage;
|
||||
}
|
||||
|
||||
$googleClient = $this->getGoogleClient();
|
||||
$googleClient->refreshToken($this->getRefreshToken());
|
||||
if ($googleClient->isAccessTokenExpired()) {
|
||||
$newAccessToken = $googleClient->getAccessToken();
|
||||
$googleClient->setAccessToken($newAccessToken);
|
||||
}
|
||||
|
||||
$raw = $this->getRawMessage();
|
||||
$googleServiceGmailMessage->setRaw($raw);
|
||||
|
||||
$service = new Google_Service_Gmail($googleClient);
|
||||
$result = $service->users_messages->send("me", $googleServiceGmailMessage);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get message body.
|
||||
* @return string
|
||||
*/
|
||||
public function getMessageBody(): string
|
||||
{
|
||||
$templateTower = new TemplatePower(PATH_TPL . "admin" . PATH_SEP . "email.tpl");
|
||||
$templateTower->prepare();
|
||||
$templateTower->assign("server", System::getServerHostname());
|
||||
$templateTower->assign("date", date("H:i:s"));
|
||||
$templateTower->assign("ver", System::getVersion());
|
||||
$templateTower->assign("engine", G::LoadTranslation("ID_MESS_ENGINE_TYPE_4"));
|
||||
$templateTower->assign("msg", G::LoadTranslation("ID_MESS_TEST_BODY"));
|
||||
$outputContent = $templateTower->getOutputContent();
|
||||
return $outputContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a plain text of the test message.
|
||||
* @return string
|
||||
*/
|
||||
public function getRawMessage(): string
|
||||
{
|
||||
$outputContent = $this->getMessageBody();
|
||||
|
||||
$strRawMessage = ""
|
||||
. "From: Email <{$this->fromAccount}> \r\n"
|
||||
. "To: <{$this->mailTo}>\r\n"
|
||||
. "Subject: =?utf-8?B?" . base64_encode(G::LoadTranslation("ID_MESS_TEST_SUBJECT")) . "?=\r\n"
|
||||
. "MIME-Version: 1.0\r\n"
|
||||
. "Content-Type: text/html; charset=utf-8\r\n"
|
||||
. "Content-Transfer-Encoding: quoted-printable\r\n\r\n"
|
||||
. "{$outputContent}\r\n";
|
||||
|
||||
$raw = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
|
||||
return $raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* This sends a test email with PHPMailerOAuth object, as long
|
||||
* as the test flag is activated.
|
||||
* @return PHPMailerOAuth
|
||||
*/
|
||||
public function sendTestMailWithPHPMailerOAuth(): PHPMailerOAuth
|
||||
{
|
||||
$phpMailerOAuth = new PHPMailerOAuth();
|
||||
if (!filter_var($this->fromAccount, FILTER_VALIDATE_EMAIL)) {
|
||||
return $phpMailerOAuth;
|
||||
}
|
||||
if (!filter_var($this->mailTo, FILTER_VALIDATE_EMAIL)) {
|
||||
return $phpMailerOAuth;
|
||||
}
|
||||
if ($this->sendTestMail === 0) {
|
||||
return $phpMailerOAuth;
|
||||
}
|
||||
$senderEmail = $this->senderEmail;
|
||||
if (!filter_var($senderEmail, FILTER_VALIDATE_EMAIL)) {
|
||||
$senderEmail = $this->fromAccount;
|
||||
}
|
||||
if (empty($this->senderName)) {
|
||||
$this->senderName = "";
|
||||
}
|
||||
|
||||
$phpMailerOAuth->isHTML(true);
|
||||
$phpMailerOAuth->isSMTP();
|
||||
$phpMailerOAuth->Host = 'smtp.gmail.com';
|
||||
$phpMailerOAuth->SMTPAuth = true;
|
||||
$phpMailerOAuth->AuthType = 'XOAUTH2';
|
||||
$phpMailerOAuth->oauthUserEmail = $this->fromAccount;
|
||||
$phpMailerOAuth->oauthClientId = $this->clientID;
|
||||
$phpMailerOAuth->oauthClientSecret = $this->clientSecret;
|
||||
$phpMailerOAuth->oauthRefreshToken = $this->refreshToken;
|
||||
$phpMailerOAuth->SetFrom($senderEmail, $this->senderName);
|
||||
$phpMailerOAuth->Subject = G::LoadTranslation("ID_MESS_TEST_SUBJECT");
|
||||
$phpMailerOAuth->Body = utf8_encode($this->getMessageBody());
|
||||
$phpMailerOAuth->AddAddress($this->mailTo);
|
||||
$status = $phpMailerOAuth->Send();
|
||||
$this->saveIntoStandardLogs($status ? "sent" : "pending");
|
||||
$this->saveIntoAppMessage($status ? "sent" : "pending");
|
||||
return $phpMailerOAuth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register into APP_MESSAGE table.
|
||||
* @param string $status
|
||||
*/
|
||||
public function saveIntoAppMessage(string $status = "")
|
||||
{
|
||||
$appMsgUid = G::generateUniqueID();
|
||||
$spool = new AppMessage();
|
||||
$spool->setAppMsgUid($appMsgUid);
|
||||
$spool->setMsgUid("");
|
||||
$spool->setAppUid("");
|
||||
$spool->setDelIndex(0);
|
||||
$spool->setAppMsgType(WsBase::MESSAGE_TYPE_TEST_EMAIL);
|
||||
$spool->setAppMsgTypeId(isset(AppMessage::$app_msg_type_values[WsBase::MESSAGE_TYPE_TEST_EMAIL]) ? AppMessage::$app_msg_type_values[WsBase::MESSAGE_TYPE_TEST_EMAIL] : 0);
|
||||
$spool->setAppMsgSubject(G::LoadTranslation("ID_MESS_TEST_SUBJECT"));
|
||||
$spool->setAppMsgFrom($this->fromAccount);
|
||||
$spool->setAppMsgTo($this->mailTo);
|
||||
$spool->setAppMsgBody(utf8_encode($this->getMessageBody()));
|
||||
$spool->setAppMsgDate(date('Y-m-d H:i:s'));
|
||||
$spool->setAppMsgCc("");
|
||||
$spool->setAppMsgBcc("");
|
||||
$spool->setappMsgAttach(serialize([""]));
|
||||
$spool->setAppMsgTemplate("");
|
||||
$spool->setAppMsgStatus($status);
|
||||
$spool->setAppMsgStatusId(AppMessage::$app_msg_status_values[$status] ? AppMessage::$app_msg_status_values[$status] : 0);
|
||||
$spool->setAppMsgSendDate(date('Y-m-d H:i:s'));
|
||||
$spool->setAppMsgShowMessage(1);
|
||||
$spool->setAppMsgError("");
|
||||
$spool->setAppNumber(0);
|
||||
$spool->setTasId(0);
|
||||
$spool->setProId(0);
|
||||
$spool->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register into standard logs.
|
||||
* @param string $status
|
||||
*/
|
||||
public function saveIntoStandardLogs(string $status = "")
|
||||
{
|
||||
$channel = "Test Email Servers Configuration";
|
||||
$severity = 200; //INFO
|
||||
$message = "Email Server test has been sent";
|
||||
$context = [
|
||||
"emailServerUid" => $this->emailServerUid,
|
||||
"emailEngine" => $this->emailEngine,
|
||||
"from" => $this->fromAccount,
|
||||
"senderAccount" => $this->mailTo,
|
||||
"senderEmail" => $this->senderEmail,
|
||||
"senderName" => $this->senderName,
|
||||
"status" => $status
|
||||
];
|
||||
$workspace = config("system.workspace");
|
||||
Bootstrap::registerMonolog($channel, $severity, $message, $context, $workspace);
|
||||
}
|
||||
}
|
||||
12
workflow/engine/src/ProcessMaker/Model/AppMessage.php
Normal file
12
workflow/engine/src/ProcessMaker/Model/AppMessage.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace ProcessMaker\Model;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class AppMessage extends Model
|
||||
{
|
||||
protected $table = 'APP_MESSAGE';
|
||||
public $timestamps = false;
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace ProcessMaker\Model;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
|
||||
class EmailServerModel extends Model
|
||||
{
|
||||
@@ -31,13 +32,25 @@ class EmailServerModel extends Model
|
||||
'EMAIL_SERVER.SMTPSECURE',
|
||||
'EMAIL_SERVER.MESS_TRY_SEND_INMEDIATLY',
|
||||
'EMAIL_SERVER.MAIL_TO',
|
||||
'EMAIL_SERVER.MESS_DEFAULT'
|
||||
'EMAIL_SERVER.MESS_DEFAULT',
|
||||
'EMAIL_SERVER.OAUTH_CLIENT_ID',
|
||||
'EMAIL_SERVER.OAUTH_CLIENT_SECRET',
|
||||
'EMAIL_SERVER.OAUTH_REFRESH_TOKEN'
|
||||
];
|
||||
$query = EmailServerModel::query()->select($selectedColumns);
|
||||
$query->where('EMAIL_SERVER.MESS_UID', '=', $messUid);
|
||||
$res = $query->get()->values()->toArray();
|
||||
$firstElement = head($res);
|
||||
|
||||
if (!empty($firstElement)) {
|
||||
$firstElement['OAUTH_CLIENT_ID'] = !empty($firstElement['OAUTH_CLIENT_ID']) ?
|
||||
Crypt::decryptString($firstElement['OAUTH_CLIENT_ID']) : '';
|
||||
$firstElement['OAUTH_CLIENT_SECRET'] = !empty($firstElement['OAUTH_CLIENT_SECRET']) ?
|
||||
Crypt::decryptString($firstElement['OAUTH_CLIENT_SECRET']) : '';
|
||||
$firstElement['OAUTH_REFRESH_TOKEN'] = !empty($firstElement['OAUTH_REFRESH_TOKEN']) ?
|
||||
Crypt::decryptString($firstElement['OAUTH_REFRESH_TOKEN']) : '';
|
||||
}
|
||||
|
||||
return $firstElement;
|
||||
}
|
||||
|
||||
@@ -64,7 +77,10 @@ class EmailServerModel extends Model
|
||||
'EMAIL_SERVER.SMTPSECURE',
|
||||
'EMAIL_SERVER.MESS_TRY_SEND_INMEDIATLY',
|
||||
'EMAIL_SERVER.MAIL_TO',
|
||||
'EMAIL_SERVER.MESS_DEFAULT'
|
||||
'EMAIL_SERVER.MESS_DEFAULT',
|
||||
'EMAIL_SERVER.OAUTH_CLIENT_ID',
|
||||
'EMAIL_SERVER.OAUTH_CLIENT_SECRET',
|
||||
'EMAIL_SERVER.OAUTH_REFRESH_TOKEN'
|
||||
];
|
||||
$query = EmailServerModel::query()->select($selectedColumns)
|
||||
->where('MESS_DEFAULT', '=', 1);
|
||||
@@ -77,6 +93,12 @@ class EmailServerModel extends Model
|
||||
$firstElement['MESS_PASSWORD_HIDDEN'] = '';
|
||||
$firstElement['MESS_EXECUTE_EVERY'] = '';
|
||||
$firstElement['MESS_SEND_MAX'] = '';
|
||||
$firstElement['OAUTH_CLIENT_ID'] = !empty($firstElement['OAUTH_CLIENT_ID']) ?
|
||||
Crypt::decryptString($firstElement['OAUTH_CLIENT_ID']) : '';
|
||||
$firstElement['OAUTH_CLIENT_SECRET'] = !empty($firstElement['OAUTH_CLIENT_SECRET']) ?
|
||||
Crypt::decryptString($firstElement['OAUTH_CLIENT_SECRET']) : '';
|
||||
$firstElement['OAUTH_REFRESH_TOKEN'] = !empty($firstElement['OAUTH_REFRESH_TOKEN']) ?
|
||||
Crypt::decryptString($firstElement['OAUTH_REFRESH_TOKEN']) : '';
|
||||
}
|
||||
|
||||
return $firstElement;
|
||||
|
||||
Reference in New Issue
Block a user