Skip to content

Instantly share code, notes, and snippets.

@ry8806
Last active May 8, 2021 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ry8806/8c54c7ad455443dc4589f3f4991d0979 to your computer and use it in GitHub Desktop.
Save ry8806/8c54c7ad455443dc4589f3f4991d0979 to your computer and use it in GitHub Desktop.
Elastic Email + JavaScript
<form id="subscribeForm">
<input id="email-input" type="text" />
<button type="submit">Submit</button>
</form>
function SetupNewsletterSubscribe(publicAccountId, listName, formId, onSuccess) {
var eeUrl = "https://api.elasticemail.com/v2/contact/add";
var email = jQuery("#email-input");
var form = jQuery("#" + formId).submit(function (event) {
event.preventDefault();
jQuery.post(eeUrl, {
email: email.val(),
publicAccountId: publicAccountId,
listName: listName
}, function () { }, "json").done(function (result) {
if (result.success == true) {
onSuccess();
}
}).fail(function () {
// Do something here if it fails...
});
});
};
// DOM Ready, call the function defined above with parameters
jQuery(function () {
SetupNewsletterSubscribe("**MY_PUBLIC_ACCOUNT_ID**", "NameOfEmailList", "subscribeForm", function () {
// Do stuff here when the email address has been added to the service!
});
});
@justoverclockl
Copy link

how can i use a variable here:

SetupNewsletterSubscribe("my variable*"

@ry8806
Copy link
Author

ry8806 commented May 7, 2021

you could do something like:

var myAccountId = "this-is-my-account-id"

and then:

// DOM Ready, call the function defined above with parameters
jQuery(function () {
    SetupNewsletterSubscribe(myAccountId, "NameOfEmailList", "subscribeForm", function () {
        // Do stuff here when the email address has been added to the service!
    });
});

@justoverclockl
Copy link

justoverclockl commented May 8, 2021

this actually does not send any email (obviously i've replaced the id with my public id, any help on this? maybe a javascript version instead of jquery?

  function SetupNewsletterSubscribe(publicAccountID, listName, formId, onSuccess) {
    var eeUrl = "https://api.elasticemail.com/v2/contact/add";
    var email = jQuery("#email-input");
    var form = jQuery("#" + formId).submit(function (event) {
      event.preventDefault();
      jQuery.post(eeUrl, {
        email: email.val(),
        publicAccountId: publicAccountID,
        listName: listName
      }, function () {
      }, "json").done(function (result) {
        if (result.success == true) {
          onSuccess();

        }
      }).fail(function () {
        // Do something here if it fails...
      });
    });
  };

// DOM Ready, call the function defined above with parameters
  jQuery(function () {
    SetupNewsletterSubscribe("my-id", "Flarumsub", "subscribeForm", function () {

    });
  });

@ry8806
Copy link
Author

ry8806 commented May 8, 2021

Correct, this doesn't send emails and it's not meant to.

This simply adds the given email address to your list of contacts in Elastic Email, under the specified list (from listName)

@justoverclockl
Copy link

justoverclockl commented May 8, 2021

oh that's the reason, anyway it works fine, just the last question, how to clear field after submission?
now i'm using
document.getElementById("subscribeForm").reset();

@ry8806
Copy link
Author

ry8806 commented May 8, 2021

$('#email-input').val('');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment