File size: 4,859 Bytes
6966756 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | <?php
namespace App\Notify;
use App\Notify\NotifyProcess;
use App\Notify\Notifiable;
use Mailjet\Client;
use Mailjet\Resources;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use SendGrid;
use SendGrid\Mail\Mail;
class Email extends NotifyProcess implements Notifiable{
/**
* Email of receiver
*
* @var string
*/
public $email;
/**
* Assign value to properties
*
* @return void
*/
public function __construct(){
$this->statusField = 'email_status';
$this->body = 'email_body';
$this->globalTemplate = 'email_template';
$this->notifyConfig = 'mail_config';
}
/**
* Send notification
*
* @return void|bool
*/
public function send(){
if (!gs('en')) {
return false;
}
//get message from parent
$message = $this->getMessage();
if ($message) {
//Send mail
$methodName = gs('mail_config')->name;
$method = $this->mailMethods($methodName);
try{
$this->$method();
$this->createLog('email');
}catch(\Exception $e){
$this->createErrorLog($e->getMessage());
session()->flash('mail_error',$e->getMessage());
}
}
}
/**
* Get the method name
*
* @return string
*/
protected function mailMethods($name){
$methods = [
'php'=>'sendPhpMail',
'smtp'=>'sendSmtpMail',
'sendgrid'=>'sendSendGridMail',
'mailjet'=>'sendMailjetMail',
];
return $methods[$name];
}
protected function sendPhpMail(){
$sentFromName = $this->getEmailFrom()['name'];
$sentFromEmail = $this->getEmailFrom()['email'];
$headers = "From: $sentFromName <$sentFromEmail> \r\n";
$headers .= "Reply-To: $sentFromName <$sentFromEmail> \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
@mail($this->email, $this->subject, $this->finalMessage, $headers);
}
protected function sendSmtpMail(){
$mail = new PHPMailer(true);
$config = gs('mail_config');
//Server settings
$mail->isSMTP();
$mail->Host = $config->host;
$mail->SMTPAuth = true;
$mail->Username = $config->username;
$mail->Password = $config->password;
if ($config->enc == 'ssl') {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
}else{
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
}
$mail->Port = $config->port;
$mail->CharSet = 'UTF-8';
//Recipients
$mail->setFrom($this->getEmailFrom()['email'], $this->getEmailFrom()['name']);
$mail->addAddress($this->email, $this->receiverName);
$mail->addReplyTo($this->getEmailFrom()['email'], $this->getEmailFrom()['name']);
// Content
$mail->isHTML(true);
$mail->Subject = $this->subject;
$mail->Body = $this->finalMessage;
$mail->send();
}
protected function sendSendGridMail(){
$sendgridMail = new Mail();
$sendgridMail->setFrom($this->getEmailFrom()['email'], $this->getEmailFrom()['name']);
$sendgridMail->setSubject($this->subject);
$sendgridMail->addTo($this->email, $this->receiverName);
$sendgridMail->addContent("text/html", $this->finalMessage);
$sendgrid = new SendGrid(gs('mail_config')->appkey);
$response = $sendgrid->send($sendgridMail);
if($response->statusCode() != 202){
throw new Exception(json_decode($response->body())->errors[0]->message);
}
}
protected function sendMailjetMail()
{
$mj = new Client(gs('mail_config')->public_key, gs('mail_config')->secret_key, true, ['version' => 'v3.1']);
$body = [
'Messages' => [
[
'From' => [
'Email' => $this->getEmailFrom()['email'],
'Name' => $this->getEmailFrom()['name'],
],
'To' => [
[
'Email' => $this->email,
'Name' => $this->receiverName,
]
],
'Subject' => $this->subject,
'TextPart' => "",
'HTMLPart' => $this->finalMessage,
]
]
];
$response = $mj->post(Resources::$Email, ['body' => $body]);
}
/**
* Configure some properties
*
* @return void
*/
public function prevConfiguration(){
if ($this->user) {
$this->email = $this->user->email;
$this->receiverName = $this->user->fullname;
}
$this->toAddress = $this->email;
}
private function getEmailFrom(){
$this->sentFrom = $this->template->email_sent_from_address ?? gs('email_from');
return [
'email'=>$this->sentFrom,
'name'=>$this->replaceTemplateShortCode($this->template->email_sent_from_name ?? gs('site_name')),
];
}
}
|