2024-12-09 14:37:11 +00:00
< ? php
use App\Models\InstanceSettings ;
use App\Models\Team ;
use App\Notifications\Internal\GeneralNotification ;
use Illuminate\Mail\Message ;
use Illuminate\Notifications\Messages\MailMessage ;
use Illuminate\Support\Facades\Mail ;
function is_transactional_emails_enabled () : bool
{
$settings = instanceSettings ();
return $settings -> smtp_enabled || $settings -> resend_enabled ;
}
function send_internal_notification ( string $message ) : void
{
try {
$team = Team :: find ( 0 );
$team ? -> notify ( new GeneralNotification ( $message ));
} catch ( \Throwable $e ) {
ray ( $e -> getMessage ());
}
}
function send_user_an_email ( MailMessage $mail , string $email , ? string $cc = null ) : void
{
$settings = instanceSettings ();
$type = set_transanctional_email_settings ( $settings );
2025-02-27 11:56:37 +00:00
if ( blank ( $type )) {
2024-12-09 14:37:11 +00:00
throw new Exception ( 'No email settings found.' );
}
if ( $cc ) {
Mail :: send (
[],
[],
fn ( Message $message ) => $message
-> to ( $email )
-> replyTo ( $email )
-> cc ( $cc )
-> subject ( $mail -> subject )
-> html (( string ) $mail -> render ())
);
} else {
Mail :: send (
[],
[],
fn ( Message $message ) => $message
-> to ( $email )
-> subject ( $mail -> subject )
-> html (( string ) $mail -> render ())
);
}
}
2025-02-27 11:56:37 +00:00
function set_transanctional_email_settings ( ? InstanceSettings $settings = null ) : ? string // returns null|resend|smtp and defaults to array based on mail.php config
2024-12-09 14:37:11 +00:00
{
if ( ! $settings ) {
$settings = instanceSettings ();
}
2025-02-27 11:56:37 +00:00
if ( ! data_get ( $settings , 'smtp_enabled' ) && ! data_get ( $settings , 'resend_enabled' )) {
return null ;
}
2024-12-09 14:37:11 +00:00
if ( data_get ( $settings , 'resend_enabled' )) {
config () -> set ( 'mail.default' , 'resend' );
2025-02-27 11:56:37 +00:00
config () -> set ( 'mail.from.address' , data_get ( $settings , 'smtp_from_address' ));
config () -> set ( 'mail.from.name' , data_get ( $settings , 'smtp_from_name' ));
2024-12-09 14:37:11 +00:00
config () -> set ( 'resend.api_key' , data_get ( $settings , 'resend_api_key' ));
return 'resend' ;
}
2024-12-23 14:28:35 +00:00
$encryption = match ( strtolower ( data_get ( $settings , 'smtp_encryption' ))) {
'starttls' => null ,
'tls' => 'tls' ,
'none' => null ,
default => null ,
};
2024-12-09 14:37:11 +00:00
if ( data_get ( $settings , 'smtp_enabled' )) {
2025-02-27 11:56:37 +00:00
config () -> set ( 'mail.from.address' , data_get ( $settings , 'smtp_from_address' ));
config () -> set ( 'mail.from.name' , data_get ( $settings , 'smtp_from_name' ));
2024-12-09 14:37:11 +00:00
config () -> set ( 'mail.default' , 'smtp' );
config () -> set ( 'mail.mailers.smtp' , [
'transport' => 'smtp' ,
'host' => data_get ( $settings , 'smtp_host' ),
'port' => data_get ( $settings , 'smtp_port' ),
2024-12-23 14:28:35 +00:00
'encryption' => $encryption ,
2024-12-09 14:37:11 +00:00
'username' => data_get ( $settings , 'smtp_username' ),
'password' => data_get ( $settings , 'smtp_password' ),
'timeout' => data_get ( $settings , 'smtp_timeout' ),
'local_domain' => null ,
2024-12-23 14:28:35 +00:00
'auto_tls' => data_get ( $settings , 'smtp_encryption' ) === 'none' ? '0' : '' , // If encryption is "none", it will not try to upgrade to TLS via StartTLS to make sure it is unencrypted.
2024-12-09 14:37:11 +00:00
]);
return 'smtp' ;
}
}