├── .gitignore ├── registration.php ├── etc ├── di.xml ├── module.xml ├── adminhtml │ ├── routes.xml │ └── system.xml └── config.xml ├── Model ├── Config │ └── Source │ │ └── Authtype.php └── Transport.php ├── composer.json ├── README.md ├── view └── adminhtml │ └── templates │ └── system │ └── config │ └── testbutton.phtml ├── Block └── Adminhtml │ └── System │ └── Config │ └── TestButton.php ├── Helper └── Data.php └── Controller └── Adminhtml └── Test └── Index.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.buildpath 2 | /.cache 3 | /.metadata 4 | /.project 5 | /.settings 6 | atlassian* 7 | /nbproject 8 | /sitemap 9 | /.idea 10 | /vendor 11 | -------------------------------------------------------------------------------- /registration.php: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /etc/module.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /etc/adminhtml/routes.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /Model/Config/Source/Authtype.php: -------------------------------------------------------------------------------- 1 | 'ssl', 'label' => 'SSL (SendGrid)'], 17 | ['value' => 'tls', 'label' => 'TLS (SendGrid)'] 18 | ]; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yukoff/magento2-sendgrid-smtp", 3 | "description":"Configure Magento 2 to use SendGrid SMTP server to send email", 4 | "keywords": [ 5 | "magento2", 6 | "magento 2", 7 | "email", 8 | "smtp", 9 | "sendgrid" 10 | ], 11 | "require": { 12 | "php": "~5.5.0|~5.6.0|~7.0.0", 13 | "magento/module-backend": "100.0.*", 14 | "magento/framework": "100.0.*" 15 | }, 16 | "type": "magento2-module", 17 | "version": "0.2.1", 18 | "license": [ 19 | "OSL-3.0", 20 | "AFL-3.0" 21 | ], 22 | "autoload": { 23 | "files": [ 24 | "registration.php" 25 | ], 26 | "psr-4": { 27 | "SendGrid\\SendGridSmtp\\": "" 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /etc/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 11 | 12 | 13 | ssl 14 | LOGIN 15 | smtp.sendgrid.net 16 | 1 17 | 1 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SendGrid SMTP Plugin for Magento2 2 | Configure Magento to use SendGrid's SMTP server to send all transactional emails. 3 | 4 | *Note:* You need a valid SendGrid account. 5 | 6 | ## 1. Install SendGrid SMTP Plugin 7 | ### Using Composer 8 | ``` 9 | composer require yukoff/magento2-sendgrid-smtp 10 | ``` 11 | 12 | ### Using Source Code 13 | ``` 14 | composer config repositories.yukoff-sendgridsmtp git git@github.com:yukoff/magento2-sendgrid-smtp.git 15 | composer require yukoff/magento2-sendgrid-smtp 16 | ``` 17 | 18 | ### Manual Installation 19 | Install SendGrid SMTP plugin for Magento2 20 | * Download the extension 21 | * Unzip the file 22 | * Create a folder {MAGENTO_ROOT}/app/code/SendGrid/SendGridSmtp 23 | * Copy the content from the unzipped folder 24 | 25 | ## 2. Enable SendGrid SMTP Plugin 26 | ``` 27 | php -f bin/magento module:enable --clear-static-content SendGrid_SendGridSmtp 28 | php -f bin/magento setup:upgrade 29 | ``` 30 | 31 | ## 3. Config SendGrid SMTP Plugin 32 | Log into your Magetno2 backend, then goto `Store -> System -> SendGrid SMTP` and enter your email credentials, SendGrid category (if used) etc. 33 | -------------------------------------------------------------------------------- /view/adminhtml/templates/system/config/testbutton.phtml: -------------------------------------------------------------------------------- 1 | helper('Magento\Framework\Json\Helper\Data'); 14 | ?> 15 | 16 | 48 | 49 | getButtonHtml() ?> 50 | -------------------------------------------------------------------------------- /Block/Adminhtml/System/Config/TestButton.php: -------------------------------------------------------------------------------- 1 | _urlBuilder = $context->getUrlBuilder(); 27 | parent::__construct($context, $data); 28 | } 29 | 30 | /** 31 | * Set template 32 | * 33 | * @return void 34 | */ 35 | protected function _construct() 36 | { 37 | parent::_construct(); 38 | $this->setTemplate('SendGrid_SendGridSmtp::system/config/testbutton.phtml'); 39 | } 40 | 41 | /** 42 | * Generate button html 43 | * 44 | * @return string 45 | */ 46 | public function getButtonHtml() 47 | { 48 | $button = $this->getLayout()->createBlock( 49 | 'Magento\Backend\Block\Widget\Button' 50 | )->setData( 51 | [ 52 | 'id' => 'sendgridsmtp_debug_result_button', 53 | 'label' => __('Send Test Email'), 54 | 'onclick' => 'javascript:sendgridSmtpDebugTest(); return false;', 55 | ] 56 | ); 57 | 58 | return $button->toHtml(); 59 | } 60 | 61 | public function getAdminUrl(){ 62 | return $this->_urlBuilder->getUrl('sendgridSendGridSmtp/test', ['store' => $this->_request->getParam('store')]); 63 | } 64 | 65 | /** 66 | * Render button 67 | * 68 | * @param \Magento\Framework\Data\Form\Element\AbstractElement $element 69 | * @return string 70 | */ 71 | public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element) 72 | { 73 | // Remove scope label 74 | $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue(); 75 | return parent::render($element); 76 | } 77 | 78 | /** 79 | * Return element html 80 | * 81 | * @param \Magento\Framework\Data\Form\Element\AbstractElement $element 82 | * @return string 83 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) 84 | */ 85 | protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element) 86 | { 87 | return $this->_toHtml(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /Model/Transport.php: -------------------------------------------------------------------------------- 1 | getConfigSetReturnPath(); 34 | switch ($setReturnPath) { 35 | case 1: 36 | $returnPathEmail = $message->getFrom(); 37 | break; 38 | case 2: 39 | $returnPathEmail = $dataHelper->getConfigReturnPathEmail(); 40 | break; 41 | default: 42 | $returnPathEmail = null; 43 | break; 44 | } 45 | 46 | if ($returnPathEmail !== null && $dataHelper->getConfigSetReturnPath()) { 47 | $message->setReturnPath($returnPathEmail); 48 | } 49 | 50 | if ($message->getReplyTo() === NULL && $dataHelper->getConfigSetReplyTo()) { 51 | $message->setReplyTo($returnPathEmail); 52 | } 53 | 54 | // set SendGrid category 55 | if ($dataHelper->getConfigCategory()) { 56 | $xSmtpApiHeader = [ 57 | 'category' => $dataHelper->getConfigCategory() 58 | ]; 59 | // FIXME: X-SMTPAPI used in several places, make it a constant instead 60 | $message->addHeader('X-SMTPAPI', $jsonHelper->jsonEncode($xSmtpApiHeader)); 61 | } 62 | 63 | // set config 64 | $smtpConf = [ 65 | 'auth' => strtolower($dataHelper->getConfigAuth()), 66 | 'ssl' => $dataHelper->getConfigSsl(), 67 | 'username' => $dataHelper->getConfigUsername(), 68 | 'password' => $dataHelper->getConfigPassword() 69 | ]; 70 | 71 | $smtpHost = $dataHelper->getConfigSmtpHost(); 72 | parent::__construct($smtpHost, $smtpConf); 73 | $this->_message = $message; 74 | } 75 | 76 | /** 77 | * Send mail using this transport 78 | * 79 | * @return void 80 | * @throws \Magento\Framework\Exception\MailException 81 | */ 82 | public function sendMessage() 83 | { 84 | try { 85 | parent::send($this->_message); 86 | } catch (\Exception $e) { 87 | throw new \Magento\Framework\Exception\MailException(new \Magento\Framework\Phrase($e->getMessage()), $e); 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /Helper/Data.php: -------------------------------------------------------------------------------- 1 | 8 | * @copyright yukoff (https://github.com/yukoff) 9 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 10 | */ 11 | 12 | namespace SendGrid\SendGridSmtp\Helper; 13 | 14 | class Data extends \Magento\Framework\App\Helper\AbstractHelper 15 | { 16 | 17 | /** 18 | * @param \Magento\Framework\App\Helper\Context $context 19 | * @param \Magento\Framework\ObjectManagerInterface 20 | */ 21 | public function __construct( 22 | \Magento\Framework\App\Helper\Context $context 23 | ) 24 | { 25 | parent::__construct($context); 26 | } 27 | 28 | /** 29 | * Get system config password 30 | * 31 | * @param \Magento\Store\Model\ScopeInterface::SCOPE_STORE $store 32 | * @return string 33 | */ 34 | public function getConfigPassword($store_id = null){ 35 | return $this->scopeConfig->getValue('system/sendgridsmtp/password', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store_id); 36 | } 37 | 38 | /** 39 | * Get system config username 40 | * 41 | * @param \Magento\Store\Model\ScopeInterface::SCOPE_STORE $store 42 | * @return string 43 | */ 44 | public function getConfigUsername($store_id = null){ 45 | return $this->scopeConfig->getValue('system/sendgridsmtp/username', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store_id); 46 | } 47 | 48 | /** 49 | * Get system config auth type 50 | * 51 | * @param \Magento\Store\Model\ScopeInterface::SCOPE_STORE $store 52 | * @return string 53 | */ 54 | public function getConfigAuth($store_id = null){ 55 | return $this->scopeConfig->getValue('system/sendgridsmtp/auth', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store_id); 56 | } 57 | 58 | /** 59 | * Get system config ssl 60 | * 61 | * @param \Magento\Store\Model\ScopeInterface::SCOPE_STORE $store 62 | * @return string 63 | */ 64 | public function getConfigSsl($store_id = null){ 65 | return $this->scopeConfig->getValue('system/sendgridsmtp/ssl', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store_id); 66 | } 67 | 68 | /** 69 | * Get system config smtp host 70 | * 71 | * @param \Magento\Store\Model\ScopeInterface::SCOPE_STORE $store 72 | * @return string 73 | */ 74 | public function getConfigSmtpHost($store_id = null){ 75 | return $this->scopeConfig->getValue('system/sendgridsmtp/smtphost', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store_id); 76 | } 77 | 78 | /** 79 | * Get system config sendgrid category 80 | * 81 | * @param \Magento\Store\Model\ScopeInterface::SCOPE_STORE $store 82 | * @return string 83 | */ 84 | public function getConfigCategory($store_id = null){ 85 | return $this->scopeConfig->getValue('system/sendgridsmtp/category', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store_id); 86 | } 87 | 88 | /** 89 | * Get system config reply to 90 | * 91 | * @param \Magento\Store\Model\ScopeInterface::SCOPE_STORE $store 92 | * @return bool 93 | */ 94 | public function getConfigSetReplyTo($store_id = null){ 95 | return $this->scopeConfig->getValue('system/sendgridsmtp/set_reply_to', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store_id); 96 | } 97 | 98 | /** 99 | * Get system config set return path 100 | * 101 | * @param \Magento\Store\Model\ScopeInterface::SCOPE_STORE $store 102 | * @return int 103 | */ 104 | public function getConfigSetReturnPath($store_id = null){ 105 | return $this->scopeConfig->getValue('system/sendgridsmtp/set_return_path', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store_id); 106 | } 107 | 108 | /** 109 | * Get system config return path email 110 | * 111 | * @param \Magento\Store\Model\ScopeInterface::SCOPE_STORE $store 112 | * @return string 113 | */ 114 | public function getConfigReturnPathEmail($store_id = null){ 115 | return $this->scopeConfig->getValue('system/sendgridsmtp/return_path_email', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store_id); 116 | } 117 | 118 | } -------------------------------------------------------------------------------- /Controller/Adminhtml/Test/Index.php: -------------------------------------------------------------------------------- 1 | _resultPageFactory = $resultPageFactory; 40 | $this->_dataHelper = $dataHelper; 41 | $this->_jsonHelper = $jsonHelper; 42 | parent::__construct($context); 43 | } 44 | 45 | /** 46 | * Index action 47 | * 48 | * @return \Magento\Backend\Model\View\Result\Page 49 | */ 50 | public function execute() { 51 | 52 | $request = $this->getRequest(); 53 | $store_id = $request->getParam('store', null); 54 | 55 | 56 | $name = 'SendGrid SMTP Plugin Test'; 57 | $username = $request->getPost('username'); 58 | $password = $request->getPost('password'); 59 | 60 | // if default view 61 | // see https://github.com/magento/magento2/issues/3019 62 | if(!$request->getParam('store', false)){ 63 | if(empty($username) || empty($password)){ 64 | $this->getResponse()->setBody(__('Please enter a valid username/password')); 65 | return; 66 | } 67 | } 68 | 69 | // if password mask (6 stars) 70 | $password = ($password == '******') ? $this->_dataHelper->getConfigPassword($store_id) : $password; 71 | 72 | $to = $request->getPost('email') ? $request->getPost('email') : $username; 73 | 74 | // SMTP server configuration 75 | $smtpHost = $request->getPost('smtphost'); 76 | 77 | $smtpConf = array( 78 | 'auth' => strtolower($request->getPost('auth')), 79 | 'ssl' => $request->getPost('ssl'), 80 | 'username' => $username, 81 | 'password' => $password 82 | ); 83 | 84 | // SendGrid category 85 | $category = $request->getPost('category'); 86 | 87 | $transport = new \Zend_Mail_Transport_Smtp($smtpHost, $smtpConf); 88 | 89 | $from = trim($request->getPost('from_email')); 90 | $from = \Zend_Validate::is($from, 'EmailAddress') ? $from : $username; 91 | 92 | //Create email 93 | $mail = new \Zend_Mail(); 94 | $mail->setFrom($from, $name); 95 | $mail->addTo($to, $to); 96 | $mail->setSubject('Hello from SendGrid'); 97 | $mail->setBodyText('Thank you for choosing SendGrid extension.'); 98 | 99 | if ($category) { 100 | $mail->addHeader( 101 | 'X-SMTPAPI', 102 | $this->_jsonHelper->jsonEncode([ 'category' => $category ]) 103 | ); 104 | } 105 | 106 | $result = __('Sent... Please check your email') . ' ' . $to; 107 | 108 | try { 109 | //only way to prevent zend from giving a error 110 | if (!$mail->send($transport) instanceof \Zend_Mail){} 111 | } catch (\Exception $e) { 112 | $result = __($e->getMessage()); 113 | } 114 | 115 | $this->getResponse()->setBody($this->makeClickableLinks($result)); 116 | } 117 | 118 | /** 119 | * Make link clickable 120 | * @param string $s 121 | * @return string 122 | */ 123 | public function makeClickableLinks($s) { 124 | return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '$1', $s); 125 | } 126 | 127 | /** 128 | * Is the user allowed to view the blog post grid. 129 | * 130 | * @return bool 131 | */ 132 | protected function _isAllowed() 133 | { 134 | return $this->_authorization->isAllowed('SendGrid_SendGridSmtp'); 135 | } 136 | 137 | } -------------------------------------------------------------------------------- /etc/adminhtml/system.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | Magento\Config\Model\Config\Source\Email\Smtpauth 16 | Use "login" for SendGrid. 17 | 18 | 19 | 20 | SendGrid\SendGridSmtp\Model\Config\Source\Authtype 21 | Use "ssl" for SendGrid. 22 | 23 | 24 | 25 | Use "smtp.sendgrid.net" for SendGrid. 26 | 27 | 28 | 29 | Email Address. 30 | 31 | 32 | 33 | Magento\Config\Model\Config\Backend\Encrypted 34 | 35 | 36 | 37 | SendGrid documentation for more details.]]> 38 | 39 | 40 | 41 | Magento\Config\Model\Config\Source\Yesno 42 | 43 | 44 | 45 | Magento\Config\Model\Config\Source\Yesnocustom 46 | 47 | 48 | 49 | validate-email 50 | Magento\Config\Model\Config\Backend\Email\Address 51 | 52 | 2 53 | 54 | 55 | 56 | 57 | 0 58 | Magento\Config\Block\System\Config\Form\Fieldset 59 | 60 | 61 | Email address to send test to. 62 | validate-email 63 | 64 | 65 | 66 | Leave blank to use Username instead 67 | validate-email 68 | 69 | 70 | SendGrid\SendGridSmtp\Block\Adminhtml\System\Config\TestButton 71 | 72 | 73 | 74 |
75 |
76 |
77 | --------------------------------------------------------------------------------