PHPackages                             codebaby/email - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. codebaby/email

ActiveMagento2-module[Mail &amp; Notifications](/categories/mail)

codebaby/email
==============

General purpose email sending module

v1.0.0(5y ago)181OSL-3.0PHPPHP ~7.0.0|~7.1.0|~7.2.0|~7.3.0|~7.4.0

Since Apr 11Pushed 5y ago2 watchersCompare

[ Source](https://github.com/caduhigueras/magento2-send-email)[ Packagist](https://packagist.org/packages/codebaby/email)[ RSS](/packages/codebaby-email/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Codebaby Email for Magento2
===========================

[](#codebaby-email-for-magento2)

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-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
=====================

[](#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 =&gt; $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:
===============

[](#usage-examples)

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

[](#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):
=============================================

[](#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:
===================================================

[](#example-3-sending-email-using-php-demo-controller)

```
