Achraf Khouadja
Achraf Khouadja

Reputation: 6279

Mailchimp Does not work on production [laravel on azure]

I deployed a small laravel app that subscribes a user to a mailchimp list. it's very basic yet it does not work on production

NOTE: EVERYTHING is fine in Localhost env and a CONTACT FORM WORKS FINE(uses SMTP)

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=AUSERNAME
MAIL_PASSWORD=APASSWORD
MAIL_ENCRYPTION=TLS

MAILCHIMP_APIKEY=APIKEYHERE
MAILCHIMP_LIST_ID=LISTIDHERE

Controller

Newsletter::subscribe($request->email, [
                'firstName' => 'test', 
                'lastName' => 'tessst', 
                'listName' => 'whishlist' ], 'subscribers');

return response()->json([
                     'status' => 'success',
                     'msg'  => 'Subscribed successfully']);

laravel-newsletter Config file

<?php

return [

    'apiKey'          => env('MAILCHIMP_APIKEY'),


    'defaultListName' => 'subscribers',

    'lists'           => [


        'subscribers' => [


            'id' => '5920168294',
        ],
        'whishlist'   => [


            'id' => '8e553f3d39',
        ],
    ],
];

My i guess is that this has something to do with HTTPS (i fixed the issue by adding a file cacert.pem and referencing it in php.ini ) if this is the issue how can i fix this on azure?

And sorry there is no error output since it returns success to the ajax call.(if how can i get the response from mailchimp to check the error?)

Thanks in advance.

Upvotes: 2

Views: 580

Answers (1)

Achraf Khouadja
Achraf Khouadja

Reputation: 6279

Well i dont know what is the issue here.

but i managed to make this work when i got rid of the package i'm using which is spatie/laravel-newsletter and used CURL instead and API V3.

 $email = $request->email;
    $listid = env('MAILCHIMP_LIST_ID');
    $apikey =  env('MAILCHIMP_APIKEY');
    $server = substr($apikey, strpos($apikey, '-') + 1);
    $auth = base64_encode('user:' . $apikey);
    $data = array(
        'apikey'        => $apikey,
        'email_address' => $email,
        'status'        => 'subscribed',
        'merge_fields'  => array(
            'FNAME' => 'test1',
            'LNAME' => 'test2',
        ),

    );
    $json_data = json_encode($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://'. $server.'api.mailchimp.com/3.0/lists/'. $listid .'/members/');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
        'Authorization: Basic ' . $auth));
    curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;

Upvotes: 2

Related Questions