How to send email using MailChimp API
Solution 1
There is no "SendEmail" endpoint in API v3.0. MailChimp's STS was a pre-cursor to its Mandrill transactional service and may only still work for user accounts that have existing STS campaigns. No new STS campaigns can be created. If you have a monthly, paid MailChimp account, you should look into Mandrill. If not, I've had good luck with Mailgun.
Solution 2
You should use HTTP Basic authentication in MailChimp API 3.0.
needle.get('https://<dc>.api.mailchimp.com/3.0/<endpoint>', { username: 'anystring', password: 'your_apikey' },
function(err, resp) {
// your code here...
});
EDIT
@TooMuchPete is right, the SendMail
endpoint is not valid in MailChimp API v3.0. I didn't notice that and I've edited my answer.
Related videos on Youtube
Tay Moore
Updated on September 05, 2022Comments
-
Tay Moore over 1 year
I'm creating an app in nodejs to send an email using MailChimp. I've tried to use https://apidocs.mailchimp.com/sts/1.0/sendemail.func.php but changed it to use 3.0 api because 1.0 seems to no longer work (big surprise). I've setup my app with
var apiKey = '<<apiKey>>', toEmail = '<<emailAddress>>', toNames = '<<myName>>', message = { 'html': 'Yo, this is the <b>html</b> portion', 'text': 'Yo, this is the *text* portion', 'subject': 'This is the subject', 'from_name': 'Me!', 'from_email': '', 'to_email': toEmail, 'to_name': toNames }, tags = ['HelloWorld'], params = { 'apikey': apiKey, 'message': message, 'track_opens': true, 'track_clicks': false, 'tags': tags }, url = 'https://us13.api.mailchimp.com/3.0/SendEmail'; needle.post(url, params, function(err, headers) { if (err) { console.error(err); } console.log(headers); } });
I keep getting a 401 response (not authorized because I'm not sending the API key properly)
I have to use needle due to the constraints on the server.
-
Tay Moore about 8 yearsDoes 'anystring' need to be something specific or can it literally be any string. I've tried this but I'm still getting a 401 error.
-
Tay Moore about 8 yearsSo this fixed it, I had a typo in username.
-
TooMuchPete about 8 yearsWhile this is true about authentication, it's worth noting that SendEmail is not a valid API v3 endpoint.
-
Tay Moore about 8 yearsThis is the correct answer. It should also be mentioned that even if this did work, I didn't set up the authentication correctly. @micmia mentions the correct way to setup the authentication.
-
Tay Moore about 8 yearsbefore I start a new question, any idea why this would return a 405 (Method not allowed) var apiKey = '<<apikey>>', url = 'us13.api.mailchimp.com/3.0/campaigns/<<campaign>>/…'; try { needle.post(url, {username: 'anystring', password: apiKey}, function(err, headers) { if (err) { console.error(err); } console.log(headers);
-
TooMuchPete about 8 yearsMethod Not Allowed is a standard HTTP error. It means that the HTTP method you're using (in this case POST) is not valid for the resource you selected. MailChimp uses HTTP methods to distinguish between operations. POST for create, GET for read, etc. So how to fix that particular problem depends on what you're trying to do, but you'll either want to use a different method (PATCH or GET) or use a different endpoint (like /campaigns/)
-
Tay Moore about 8 yearsExcellent answer, turns out I had the wrong campaign ID. I'm leaving a comment here so others know that I'm going to use Mandrill to get the job done. If you have a MailChimp subscription already, then you can use Mandrill to send messages. It's a better API (in my opinion) for this task.