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:
How to do it…
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
Under the section headed # Setup template email values, enter relevant values for the sender name, sender e-mail address, subject, and e-mail body.
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.
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:
,
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.