简体   繁体   中英

How can I send email with php and mailchimp?

Currently I send email to my website's users using mail() function (which is a php function) . But sadly they go in spam folder. After some research, I figured out I have to use mailchimp to send email. So my first question: Is it true? (using mailchimp for sending emails to avoid being spam) .

In short, all I'm trying to do is sending an email to my website's user as an register activation.


And here is my code:

<?php

$apiKey = "API_KEY";
$campaignId = "CAMPAIGN_ID";

$memberId = md5(strtolower("stackoverflow0916@gmail.com"));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://'. $dataCenter . '.api.mailchimp.com/3.0/campaigns/' . $campaignId .'/actions/test';

$jsonEmail = '{"test_emails":["the mail you want to send thing sat"],"send_type":"html"}';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey:'.$apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonEmail);

$result = curl_exec($ch);
curl_close($ch);

var_dump($result);

?>

And it returns:

string(234) "{"type":" http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/ ","title":"Resource Not Found","status":404,"detail":"The requested resource could not be found.","instance":"4541f02b-ea08-44da-a1d0-0f45f5457399"}"

How can I fix it?

I don't think that you have understood how to use MailChimp. The code above uses an endpoint on MailChimp that is used for testing a campaign ie sending a test email to one or two people to ensure that when the campaign is sent out it works as expected.

In general the idea is that you create a campaign that is due to be sent out at one time to many people. There are variations on this including automations and transactional emails (MailChimp have now incorporated their transactional email application Mandrill into the main application for this purpose).

php mail() is typically used to send out individual emails ie transactional emails rather than mass emails.

If you want to sent out transactional emails through MailChimp then look at that area of the application (or look up Mandrill). If you want to send out campaign type emails then you need to have your list of emails set up and use the POST /campaigns/{campaign_id}/actions/send rather than the test which is only used for testing email campaigns.

As for your specific example...

I am not sure about the syntax that you have given here:

curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);

I would have used the following:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

Other than that your example works for me when I use my own API key and campaign. You may want to check that the campaign id is valid as if it is not then that would certainly give the error response that you got.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM