How to extract the emails of your Firebase app users in a format readable by most email delivery services such as MailChimp and MailGun.
TL;DR: https://github.com/Marwan01/import-firebase-users
Dependencies
Go
firebase-cli
Firebase CLI
Download and install firebase-cli: https://firebase.google.com/docs/cli
brew install firebase-cli
Login to firebase-cli:
firebase login
Get your project ID:
firebase projects:list
Run the command to get your auth users data:
firebase auth:export save_file.csv --format=csv --project <PROJECT_ID>
You should now have a csv save_file.csv that has all your users’ emails along with a bunch of scrambled extra data.
Golang Implementation
To get started, create a new Directory/Git Repository, and initialize and Go project within it:
mkdir import-firebase-users
cd import-firebase-users
touch splitclean.go
go mod init
Let’s start by adding the skeleton code for our service:
package main
import (
"fmt"
)
func main() {
fmt.Println("Firebase email export start.")
}
Let’s test our execution by running go run splitclean.go . We should expect to see in stdout: Firebase email export start .
Now we can start working on the main course of the script, which is getting the data csv from Firebase and cleaning it up. First let’s open and parse the csv into a reader that we can loop over and extract emails from:
// Open the file
csvfile, err := os.Open("save_file.csv")
if err != nil {
log.Fatalln("Couldn't open the csv file", err)
}
// Parse the file
r := csv.NewReader(csvfile)
Then let’s create an emails slice, and loop over the reader, and pick out the record that has the email:
var emails []string
// Iterate through the records
for {
// Read each record from csv
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
emails = append(emails, record[1])
}
Now let’s create a .txt file and write a header column Email Address . This is required by most mail services, and makes it automatically convertible to .csv with a header, if that’s what you want to work with.
// Create a .txt file
f, err := os.Create("emails.txt")
if err != nil {
log.Fatal(err)
}
// write header for the file
_, _ = f.WriteString("Email Address\n")
// close file when done
defer f.Close()
Now for the last step, let’s loop over the email slice and add each email to its own line on the .txt file:
// loop over the emails slice
for i := 0; i < len(emails); i++ {
_, err2 := f.WriteString(emails[i])
_, _ = f.WriteString("\n")
if err2 != nil {
log.Fatal(err2)
}
}
(Optional) Bash Script
We can simplify the execution one more step, by adding a two command bash script that will call the firebase-cli with a a Project ID parameter, and the your Golang script:
touch entrypoint.sh
Add the two commands:
firebase auth:export save_file.csv --format=csv --project $1
go run splitclean.go
Make your script executable:
chmod +x entrypoint.sh
Finally run the script:
./entrypoint.sh <firebase project ID>
Top comments (0)