Sending mail with attachment from iPhone Application

iPhone applications dealing with data (Budgets, Sales, etc.) are much more useful when we can send the data through mail. The data collected can be moved to a central location and used by other  people or applications.

The iPhone SDK has provided developers with the MFMailComposeViewController class, which can be used to display a  standard interface that manages the editing and sending of an email message. This UI will have the facilities to add recipient mail ids in To, Cc or Bcc. There are options to specify the subject line and type in the content of the mail. The user can edit the initial contents you specify and choose to send the email or cancel the operation.

MFMailComposeViewController class also offers methods (such as addAttachmentData) to add a file/data as an attachment. This method supports most file formats. The file format is passed as a parameter (mimeType). The mimeType is generally divided into categories like 

  • application – formats like json, pdf etc… comes under this type
  • audio – most of the audio formats comes under this type
  • image – most of the image file formats are under this type
  • text – formats like csv, css, rtf, xml, rtx etc…
  • video – most of the video formats are under this type.

So the mimeType for a CSV file will be specified as “text/csv”. Along with this, we will pass parameters for data as well as the filename to be used for the attachment.

The user can click on Send button in the UI to send the mail. The didFinishWithResult method, which is a delegate method of MFMailComposeViewController class can be implemented to get the status of the mail sent (like MFMailComposeResultCancelled, MFMailComposeResultSaved, MFMailComposeResultSent, MFMailComposeResultFailed). This status can be used to further handle or alert the user regarding the mail they sent.

Thus the developers can easily make use of the iPhone SDK to implement the mailing facility in their own application, which will add more value and make it more user-friendly.

References: More details on mimeType – http://www.iana.org/assignments/media-types/

Leave a Reply