Generating a vCard in Rails App with vCardigan

Kelsey Pedersen
2 min readDec 21, 2021

All new Dryftwell users receive a “Welcome” text.

This text includes a contact card with additional contact info for Dryftwell + a photo. We text users a couple times during a trip so we want them to be able to easily save our contact information.

I had previously created a vCard within the contacts app on my Macbook. However, for some AT&T users, the card was malformed and didn’t show up correctly. So, this week, I went about figuring out how to programmatically generate a new vCard.

It was super easy! So figured I’d share if anyone else is trying to create one.

Create a new vcard.rb in the root directory of your Rails app

After a bit of research, I decided to use the Ruby library: vCardigan. It’s the library used in Twilio docs + super simple to use.

The only tricky piece was figuring out how to encode our logo image!

Here’s the code snippet below:

#vcard.rbrequire “vcardigan”
require “base64”
vcard = VCardigan.create(:version => ‘4.0’)
vcard.fullname ‘Placeholder Name’
vcard.tel ‘+16502223333’, :type => ‘mobile’
vcard.email ‘hello@placeholder.com’
vcard[:item1].URL ‘https://www.placeholder.com/'
encoded_string = Base64.strict_encode64(File.open(“placeholder-logo.png”, “rb”).read)
vcard.photo encoded_string, {:type => ‘image/png’, :’ENCODING’ => ‘b’}
file = File.open(“vcard.vcf”, “w”)
file.puts vcard
file.close

Generate new vCard from the command line

Run the following command in your terminal:

$ bundle exec ruby vcard.rb

Upload the vCard

Now that you have the new vCard file, you’ll now want to use it!

If you use Twilio, you’ll send the vCard to users via the mediaUrl . You’ll need to host online with a public URL — we’ve used AWS S3 and Twilio Functions. Upload the new file and then pass into your Twilio object.

--

--