# How to send a marketing email programmatically

**URL:** <https://community.hubspot.com/t/how-to-send-a-marketing-email-programmatically/87895>\
**Category:** APIs & Integrations\
**Tags:** professional\
**Created:** [August 21, 2023, 7:36am UTC](https://community.hubspot.com/t/how-to-send-a-marketing-email-programmatically/87895 "2023-08-21T07:36:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmadusanka](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@dmadusanka](https://community.hubspot.com/u/dmadusanka)\
**Post date:** [August 21, 2023, 7:36am UTC](https://community.hubspot.com/t/how-to-send-a-marketing-email-programmatically/87895/1 "2023-08-21T07:36:49Z")

</div>

I want to create a marketing email template in HS and send it programmatically via my web app. And I need to set variables to make the email content dynamic. Can someone suggest a solution for this, please? Highly appreciate your support.

---

<div class="post-metadata">

**Author:** ![coldrickjack](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/coldrickjack/32/6684_2.png) [@coldrickjack](https://community.hubspot.com/u/coldrickjack)\
**Post date:** [August 21, 2023, 9:28am UTC](https://community.hubspot.com/t/how-to-send-a-marketing-email-programmatically/87895/2 "2023-08-21T09:28:35Z")

</div>

Hi @dmadusanka,

Whilst it is possible to send “[transactional](https://knowledge.hubspot.com/email/how-to-use-transactional-email-in-hubspot)” emails via the API (providing you’ve purchased the transactional email add-on) the same does not exist for “marketing” emails. At present there is no method of sending marketing emails via the API with HubSpot. They must be sent manually to individual contacts/lists or automatically using workflows.

One workaround to consider be to programmatically [enroll a contact into a workflow](https://legacydocs.hubspot.com/docs/methods/workflows/add_contact) and ensure the workflow has a send email action within it. You would need to make sure that you are constantly updating the worklfow action to use the appropriate marketing email or set up separate workflows for each campaign.

---

<div class="post-metadata">

**Author:** ![dmadusanka](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@dmadusanka](https://community.hubspot.com/u/dmadusanka)\
**Post date:** [August 23, 2023, 9:29am UTC](https://community.hubspot.com/t/how-to-send-a-marketing-email-programmatically/87895/3 "2023-08-23T09:29:45Z")

</div>

@coldrickjack Thank you so much for your workaround. It seems to be a good solution. I will work on this and let you know the result.

---

<div class="post-metadata">

**Author:** ![dmadusanka](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@dmadusanka](https://community.hubspot.com/u/dmadusanka)\
**Post date:** [August 23, 2023, 10:24am UTC](https://community.hubspot.com/t/how-to-send-a-marketing-email-programmatically/87895/4 "2023-08-23T10:24:15Z")

</div>

@coldrickjack As you suggested, now I am going to create a contact using [this](https://developers.hubspot.com/docs/api/crm/contacts). I created a private app and gave “crm.objects.contacts.write” scope. But when I call create contact API it returns “invalid from object type 0-1 for associations to be created. expected: 0-2”,“correlationId”. Any idea what would be the issue here? The code block I used is given below.

\<?php  
use HubSpot\Factory;  
use HubSpot\Client\Crm\Contacts\ApiException;  
use HubSpot\Client\Crm\Contacts\Model\AssociationSpec;  
use HubSpot\Client\Crm\Contacts\Model\PublicAssociationsForObject;  
use HubSpot\Client\Crm\Contacts\Model\PublicObjectId;  
use HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInputForCreate;

$client = Factory::createWithAccessToken(‘YOUR\_ACCESS\_TOKEN’);

$properties1 = [  
‘company’ =\> ‘Biglytics’,  
‘email’ =\> ‘bcooper@biglytics.net’,  
‘firstname’ =\> ‘Bryan’,  
‘lastname’ =\> ‘Cooper’,  
‘phone’ =\> ‘(877) 929-0687’,  
‘website’ =\> ‘[biglytics.net](http://biglytics.net)’  
];  
$to1 = new PublicObjectId([  
‘id’ =\> ‘101’  
]);  
$associationSpec1 = new AssociationSpec([  
‘association\_category’ =\> ‘HUBSPOT\_DEFINED’,  
‘association\_type\_id’ =\> 2  
]);  
$publicAssociationsForObject1 = new PublicAssociationsForObject([  
‘to’ =\> $to1,  
‘types’ =\> [$associationSpec1]  
]);  
$simplePublicObjectInputForCreate = new SimplePublicObjectInputForCreate([  
‘properties’ =\> $properties1,  
‘associations’ =\> [$publicAssociationsForObject1],  
]);  
try {  
$apiResponse = $client-\>crm()-\>contacts()-\>basicApi()-\>create($simplePublicObjectInputForCreate);  
var\_dump($apiResponse);  
} catch (ApiException $e) {  
echo "Exception when calling basic\_api-\>create: ", $e-\>getMessage();  
}

---

<div class="post-metadata">

**Author:** ![coldrickjack](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/coldrickjack/32/6684_2.png) [@coldrickjack](https://community.hubspot.com/u/coldrickjack)\
**Post date:** [August 24, 2023, 8:07am UTC](https://community.hubspot.com/t/how-to-send-a-marketing-email-programmatically/87895/5 "2023-08-24T08:07:18Z")

</div>

Hi @dmadusanka,

As you are just creating a contact and not associating it to an existing crm record I would remove the part of your code where you are handling hte association itself. i.e this part:

```lia-code-sample
$to1 = new PublicObjectId([
'id' => '101'
]);
$associationSpec1 = new AssociationSpec([
'association_category' => 'HUBSPOT_DEFINED',
'association_type_id' => 2
]);
$publicAssociationsForObject1 = new PublicAssociationsForObject([
'to' => $to1,
'types' => [$associationSpec1]
]);
$simplePublicObjectInputForCreate = new SimplePublicObjectInputForCreate([
'properties' => $properties1,
'associations' => [$publicAssociationsForObject1],
]);

```
