2023-06-01 10:15:33 +00:00
< ? php
namespace App\Notifications\Channels ;
2023-08-08 15:28:36 +00:00
use Exception ;
2023-06-01 10:15:33 +00:00
use Illuminate\Mail\Message ;
use Illuminate\Notifications\Notification ;
use Illuminate\Support\Facades\Mail ;
class EmailChannel
{
2025-01-07 14:31:43 +00:00
public function send ( SendsEmail $notifiable , Notification $notification ) : void
2023-06-01 10:15:33 +00:00
{
2023-09-18 13:19:27 +00:00
try {
2025-01-07 14:31:43 +00:00
$this -> bootConfigs ( $notifiable );
$recipients = $notifiable -> getRecipients ( $notification );
2024-01-07 22:32:54 +00:00
if ( count ( $recipients ) === 0 ) {
2023-09-18 13:19:27 +00:00
throw new Exception ( 'No email recipients found' );
}
2023-06-12 10:00:01 +00:00
2025-01-07 14:31:43 +00:00
$mailMessage = $notification -> toMail ( $notifiable );
2023-09-18 13:19:27 +00:00
Mail :: send (
[],
[],
fn ( Message $message ) => $message
2024-01-07 22:32:54 +00:00
-> to ( $recipients )
2023-09-18 13:19:27 +00:00
-> subject ( $mailMessage -> subject )
2024-06-10 20:43:34 +00:00
-> html (( string ) $mailMessage -> render ())
2023-09-18 13:19:27 +00:00
);
} catch ( Exception $e ) {
2023-11-14 10:04:45 +00:00
$error = $e -> getMessage ();
if ( $error === 'No email settings found.' ) {
throw $e ;
}
2023-10-18 12:22:09 +00:00
$message = " EmailChannel error: { $e -> getMessage () } . Failed to send email to: " ;
2024-01-07 22:32:54 +00:00
if ( isset ( $recipients )) {
$message .= implode ( ', ' , $recipients );
2023-10-18 12:22:09 +00:00
}
2023-11-06 09:23:51 +00:00
if ( isset ( $mailMessage )) {
$message .= " with subject: { $mailMessage -> subject } " ;
}
2023-10-18 12:22:09 +00:00
send_internal_notification ( $message );
2023-09-18 13:19:27 +00:00
throw $e ;
2023-06-01 10:15:33 +00:00
}
}
private function bootConfigs ( $notifiable ) : void
{
2024-12-09 16:37:15 +00:00
$emailSettings = $notifiable -> emailNotificationSettings ;
if ( $emailSettings -> use_instance_email_settings ) {
2023-08-31 13:00:59 +00:00
$type = set_transanctional_email_settings ();
2025-02-27 11:56:37 +00:00
if ( blank ( $type )) {
2023-08-31 13:00:59 +00:00
throw new Exception ( 'No email settings found.' );
}
return ;
}
2024-12-09 16:37:15 +00:00
config () -> set ( 'mail.from.address' , $emailSettings -> smtp_from_address ? ? 'test@example.com' );
config () -> set ( 'mail.from.name' , $emailSettings -> smtp_from_name ? ? 'Test' );
if ( $emailSettings -> resend_enabled ) {
2023-08-31 13:00:59 +00:00
config () -> set ( 'mail.default' , 'resend' );
2024-12-09 16:37:15 +00:00
config () -> set ( 'resend.api_key' , $emailSettings -> resend_api_key );
2023-08-31 11:10:39 +00:00
}
2024-12-09 16:37:15 +00:00
if ( $emailSettings -> smtp_enabled ) {
2024-12-23 14:28:35 +00:00
$encryption = match ( strtolower ( $emailSettings -> smtp_encryption )) {
'starttls' => null ,
'tls' => 'tls' ,
'none' => null ,
default => null ,
};
2023-08-21 09:11:51 +00:00
config () -> set ( 'mail.default' , 'smtp' );
config () -> set ( 'mail.mailers.smtp' , [
2024-06-10 20:43:34 +00:00
'transport' => 'smtp' ,
2024-12-09 16:37:15 +00:00
'host' => $emailSettings -> smtp_host ,
'port' => $emailSettings -> smtp_port ,
2024-12-23 14:28:35 +00:00
'encryption' => $encryption ,
2024-12-09 16:37:15 +00:00
'username' => $emailSettings -> smtp_username ,
'password' => $emailSettings -> smtp_password ,
'timeout' => $emailSettings -> smtp_timeout ,
2024-06-10 20:43:34 +00:00
'local_domain' => null ,
2024-12-23 14:28:35 +00:00
'auto_tls' => $emailSettings -> smtp_encryption === 'none' ? '0' : '' , // If encryption is "none", it will not try to upgrade to TLS via StartTLS to make sure it is unencrypted.
2023-08-21 09:11:51 +00:00
]);
}
2023-06-01 10:15:33 +00:00
}
2023-08-08 09:51:36 +00:00
}