In service development, it was decided to manage e-mail newsletter distribution with Mailchimp.
At that time, by hitting the API provided by Mailchimp at the time of user registration,
Implemented the process to subscribe to the mailchimp side mailing list.
There are few Japanese documents that can be used as a reference for the Mailchimp API, so
I hope you find this article useful.
Programming language: Ruby
Framework: Rails
It is a very general Ruby + Rails environment.
Describe the following in the Gemfile
gem "mailchimp-api", require: "mailchimp"
Have your API key ready to use the Mailchimp API.
First, click on Mailchimp's own icon.
A menu will appear, so click "Account".
Click "Extras" on the next screen.
Press "API keys".
Press "Create A Key" on the screen that appears after that.
Save the "API key" that came out.
Mailchimp has the concept of a list to register user's mail information.
This is called "Audience".
Therefore, which list should you register? Must be specified.
To do this, we get the ID of the list, the "Audiens ID".
From here as well, it is a tick and Mailchimp console operation.
First, press "Audience" in the header. Then press "Audience dashboard".
Then click "Manage Audience"-> "Settings".
Click "Settings" → "Audience name and defaults".
It was finally done. Audience ID. Make a note of this.
Enter the obtained API key and Audience ID into .env.
.env
# Mailchimp
MAILCHIMP_API_KEY= #API key
MAILCHIMP_LIST_ID= # Audience ID
Create a model for Mailchimp.
First, create a class.
I have referenced a part of this article (https://qiita.com/kon_yu/items/372bba35e60744633c94).
qiita.rb
class MailMagazine
#Mailchimp API initialization
def initialize
@mailchimp = Mailchimp::API.new(ENV['MAILCHIMP_API_KEY'])
end
#Add to list
#Refer to the account merge tag for the hash value of the third argument(lists/settings/merge-tags?id=XXX)
def add_member(email, first_name, last_name)
@mailchimp.lists.subscribe(
ENV['MAILCHIMP_LIST_ID'],
{email: email},
{FNAME: first_name, LNAME: last_name},
"html",
false
)
end
end
Create a method called ʻadd_member` and register it.
The first argument is the email address.
The second argument is the name.
The third argument is the surname.
is.
lists. Subscribe
in ʻadd_member` is an API method,
It will register your email address in Mailchimp.
The first argument is ʻAudience ID`.
The second argument is the Hash value of the email address you want to register.
The third argument is auxiliary information that can be set with Mailchimp. You can register your name and birthday.
For content, see the Mailchimp console
「Audience」→「Settings」→「Audience fields and * |MERGE| * tags」
Please refer to the.
You can check and register information other than your email address like this.
As with your email address, enter a Hash value keyed by Mergetag.
Incorporate into the user registration process like this.
mailchimp = MailMagazine.new
mailchimp.add_member(@user.email, @user.first_name, @user.last_name)
Surprisingly easy ^ ^
Have a good Mailchimp life ~
Recommended Posts