Configuring SMTP settings

My site is using ses for the smtp service. To use that I tried following configuration in config file.

 
$CONFIG->emailer_transport = 'smtp';

 

/**
 * Configure emailer SMTP settings
 *
 * This setting is only necessary if the above emailer transport is set to 'smtp'.
 */
$CONFIG->emailer_smtp_settings = array(
    'name'              => 'localhost.localdomain',
    'host'              => 'xxxx.smtp.aws.com',
    'connection_class'  => 'login',
    'connection_config' => [
        'username' => 'xxxx-username',
        'password' => 'xxxx-password',
        'ssl'      => 'ssl'// OPTIONAL (tls or ssl)
        'port'     => '587'// OPTIONAL (Non-SSL default 25, SSL default 465, TLS default 587)
//      'use_complete_quit' => '', // OPTIONAL
    ],
);

With the above configuration, its expected that the mail is sent from the server. But no mail is getting send from server. 

I tried same smtp settings with phpmailer which is sending the mails successfully and I am receiving it.

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

 

// Load Composer's autoloader
require '/vendor/autoload.php';

 

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

 

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                              // Enable verbose debug output
    $mail->isSMTP();                                                    // Send using SMTP
    $mail->Host       = 'xxxx.smtp.aws.com';                            // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                           // Enable SMTP authentication
    $mail->Username   = 'xxxx-username';                                // SMTP username
    $mail->Password   = 'xxxx-password';                                // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;                 // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                            // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

 

    //Recipients
    $mail->setFrom('info@yyy.com''Name');
    $mail->addAddress('receiver@gmail.com''Joe User');                 // Add a recipient

 

    // Content
    $mail->isHTML(true);                                                 // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

 

    $mail->send();
    echo 'Message has been sent';
catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

 

?>

 

Even with simple mail() function mails are gettiing delivered. But not when setting the config values to "sendmail" or "smtp". Any idea?

  • Fix this piece and try again:

    'ssl'      => 'ssl', // OPTIONAL (tls or ssl)
    'port'     => '587', // OPTIONAL (Non-SSL default 25, SSL default 465, TLS default 587)

    Read also this discussion - perhaps, you will find there something useful for you.

  • With the help of iionly I've developed a plugin that works well with Amazon ses, for elgg 2.3.14

    All the relevant code is in start.php (below). Also, you need to install the appropriate Amazon AWS sdk.

    Hope it help you.

    <?php
    require_once(dirname(__FILE__) . '/Amazon-AWS-SDK/aws-autoloader.php');
    use Aws\Ses\SesClient;
    use Aws\Ses\Exception\SesException;
     
    elgg_register_event_handler('init', 'system', 'amazon_ses_init');
     
    function amazon_ses_init() {
       elgg_register_plugin_hook_handler('email', 'system', 'amazon_ses_send_email');
    }
     
    function amazon_ses_send_email($hook, $type, $returnvalue, $params) {
        if (!is_array($returnvalue) || !is_array($returnvalue['params'])) {
            return;
        }
     
        $to_address = '';
        $contact = $returnvalue['to'];
        $containsName = preg_match('/<(.*)>/', $contact, $matches) == 1;
        if ($containsName) {
            $to_address = $matches[1];
        } else {
            $to_address = $contact;
        }
     
        if (!$to_address) {
            return $returnvalue;
        }
     
        $users = get_user_by_email($to_address);
        $recipient = $users[0];
     
        if (!($recipient instanceof ElggUser)) {
            return $returnvalue;
        }
        
        $subject = $returnvalue['subject'];
        $from = $returnvalue['from'];
        
        $site_url = elgg_get_site_url();
    $notification_settings_url = $site_url . '/notifications/personal/' . $recipient->username;
    $group_notification_settings_url =  $site_url . '/notifications/group/' . $recipient->username;
    $recipient_language = ($recipient->language) ? $recipient->language : (($site_language = elgg_get_config('language')) ? $site_language : 'en');
        
        $originalMessage = $returnvalue['body'];
        /** ===== Prepare the HTML message ====== */
    $messageHTML = $originalMessage.elgg_echo('amazon_ses:content', [$to_address, $site_url, $notification_settings_url, $group_notification_settings_url], $recipient_language);
        /** ===== Prepare the TEXT message ======
         * ======= remove HTML tags =============== */
        $messageTEXT = preg_replace('/<.*?>/s', '', $messageHTML);
        
        /** ===== replace non-breaking space with regular space =============== */
        $messageTEXT = preg_replace('/&nbsp;/', ' ', $messageTEXT);
      /** ===== Amazon SES Starts ===== */  
        $client = SesClient::factory([
            'version'=> 'latest',
            'region' => 'us-east-1'
        ]);
        $charset = 'UTF-8';
     
    try {
    if(preg_match('/<.*?>/s',$originalMessage)){// == if there are HTML tags send HTML and TEXT emails
         $result = $client->sendEmail([
            'Destination' => [
                'ToAddresses' => [
                    $to_address,
                ],
            ],
            'Message' => [
                'Body' => [
                    'Html' => [
                        'Charset' => $charset,
                        'Data' => $messageHTML,
                    ],
                    'Text' => [
                        'Charset' => $charset,
                        'Data' => $messageTEXT,
                    ],
                ],
                'Subject' => [
                    'Charset' => $charset,
                    'Data' => $subject,
                ],
            ],
        'Source' => $from, 
      ]);  
      }else{// === If no HTML tags - send only TEXT email
        
            $result = $client->sendEmail([
            'Destination' => [
                'ToAddresses' => [
                    $to_address,
                ],
            ],
            'Message' => [
                'Body' => [
                    'Text' => [
                        'Charset' => $charset,
                        'Data' => $messageTEXT,
                    ],
                ],
                'Subject' => [
                    'Charset' => $charset,
                    'Data' => $subject,
                ],
            ],
        'Source' => $from, 
      ]);
        
      }
            $messageId = $result->get('MessageId');
     
            return true;
        } catch (SesException $error) {
            $errorMessage = $error->getAwsErrorMessage();
            
            $message .= $messageTEXT.' ..... send by mail.';
     
            $headers = "From: $from"; 
            if(mail($to_address,$subject,$message,$headers)!==false){
                return true;
            }               
        }
        return $returnvalue;
    }
  • @Nikolai: tried all those permutations and combinations before posting the ticket :)

    @Reuven: Thanks a lot for that. Let me give it a try.

  • @Reuven: Thanks for that script. That was a good starting point for me. I modified this and used Cash's php mailer plugin too to connect to SES via smtp. Now its working in Elgg 2.3 branch.

  • I have question on the same matter. I made necessary changes on settings.php but it didn't solve the issue.
    It says "Laminas\Mail\Transport\Exception\RuntimeException"
    I wrote my hosting server about the issue. They replied that it is seen that sending is attempted with the mail() function on the log record. The PHP mailer function is disabled for security reasons. You can use your forms stably by revising your software as SMTP.
    So where or how can I unable mail() function and direct my elgg to smtp settings?

  • @Fazli

    Have you set/enabled this setting https://github.com/Elgg/Elgg/blob/4.x/elgg-config/settings.example.php#L418

    I think you forgot to tell the system to use 'smtp' (as the default is 'sendmail')

    And all the correct information is filled in here https://github.com/Elgg/Elgg/blob/4.x/elgg-config/settings.example.php#L432-L444

  • You're righ Jerome.

    I missed the double slash (//) before the line "$CONFIG->emailer_transport = 'smtp';"

    Thanks for your interest. Now it's ok.