﻿![]()

#### 

#### About

In previous [article](https://www.brimit.com/blog/sitecore-send-3-rest-api) we reviewed **Sitecore Send REST API**. So now let's use Sitecore Send together with Sitecore Forms using this API.
Check out my [presentation on SUG Belarus](https://youtu.be/-9DZZ-kWNpc) for more details about this integration.

#### Integration

How Sitecore Send and Sitecore Forms can be integrated? I see the following ways:

- To send emails from Sitecore Platform to customers via [Moosend SMPTP](https://help.moosend.com/hc/en-us/articles/4405943965970-Enable-SMTP-to-send-transactional-emails)
- Create customer in Moosend after form is submitted.

![Sitecore Forms Integration Scheme](https://www.brimit.com/-/media/images/blog/sitecore-send-4-forms-integration/integration.png?h=682&amp;w=2032&amp;hash=8811277E51A937BA4520157CEC9DDC4D)

#### Moosend SMTP

You just need to patch Sitecore configuration with the following file:

```
<configuration>
    <sitecore>
        <settings>
            <!-- Moosend SMTP settings -->
            <setting name="MailServer" value="smtp.mailendo.com"/>
            <setting name="MailServerUserName" value="YOU ACCOUNT EMAIL"/>
            <setting name="MailServerPassword" value="YOU ACCOUNT PASSWORD"/>
            <setting name="MailServerPort" value="25"/>
            <setting name="MailServerUseSsl" value="false"/>
        </settings>
    </sitecore>
</configuration>
```

`MailServerUserName` and `MailServerPassword` are your Sitecore Send account email and password.

After patch you're able to send emails through Moosend SMTP:

```
using (MailMessage message = new MailMessage())
{
    message.From = ...;
    message.Subject = ...;
    message.Body = ...;
    message.IsBodyHtml = true;
    message.To.Add(...);
    message.CC.Add(...);
    message.Bcc.Add(...);
    MainUtil.SendMail(message);
}
```

Also OOTB with Sitecore Forms comes `Send Email` submit action, which internally uses configured SMTP - so this submit action can also be used in your forms.

#### `Add User To Mailing List` Submit Action

Submit action works in the following way.

**Form builder**

1. User specifies the mailing list from dropdown, where subscriber should be added
2. Fields mapping is configured (custom fields in Moosend to Sitecore Form fields)

See the demo:
![Editor Demo](https://www.brimit.com/-/media/images/blog/sitecore-send-4-forms-integration/editor-demo.gif)

**Submit action**

1. Read parameters: list, fields mapping
2. Map form data to model
3. Add customer to mailing list via [REST API](https://www.brimit.com/blog/sitecore-send-3-rest-api)

##### API Key

**API key** (`Account Settings` -&gt; `API Key` in Moosend) needs to be obtained. See [this article](https://help.moosend.com/hc/en-us/articles/4403735862674-Find-the-API-key-of-your-account) for more details.

DI registration:

```
serviceCollection.AddSingleton<IMoosendSettings>((pr) =>
{
    var settings = pr.GetService<BaseSettings>();
    return new MoosendSettings()
    {
        ApiKey = settings.GetSetting("Moosend.ApiKey"),
    };
});
```

Usage (see [MailingListService.cs](https://github.com/izharikov/sitecore-moosend-website/blob/master/src/Foundation/Moosend/website/Services/MailingListService.cs)):

```
public MailingListService(IMoosendSettings moosendSettings, ...)
{
    _moosendSettings = moosendSettings;
    ...
}

public IEnumerable<MailingListJsonModel> GetAll()
{
    var response =
        _mailingListsApi.GettingAllActiveMailingLists(_moosendSettings.Format, _moosendSettings.ApiKey);
    ...
}
```

##### Submit Action Editor

1. [Editor structure](https://github.com/izharikov/sitecore-moosend-website/tree/master/src/Foundation/Moosend/serialization/Moosend%20Add%20To%20List)
2. [JS Control](https://github.com/izharikov/sitecore-moosend-website/blob/master/src/Foundation/Moosend/website/sitecore/shell/client/Applications/FormsBuilder/Layouts/Actions/MoosendAddToList.js)
3. [Service API Controller](https://github.com/izharikov/sitecore-moosend-website/blob/master/src/Foundation/Moosend/website/Controllers/MoosendServiceApiController.cs). Don't forget to register controller in DI:

    ```
     serviceCollection.AddTransient<MoosendServiceApiController>();
    ```

##### Submit Action

Form parameters:

```
public class AddUserToMailingListData
{
    public string List { get; set; }
    public IDictionary<string, string> Mapping { get; set; } = new Dictionary<string, string>();
}
```

[AddUserToMailingList.cs](https://github.com/izharikov/sitecore-moosend-website/blob/master/src/Foundation/Moosend/website/SubmitActions/AddUserToMailingList.cs).

```
protected override bool Execute(AddUserToMailingListData data, FormSubmitContext formSubmitContext)
{
    var list = data.List;

    // some validation here

    // get service:
    var service = ServiceLocator.ServiceProvider.GetService<IMailingListService>();

    // mapping:
    var name = GetFieldValue(formSubmitContext, nameFieldId) as string;
    var tags = GetFieldValue(formSubmitContext, tagsFieldId) as IEnumerable<string>;
    var customFields = GetCustomFields(data, formSubmitContext);

    // add subscriber to mailing list
    var success = service.AddToMailingList(list, email, name, customFields, tags);
    return success;
}
```

#### How it all works together

Check out this [video](https://youtu.be/-9DZZ-kWNpc?t=2391) (it's already with demo timecode).

#### Conclusion

With this integration the following features are available:

- SMTP integration
- Dynamic form and properties mapping

All sources are available on my [Git Repo](https://github.com/izharikov/sitecore-moosend-website).

###### Author

[!\[Igor Zharikov - Senior Sitecore Developer, Sitecore MVP\](https://www.brimit.com/-/jssmedia/feature/blogs/authors/igor-zharikov-2.jpg?h=700&amp;iar=0&amp;w=700&amp;hash=292A84E8CB3E5CE57CA743385CFB1464)
Igor Zharikov
Sitecore MVP / Senior Sitecore Developer](https://www.brimit.com/blog/author?authors=Igor%20Zharikov)

#### More on Sitecore

[!\[How Vercel Will Help You Save Effort When Deploying Sophisticated Sitecore Projects\](https://www.brimit.com/-/jssmedia/project/brimit/blog/2024/vercel_cover-image.png)
#Guides#How-toDXPE-commerce
##### How Vercel Will Help You Save Effort When Deploying Sophisticated Sitecore Projects
Optimize and accelerate the development and deployment of complex multisite Sitecore projects.
Alexei Vershalovich on July 17, 2024](https://www.brimit.com/blog/how-vercel-will-help-you-save-effort-when-deploying-sophisticated-sitecore-projects)

[!\[Training Up Tomorrow's Sitecore MVPs: a Mentoring Success Story\](https://www.brimit.com/-/jssmedia/project/brimit/blog/2023/sitecore-mentoring---cover-image.png)
#How-toDXP
##### Training Up Tomorrow's Sitecore MVPs: a Mentoring Success Story
How to participate in the Sitecore Mentor program and help younger colleagues jump-start a career in Sitecore development.
Sergey Baranov on October 2, 2023](https://www.brimit.com/blog/training-up-tomorrows-sitecore-mvps)

[!\[Going Headless. Part 2: When a Headless CMS Is Your Best Bet (if you have Sitecore)\](https://www.brimit.com/-/jssmedia/project/brimit/blog/2022/headless/adobestock_456986731.jpg)
#How-toDXPE-commerce
##### Going Headless. Part 2: When a Headless CMS Is Your Best Bet (if you have Sitecore)
Discover how a headless CMS can benefit organizations that use Sitecore.
Daniil Raschupkin, Palina Trokhautsava on September 15, 2022](https://www.brimit.com/blog/going-headless-part-2-when-a-headless-cms-is-your-best-bet-if-you-have-sitecore)

![](https://bat.bing.net/action/0?ti=187017043&amp;tm=gtm002&amp;Ver=2&amp;mid=5d057f29-1bd0-41e4-b0c8-4e7570b4dd3a&amp;bo=2&amp;gtm_tag_source=1&amp;pi=0&amp;lg=en-US&amp;sw=800&amp;sh=600&amp;sc=24&amp;nwd=1&amp;tl=Sitecore%20Send%20Integration%20with%20Sitecore%20Forms&amp;kw=sitecore%20send,moosend,sitecore%20forms,integration,sitecore&amp;p=https%3A%2F%2Fwww.brimit.com%2Fblog%2Fsitecore-send-4-forms-integration&amp;r=&amp;lt=285&amp;evt=pageLoad&amp;sv=2&amp;asc=D&amp;cdb=AQAY&amp;rn=346859)