6

In Magento 2, the username and password are no longer set in template.php under Email module. How can I use Gmail SMTP for email sending in Magento 2? How to configure it? Thanks

9

I found an useful extension GmailSmtpApp and followed the Transport.php to configure setting for Gmail SMTP service.

Be careful that, the ssl should be set to 'SSL', if it is set to tls, it will not work!

<?php
/**
 * Mail Transport
 * Copyright © 2015 MagePal. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace MagePal\GmailSmtpApp\Model;



class Transport extends \Zend_Mail_Transport_Smtp implements \Magento\Framework\Mail\TransportInterface
{
    /**
     * @var \Magento\Framework\Mail\MessageInterface
     */
    protected $_message;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $_scopeConfig;

    /**
     * @param MessageInterface $message
     * @param null $parameters
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @throws \InvalidArgumentException
     */
    public function __construct(\Magento\Framework\Mail\MessageInterface $message, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
    {
        if (!$message instanceof \Zend_Mail) {
            throw new \InvalidArgumentException('The message should be an instance of \Zend_Mail');
        }

        $this->_scopeConfig = $scopeConfig;

         $smtpHost = $this->_scopeConfig->getValue('system/gmailsmtpapp/smtphost', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
         $smtpConf = array(
            'auth' => $this->_scopeConfig->getValue('system/gmailsmtpapp/auth', \Magento\Store\Model\ScopeInterface::SCOPE_STORE),
            // 'ssl' => $this->_scopeConfig->getValue('system/gmailsmtpapp/ssl', \Magento\Store\Model\ScopeInterface::SCOPE_STORE),
            'ssl' => 'SSL',
            'username' => $this->_scopeConfig->getValue('system/gmailsmtpapp/username', \Magento\Store\Model\ScopeInterface::SCOPE_STORE),
            'password' => $this->_scopeConfig->getValue('system/gmailsmtpapp/password', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
         );
        parent::__construct($smtpHost, $smtpConf);
        $this->_message = $message;


    }

    /**
     * Send a mail using this transport
     *
     * @return void
     * @throws \Magento\Framework\Exception\MailException
     */
    public function sendMessage()
    {
        try { 
            parent::send($this->_message);
        } catch (\Exception $e) {
            error_log($e);
            throw new \Magento\Framework\Exception\MailException(new \Magento\Framework\Phrase($e->getMessage()), $e);
        }
    }
}

Also, you can reference this article, it may help you.

  • I implemented that module, used it with SendGrid and the test mail works, but how do I implement in my own module? I want to send an email from my own module but I can't find documentation on how to implement it anywhere. How did you do it? – Jacques Dec 19 '16 at 14:34
  • @JaccoAmersfoort ... to send emails from your own module all you need to do is magento.stackexchange.com/questions/132391/… – Renon Stewart Nov 3 '17 at 16:33
4

I will suggest you to use Emizentech_CustomSmtp extension and it's work great for me. You can us this extension from 'https://github.com/emizentech/magento2-custom-smtp'. I hope it will also help you.

  • repo link no longer available – Xenocide8998 Aug 1 '17 at 15:33
0

To setup SMTP you can follow these simple steps:

Step 1: Install SMTP

Run the following command in Magento 2 root folder

composer require mageplaza/module-smtp
php bin/magento setup:upgrade

It will automatically install SMTP module

Step 2: Configuration

Still from the same structure with General Configuration, scroll down to see SMTP Configuration Options.

smtp configuration

  • In SMTP Provider field, at the moment we support provider nearly 30 SMTP email service providers so feel free to choose your appropriate provider. Click Auto Fill button to fill Host, Port, Authentication and Protocol automatically, which are compatible with the SMTP provider you had chosen.

  • At Host field, type your Support Host name and ID Address. You can also custom STMP Provider’s Host name at here. If you had clicked Auto fill button at the above field, you can give this step a free pass.

  • Port is a specific gate where emails will be sent through. You can also pass this step if you had choose Auto fill from the first place. In general, there will be 3 kinds of Default Port

    • Port 25: Emails sent by other Protocol which different SSL will be sent through this portal
    • Port 465: Emails sent by other Protocol SSL will be sent through this portal
    • Port 578: Emails sent by other Protocol TLS will be sent through this portal
  • Authentication field is place where you decide an authentication method. If you hadn’t clicked Auto fill button before, please note those basic methods

    • Login: Authentication by login to the account through Username and Password that will be filled in the next field. Most of provider will require this method.
    • Plain
    • CRAM-MD5
  • Account: where you enter the account name matching format of the SMTP Provider you had selected
  • Password: password of the Username. After saving, the password will be encrypted into ******
  • Protocol: pass this step if you had chosen Auto fill, or you can select one of the providing protocol below here
    • None: when you select this protocol, you have to accept all the risk may occur in the process of sending.
    • SSL stands for Secure Socket Layer. This protocol ensures that all data exchanged between the web server and the browser is secure and stay safe.
    • TLS means Transport Layer Security. This protocol secures data or messages and validates the integrity of messages through message authentication codes.
  • Return-path email: leave it empty if you want to ignore this.
  • Test email recipient: This is the field for you to test the operation of the extension. After filling all fields, click Test Now button. If the information entered is valid, a successful email notification will be sent from Username to Email Test. That email will have the following content:

smtp test result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.