Hardik Davra
Hardik Davra

Reputation: 29

How to send a simple mail using mailchimp in laravel 5.8

I have tried this code

$listId = 'xxxxxxxxxxxx';

//Mailchimp instantiation with Key

$mailchimp = new \Mailchimp('xxxxxxxxxxxxxxxxxxxxxxxx-xxx');

$mailchimp->from('xxxxxxxxxxxxxxx');
$mailchimp->reply_to('xxxxxxxxxxxxxxxx');
$mailchimp->to($email->email);
$mailchimp->subject('x');xxxxxxxxxxxxx
$mailchimp->message('<html>hiiiii</html');
$mailchimp->set_header('Content-type', 'text/html');
$mailchimp->send();

Upvotes: 1

Views: 4256

Answers (1)

Waleed Muaz
Waleed Muaz

Reputation: 802

Mailchimp Installation

composer require nztim/mailchimp

For Laravel support: Laravel 5.5+ will auto-discover the package, for earlier versions you will need to: Add the service provider to config/app.php: NZTim\Mailchimp\MailchimpServiceProvider::class, Register the facade: 'Mailchimp' => NZTim\Mailchimp\MailchimpFacade::class, Add an .env value for MC_KEY (your API key) Optionally, publish the config file:

php artisan vendor:publish --provider=NZTim\Mailchimp\MailchimpServiceProvider

Code Email: -

$member = (new NZTim\Mailchimp\Member($email))->merge_fields(['FNAME' => 'First name'])->email_type('text')->confirm(false);
Mailchimp::addUpdateMember($member);

Examples

// Laravel:
// Subscribe a user to your list, existing subscribers will not receive confirmation emails
Mailchimp::subscribe('listid', '[email protected]');

// Subscribe a user to your list with merge fields and double-opt-in confirmation disabled
Mailchimp::subscribe('listid', '[email protected]', ['FNAME' => 'First name', 'LNAME' => 'Last name'], false);

// Subscribe/update a user using the Member class
$member = (new NZTim\Mailchimp\Member($email))->interests(['abc123fed' => true])->language('th');
Mailchimp::addUpdateMember('listid', $member);

Errors

  1. Exceptions are thrown for all errors.
  2. Networking/communications errors will usually be of the type Requests_Exception.
  3. API errors will be of the base type NZTim\Mailchimp\MailchimpException, e.g. incorrect API key, list does not exist.
  4. NZTim\Mailchimp\Exception\MailchimpBadRequestException includes a response() method that returns the response body as an array.
  5. Gotchas: the API throws an error when you:

    *Specify a merge field name with incorrect capitalisation

    *Omit a required merge field when adding a new member

Upvotes: 2

Related Questions