codebaby/email

General purpose email sending module

v1.0.0 2021-04-11 13:27 UTC

This package is auto-updated.

Last update: 2024-04-11 20:11:27 UTC


README

Magento 2 module that provides a generic class that can be used to send emails.

Emails can be sent using AJAX (or any other REST Api strategy) and also can be sent from the php side.

Install instructions:

Install it via composer:

composer require codebaby/email

After installation, run the following commands:

bin/magento mod:en CodeBaby_Email

bin/magento s:up

bin/magento s:d:c

bin/magento c:f

Parameters Definition

sendTo (Array) - Must contain 'email' key

sender (Array) - Must contain 'email' and 'name' keys

templateVars (Array) - Vars that will be rendered in email template, must be $keyName => $value, inside email template can be called as {{var keyName}}

subject (null|String) - Used when there is no defined template

areaCode (null|string) - Normally 'adminhtml'|'frontend'. Default is 'frontend'

storeId (null|string|int)

cc (null|Array) Emails that will be in copy

bcc (null|Array) Emails that will be in hidden copy

replyTo (null|String) the email to which the replies will point

template (null|int|string) Can be set as:

  • A system config value, such as sales_email/order/template
  • The string id present on the etc/email_templates.xml file, such as: codebaby_default_email
  • When email template is generated by the Back Office / Admin, must be the template numeric ID
  • Defaults to: codebaby_default_email

USAGE EXAMPLES:

Example 1: Sending 1 Email Using API (JQUERY AJAX):

const url = "https://demourl/rest/V1/cb/send";
const data = JSON.stringify({
    "sendTo": {
        "email": "sendto@demoemail.com"
    },
    "sender": {
        "email": "sender@demoemail.com",
        "name": "Sender Name"
    },
    "templateVars": {
        "test1": "Test Var",
        "test2": "Test Var 2"
    },
    "subject": "Custom Subject",
    "areaCode": 'frontend',
    "storeId": null,
    "cc": [
        "emailcc@demoemail.com", 
        "emailcc2@demoemail.com"
    ],
    "bcc": [
        "emailbcc@demoemail.com",
        "emailbcc2@demoemail.com"
    ],
    "replyTo": 'replyto@demoemail.com',
    "template": null
});
const settings = {
    "async": true,
    "crossDomain": true,
    "url": url,
    "method": "POST",
    "timeout": 0,
    "headers": {
        "Content-Type": "application/json",
    },
    "data": data
};

$.ajax(settings).done(function (response) {
  console.log(response);
});

Example 2: Sending 2 Emails Using API (AJAX):

const url = "https://demourl/rest/V1/cb/send";
let data = {
    "sendTo": {
        "email": "sendto@demoemail.com"
    },
    "sender": {
        "email": "sender@demoemail.com",
        "name": "Sender Name"
    },
    "templateVars": {
        "test1": "Test Var",
        "test2": "Test Var 2"
    },
    "subject": "Custom Subject",
    "areaCode": 'frontend',
    "storeId": null,
    "cc": [
        "emailcc@demoemail.com", 
        "emailcc2@demoemail.com"
    ],
    "bcc": [
        "emailbcc@demoemail.com",
        "emailbcc2@demoemail.com"
    ],
    "replyTo": 'replyto@demoemail.com',
    "template": null
};
const settingsFirstEmail = {
    "async": true,
    "crossDomain": true,
    "url": url,
    "method": "POST",
    "timeout": 0,
    "headers": {
        "Content-Type": "application/json",
    },
    "data": JSON.stringify(data)
};
data.sendTo.email = 'newemailreceiver@demo.com';
data.bcc = null;
data.cc = null;
const settingsSecondEmail = {
    "async": true,
    "crossDomain": true,
    "url": url,
    "method": "POST",
    "timeout": 0,
    "headers": {
        "Content-Type": "application/json",
    },
    "data": JSON.stringify(data)
};
$.when($.ajax(settingsFirstEmail), $.ajax(settingsSecondEmail))
    .then(emailSent, emailFail);

/**
 * This will be triggered after sendind the 2 emails and only if 
 * both are successfull
 *
 * @param resFirstEmail
 * @param resSecondEmail
 */
function emailSent(resFirstEmail, resSecondEmail) {
    console.log(resFirstEmail);
    console.log(resFirstEmail[0].success);
    console.log(resSecondEmail);
    console.log(resSecondEmail[0].success);
    //even if request is successfull, some errors may come here
    //make sure you are listening to the value in res.success and res.error
    //in order to handle errors properly
}

/**
 * This will be triggered after sendind the 2 emails and only if
 * any of them fails
 *
 * @param resFirstEmail
 * @param resSecondEmail
 */
function emailFail(resFirstEmail, resSecondEmail) {
    console.log(resFirstEmail);
    console.log(resSecondEmail);
}

Example 3: Sending Email Using PHP Demo Controller:

<?php

namespace CodeBaby\Email\Controller\CbDemo;

use CodeBaby\Email\Model\Service\MailService;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\JsonFactory;

class Index implements ActionInterface, HttpGetActionInterface, HttpPostActionInterface
{
    private MailService $mailService;
    private JsonFactory $jsonFactory;

    public function __construct(MailService $mailService, JsonFactory $jsonFactory)
    {
        $this->mailService = $mailService;
        $this->jsonFactory = $jsonFactory;
    }

    public function execute()
    {
        $sendTo = [ 'email' => 'sendto@demoemail.com' ];
        $sender = [
            "email" => "sender@demoemail.com",
            "name" => "Sender Name"
        ];
        $templateVars = [
            "test1" => "Test",
            "test2" => "Test 2"
        ];
        $subject = "Custom Subject";
        $areaCode = "frontend";
        $storeId = '0';
        $cc = [
            "emailcc@demoemail.com",
            "emailcc2@demoemail.com"
        ];
        $bcc = [
            "emailbcc@demoemail.com",
            "emailbcc2@demoemail.com"
        ];
        $replyTo = 'replyto@demoemail.com';
        $template = null;

        $result = $this->mailService->handle(
            $sendTo,
            $sender,
            $templateVars,
            $subject,
            $areaCode,
            $storeId,
            $cc,
            $bcc,
            $replyTo,
            $template
        );
        $json = $this->jsonFactory->create();
        $json->setData($result);
        return $json;
    }
}