Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Passbook App Development for iOS How-to

You're reading from   Instant Passbook App Development for iOS How-to Create and customize a Passbook Pass with the exciting new iOS features.

Arrow left icon
Product type Paperback
Published in Jun 2013
Publisher Packt
ISBN-13 9781849697064
Length 56 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Keith D. Moon Keith D. Moon
Author Profile Icon Keith D. Moon
Keith D. Moon
Arrow right icon
View More author details
Toc

Delivering your Pass via e-mail (Medium)


Passes can be delivered as an e-mail attachment, allowing the recipient to view the Pass and add it to their Passbook app.

Getting ready

The Pass e-mail creation script, used below, can be downloaded from the following location:

http://passkit.pro/ruby-email-script

How to do it…

  1. Save the following code into a file named send_pass_by_email.rb.

    require 'net/smtp'
    
    # This script accepts the following arguments: recipients name, recipients email address, path to Pass.
    # Example usage: 
    # ruby send_pass_by_email.rb "Peter Brooke" pbrooke@passkit.pro ../Pass-Example-Generic/Pass-Example-Generic.pkpass
    
    # Retrieve command line arguments
    recipientName = ARGV[0]
    recipientEmail = ARGV[1]
    passFilePath = ARGV[2]
    
    # Setup template email values
    senderName = "Passbook Example Company"
    senderEmail = "info@passkit.pro" 
    emailSubjectText = "New Employee Pass"
    emailBodyText = "Please find attached your new employee Pass"
    
    # Setup SMTP settings
    smtpDomain = "TO DEFINE. Eg. gmail.com"
    smtpLogin = "TO DEFINE. Eg. ......@gmail.com"
    smtpPassword = "TO DEFINE" 
    
    # Read file and base64 encode
    fileContent = File.read(passFilePath)
    encodedContent = [fileContent].pack("m")
    
    # The is used to separate the MIME parts, it can be anything
    # as long as it does not appear elsewhere in the email text
    boundaryMarker = "SEPARATINGSTRINGNOTFOUNDELSEWHERE"
    
    # Setup the email headers.
    headers =<<EOF
    From: #{senderName} <#{senderEmail}>
    To: #{recipientName} <#{recipientEmail}>
    Subject: #{emailSubjectText}
    MIME-Version: 1.0
    Content-Type: multipart/mixed; boundary=#{boundaryMarker}
    --#{boundaryMarker}
    EOF
    
    # Setup the email body
    body =<<EOF
    Content-Type: text/plain
    Content-Transfer-Encoding:8bit
    
    #{emailBodyText}
    --#{boundaryMarker}
    EOF
    
    # Setup the Pass attachment with the correct MIME Encoding
    attachment =<<EOF
    Content-Type: application/vnd.apple.pkpass; name=\"#{passFilePath}\"
    Content-Transfer-Encoding:base64
    Content-Disposition: attachment; filename="#{passFilePath}"
    
    #{encodedContent}
    --#{boundaryMarker}--
    EOF
    
    completeEmail = headers + body + attachment
    
    # Send email using your SMTP settings
    smtp = Net::SMTP.new 'smtp.gmail.com', 587
    smtp.enable_starttls
    smtp.start(smtpDomain, smtpLogin, smtpPassword, :login) do
      smtp.send_message(completeEmail, senderEmail, recipientEmail)
    end
  2. Under the section headed # Setup template email values, enter relevant values for the sender name, sender e-mail address, subject, and e-mail body.

  3. Under the section header # Setup SMTP settings, enter the details of the SMTP e-mail server and account details that will be used to send the e-mail. These can be found from the setting of your e-mail client, or you can use a free e-mail service like Gmail.

  4. This Ruby script accepts three arguments, the recipient's name, the recipient's e-mail address and the path to the Pass to be attached. Open the Terminal and send a Pass-enabled e-mail by calling the script with appropriate arguments, as shown in the following example:

    ruby <Path to send_pass_by_email.rb> "Peter Brooke" pbrooke@passkit.pro <Path to Pass to attach .pkpass>

    The following screenshot shows what the resulting e-mail will look like on iOS:

    ,

  5. If you sent this e-mail to an e-mail account you have access to, open this e-mail in the Mail app on an iPhone running iOS 6 or Mail on OSX 10.8.2, and you will be given the option to open and view the Pass and add it to Passbook.

How it works…

The script used above is written in Ruby, as the Ruby interpreter is installed by default on OSX.

Sending a Pass as an attachment that will be understood by iOS and OSX, and presented to the user, requires it to have a specific MIME Type specified in the attachment. This MIME type is the following:

application/vnd.apple.pkpass

This script, or something similar could be used to automate the delivery of Passes to a large number of users, through email.

You have been reading a chapter from
Instant Passbook App Development for iOS How-to
Published in: Jun 2013
Publisher: Packt
ISBN-13: 9781849697064
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image