Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+69 -101
View File
@@ -10,19 +10,32 @@
namespace IPS\Email\Outgoing;
use IPS\Settings;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use IPS\core\Advertisement;
use IPS\Db;
use IPS\Email;
use IPS\Http\Request\Exception;
use IPS\Http\Url;
use IPS\Lang;
use IPS\Member;
use IPS\Settings;
use RuntimeException;
use function count;
use function defined;
use function is_array;
use const IPS\LONG_REQUEST_TIMEOUT;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* SendGrid Email Class
*/
class _SendGrid extends \IPS\Email
class SendGrid extends Email
{
/* !Configuration */
@@ -34,15 +47,15 @@ class _SendGrid extends \IPS\Email
/**
* @brief API Key
*/
protected $apiKey;
protected string $apiKey;
/**
* Constructor
*
* @param string $apiKey API Key
* @param string $apiKey API Key
* @return void
*/
public function __construct( $apiKey )
public function __construct( string $apiKey )
{
$this->apiKey = $apiKey;
}
@@ -51,15 +64,15 @@ class _SendGrid extends \IPS\Email
* Send the email
*
* @param mixed $to The member or email address, or array of members or email addresses, to send to
* @param mixed $cc Addresses to CC (can also be email, member or array of either)
* @param mixed $bcc Addresses to BCC (can also be email, member or array of either)
* @param mixed $fromEmail The email address to send from. If NULL, default setting is used
* @param mixed $fromName The name the email should appear from. If NULL, default setting is used
* @param array $additionalHeaders The name the email should appear from. If NULL, default setting is used
* @param mixed $cc Addresses to CC (can also be email, member or array of either)
* @param mixed $bcc Addresses to BCC (can also be email, member or array of either)
* @param mixed $fromEmail The email address to send from. If NULL, default setting is used
* @param mixed $fromName The name the email should appear from. If NULL, default setting is used
* @param array $additionalHeaders The name the email should appear from. If NULL, default setting is used
* @return void
* @throws \IPS\Email\Outgoing\Exception
* @throws Exception
*/
public function _send( $to, $cc=array(), $bcc=array(), $fromEmail = NULL, $fromName = NULL, $additionalHeaders = array() )
public function _send( mixed $to, mixed $cc=array(), mixed $bcc=array(), mixed $fromEmail = NULL, mixed $fromName = NULL, array $additionalHeaders = array() ) : void
{
/* Initiate the request */
$request = $this->_initRequest( $fromEmail, $fromName );
@@ -80,11 +93,11 @@ class _SendGrid extends \IPS\Email
/* Add the recipients */
foreach ( array( 'to', 'cc', 'bcc' ) as $type )
{
if ( \is_array( $$type ) )
if ( is_array( $$type ) )
{
foreach ( $$type as $recipient )
{
if ( $recipient instanceof \IPS\Member )
if ( $recipient instanceof Member )
{
$request['personalizations'][0][ $type ][] = array( 'email' => $recipient->email, 'name' => $recipient->name );
}
@@ -97,7 +110,7 @@ class _SendGrid extends \IPS\Email
elseif ( $$type )
{
$recipient = $$type;
if ( $recipient instanceof \IPS\Member )
if ( $recipient instanceof Member )
{
$request['personalizations'][0][ $type ][] = array( 'email' => $recipient->email, 'name' => $recipient->name );
}
@@ -115,50 +128,29 @@ class _SendGrid extends \IPS\Email
$response = $this->_api( 'mail/send', $request );
if ( isset( $response['errors'] ) )
{
throw new \IPS\Email\Outgoing\Exception( $response['errors'][0]['message'] );
throw new Exception( $response['errors'][0]['message'] );
}
}
/**
* Is this email class usable?
*
* @param string $type Email Type
* @return bool
*/
public static function isUsable( string $type ): bool
{
if( ( new \IPS\core\extensions\core\CommunityEnhancements\Sendgrid() )->enabled )
{
return match( true )
{
( $type == \IPS\Email::TYPE_TRANSACTIONAL OR $type == \IPS\Email::TYPE_LIST ) AND Settings::i()->sendgrid_use_for == 2 => true,
$type == \IPS\Email::TYPE_BULK AND (int) Settings::i()->sendgrid_use_for > 0 => true,
default => false
};
}
return false;
}
/**
* Merge and Send
*
* @param array $recipients Array where the keys are the email addresses to send to and the values are an array of variables to replace
* @param mixed $fromEmail The email address to send from. If NULL, default setting is used. NOTE: This should always be a site-controlled domin. Some services like Sparkpost require the domain to be validated.
* @param mixed $fromName The name the email should appear from. If NULL, default setting is used
* @param array $additionalHeaders Additional headers to send. Merge tags can be used like in content.
* @param \IPS\Lang|NULL $language The language the email content should be in
* @param array $recipients Array where the keys are the email addresses to send to and the values are an array of variables to replace
* @param mixed $fromEmail The email address to send from. If NULL, default setting is used. NOTE: This should always be a site-controlled domin. Some services like Sparkpost require the domain to be validated.
* @param mixed $fromName The name the email should appear from. If NULL, default setting is used
* @param array $additionalHeaders Additional headers to send. Merge tags can be used like in content.
* @param Lang|NULL $language The language the email content should be in
* @return int Number of successful sends
*/
public function mergeAndSend( $recipients, $fromEmail = NULL, $fromName = NULL, $additionalHeaders = array(), \IPS\Lang $language = NULL )
public function mergeAndSend( array $recipients, mixed $fromEmail = NULL, mixed $fromName = NULL, array $additionalHeaders = array(), Lang $language = NULL ): int
{
/* Initiate the request */
$request = $this->_initRequest( $fromEmail, $fromName );
/* Add the subject and content */
$subject = $this->compileSubject( NULL, $language );
$htmlContent = $this->compileContent( 'html', FALSE, $language );
$plaintextContent = preg_replace( '/\*\|(.+?)\|\*/', '*|$1_plain|*', $this->compileContent( 'plaintext', FALSE, $language ) );
$htmlContent = $this->compileContent( 'html', null, $language );
$plaintextContent = preg_replace( '/\*\|(.+?)\|\*/', '*|$1_plain|*', $this->compileContent( 'plaintext', null, $language ) );
$request['subject'] = preg_replace( '/\*\|(.+?)\|\*/', '*|$1_plain|*', $subject );
$request['content'] = array(
array(
@@ -182,7 +174,7 @@ class _SendGrid extends \IPS\Email
{
$language->parseEmail( $v );
$finalSubstitutions["*|{$k}_plain|*"] = $v;
$finalSubstitutions["*|{$k}|*"] = htmlspecialchars( $v, ENT_QUOTES | ENT_DISALLOWED, 'UTF-8', TRUE );
$finalSubstitutions["*|{$k}|*"] = htmlspecialchars( $v, ENT_QUOTES | ENT_DISALLOWED, 'UTF-8' );
}
$request['personalizations'][] = array(
@@ -195,16 +187,16 @@ class _SendGrid extends \IPS\Email
$request = $this->_modifyRequestDataWithHeaders( $request, $additionalHeaders );
/* Log emails sent */
$this->_trackStatistics( \count( $addresses ) );
$this->_trackStatistics( count( $addresses ) );
/* Send */
try
{
$response = $this->_api( 'mail/send', $request );
}
catch( \IPS\Email\Outgoing\Exception $e )
catch(Exception $e )
{
\IPS\Db::i()->insert( 'core_mail_error_logs', array(
Db::i()->insert( 'core_mail_error_logs', array(
'mlog_date' => time(),
'mlog_to' => json_encode( $addresses ),
'mlog_from' => $fromEmail ?: Settings::i()->email_out,
@@ -221,8 +213,8 @@ class _SendGrid extends \IPS\Email
$errorcount = 0;
if ( isset( $response['errors'] ) )
{
$errorcount = \count( $response['errors'] );
\IPS\Db::i()->insert( 'core_mail_error_logs', array(
$errorcount = count( $response['errors'] );
Db::i()->insert( 'core_mail_error_logs', array(
'mlog_date' => time(),
'mlog_to' => json_encode( $addresses ),
'mlog_from' => $fromEmail ?: Settings::i()->email_out,
@@ -234,10 +226,10 @@ class _SendGrid extends \IPS\Email
) );
}
$successCount = ( \count( $recipients ) - $errorcount );
$successCount = ( count( $recipients ) - $errorcount );
/* Update ad impression count */
\IPS\core\Advertisement::updateEmailImpressions( $successCount );
Advertisement::updateEmailImpressions( $successCount );
return $successCount;
}
@@ -245,11 +237,11 @@ class _SendGrid extends \IPS\Email
/**
* Create a request
*
* @param mixed $fromEmail The email address to send from. If NULL, default setting is used. NOTE: This should always be a site-controlled domin. Some services like Sparkpost require the domain to be validated.
* @param mixed $fromName The name the email should appear from. If NULL, default setting is used
* @param mixed $fromEmail The email address to send from. If NULL, default setting is used. NOTE: This should always be a site-controlled domin. Some services like Sparkpost require the domain to be validated.
* @param mixed $fromName The name the email should appear from. If NULL, default setting is used
* @return array
*/
protected function _initRequest( $fromEmail = NULL, $fromName = NULL )
protected function _initRequest( mixed $fromEmail = NULL, mixed $fromName = NULL ): array
{
$request = array(
'personalizations' => array(),
@@ -279,12 +271,12 @@ class _SendGrid extends \IPS\Email
/**
* Modify the request data that will be sent to the SparkPost API with header data
*
* @param array $request SparkPost API request data
* @param array $additionalHeaders Additional headers to send
* @param array $allowedTags The tags that we want to parse
* @param array $request SparkPost API request data
* @param array $additionalHeaders Additional headers to send
* @param array $allowedTags The tags that we want to parse
* @return array
*/
protected function _modifyRequestDataWithHeaders( $request, $additionalHeaders = array(), $allowedTags = array() )
protected function _modifyRequestDataWithHeaders( array $request, array $additionalHeaders = array(), array $allowedTags = array() ): array
{
/* Do we have a Reply-To? */
if ( isset( $additionalHeaders['Reply-To'] ) )
@@ -297,7 +289,7 @@ class _SendGrid extends \IPS\Email
if ( $matches[1] )
{
if ( preg_match( '/^\=\?UTF\-8\?B\?(.+?)\?\=$/i', trim( $matches[1] ), $_matches ) )
if ( preg_match( '/^=\?UTF-8\?B\?(.+?)\?=$/i', trim( $matches[1] ), $_matches ) )
{
$request['reply_to']['name'] = base64_decode( $_matches[1] );
}
@@ -319,7 +311,7 @@ class _SendGrid extends \IPS\Email
unset( $additionalHeaders['To'] );
unset( $additionalHeaders['CC'] );
unset( $additionalHeaders['BCC'] );
if ( \count( $additionalHeaders ) )
if ( count( $additionalHeaders ) )
{
$request['headers'] = $additionalHeaders;
}
@@ -331,15 +323,15 @@ class _SendGrid extends \IPS\Email
/**
* Make API call
*
* @param string $method Method
* @param array $args Arguments
* @throws \IPS\Email\Outgoing\Exception Indicates an invalid JSON response or HTTP error
* @return array|null
* @param string $method Method
* @param array|null $args Arguments
* @return array|null
*@throws Exception Indicates an invalid JSON response or HTTP error
*/
protected function _api( $method, $args=NULL )
protected function _api(string $method, array $args=NULL ): ?array
{
$request = \IPS\Http\Url::external( 'https://api.sendgrid.com/v3/' . $method )
->request( \IPS\LONG_REQUEST_TIMEOUT )
$request = Url::external( 'https://api.sendgrid.com/v3/' . $method )
->request( LONG_REQUEST_TIMEOUT )
->setHeaders( array( 'Content-Type' => 'application/json', 'Authorization' => "Bearer {$this->apiKey}" ) );
try
@@ -365,44 +357,20 @@ class _SendGrid extends \IPS\Email
return $response;
}
catch ( \IPS\Http\Request\Exception $e )
catch ( Exception | RuntimeException $e )
{
throw new \IPS\Email\Outgoing\Exception( $e->getMessage(), $e->getCode() );
}
/* Capture json decoding errors */
catch ( \RuntimeException $e )
{
throw new \IPS\Email\Outgoing\Exception( $e->getMessage(), $e->getCode() );
throw new Exception($e->getMessage(), $e->getCode() );
}
}
/**
* Get API key scopes
*
* @return array
* @return array|null
*/
public function scopes()
public function scopes(): ?array
{
return $this->_api( 'scopes' );
}
/**
* Parse/Save email form settings
*
* @param array $values settings array
* @return array
*/
public static function processSettings( array $values ): array
{
if ( isset( $values['mail_method'] ) and $values['mail_method'] != 'sendgrid' and Settings::i()->sendgrid_use_for == 2 )
{
$values['sendgrid_use_for'] = 1;
}
elseif ( isset( $values['mail_method'] ) and $values['mail_method'] == 'sendgrid' )
{
$values['sendgrid_use_for'] = 2;
}
return $values;
}
}