6

I'm trying to make an app that sends an email using MFMailComposeViewController but that class doesn't seem to provide a way to set email headers.

I tried to look for a lower level solution (or even a library) that does this but couldn't find one. How can I set/get email headers in an Objective-C app?

[EDIT] To be clear, I am looking for a way to set email headers by ways other than provided by MFMailComposeViewController. I already know how to send a simple email. I am looking for a way to set email headers such as In-Reply-To, or Message-ID

Vlad
  • 8,038
  • 14
  • 60
  • 92
  • 1
    Header means u wants set Subject ? – Soumya Ranjan Sep 25 '14 at 05:03
  • there are all the methods that you need: setSubject(_:) setToRecipients(_:) setCcRecipients(_:) setBccRecipients(_:) setMessageBody(_:isHTML:) addAttachmentData(_:mimeType:fileName: – Abid Hussain Sep 25 '14 at 08:03
  • 1
    No, by header I actually mean the full email header http://en.wikipedia.org/wiki/Email#Header_fields More specifically I was trying to access headers like Message-ID or In-Reply-To. – Vlad Sep 25 '14 at 14:07
  • I already know how to send simple emails using all the methods Abid mentioned above. – Vlad Sep 25 '14 at 14:08
  • Have you seen mailcore? https://github.com/MailCore/MailCore – Nikita Took Sep 28 '14 at 18:22
  • @NikitaTook I think you missed it but I specifically stated above I am not looking for a solution like MailCore. – Vlad Sep 30 '14 at 02:10
  • 1
    @Vlad You won't find anything that integrates with built-in iOS mail that allows the setting of extra headers (`mailto:` supports the limited set that `MFMailComposeViewController` supports). – Anya Shenanigans Sep 30 '14 at 14:07
  • Take a look at [skpsmtpmessage](https://github.com/jetseven/skpsmtpmessage) – davidcondrey Oct 04 '14 at 11:43
  • @Vlad did you ever find a solution to this? I too would like to be able to add custom headers to an email composed using something like `MFMailComposeViewController`. – fractious Dec 04 '15 at 15:56

3 Answers3

1

The authors of Sparrow wrote a great class called MailCore (now with version 2) that allows you to do just that.

If you'll go to the class Class documentation here: http://libmailcore.com/mailcore2/api/Classes/MCOMessageHeader.html

You can find all kind of useful headers in the properties such as messageID, inReplyTo etc'.

Segev
  • 19,035
  • 12
  • 80
  • 152
  • Thanks but if you read the requirement above, I specifically stated I am not looking for a solution like MailCore. – Vlad Sep 30 '14 at 02:10
1

If You want to create email header you can create your own header using html and set it as the message body of the mail with isHTML as True.

I just tried the following code:

NSString * Htmlstr =[NSString stringWithFormat:@ "<html>"
                   "<body>"
                   "<div id=\"container\" style=\"background-color:#EEEEEE;height:300px;width:500px;float:left;\">"
                   "<div id=\"header\" style=\"background-color:#FFA500;\">"
                   "<h3 style=\"margin-top:0;\">ABC</h3></div>"
                   "<div id=\"content\" style=\"background-color:#EEEEEE;height:300px;width:500px;float:left;\">%@</div>"
                   "<div id=\"Banner\" style=\"background-color:#EEEEEE;clear:both;text-align:left;margin-top:0;\"><img src=\"%@\" alt=\"Smiley face\" width=\"500\" height=\"100\"></div>"

                   "<div id=\"footer\" style=\"background-color:#FFA500;clear:both;text-align:center;margin-bottom:0;\">Copyright © abc.com</div>"
                   "</div>"
                   "</body>"
                   "</html>",emailBody,self.SelectedBannerImage];

[mailer setMessageBody:Htmlstr isHTML:YES];

Hope this helps you..

Mahesh
  • 526
  • 1
  • 6
  • 21
  • 1
    Thanks, but I think there was misunderstanding. By header I meant this: http://en.wikipedia.org/wiki/Email#Header_fields – Vlad Oct 01 '14 at 15:10
1

I think you'd have enough resources if you used libcurl (c library) or one of its cocoa wrappers, no?

Check here for BBHTTP and CURLHandle.

That should let you manually compose your smtp traffic using cocoa/objc syntax. Is that more along the lines of what you're looking for?

Miles Alden
  • 1,542
  • 1
  • 11
  • 21
  • I was more looking for a way to use Mail.app to send the email without the user having to go through all the complexities of using low level protocols. I thought since other more basic headers such as title, etc. are allowed there should be a way to attach headers like In-Reply-To using a high level API somehow. – Vlad Oct 04 '14 at 16:49
  • I was going to suggest that you pre-compose an email and open it using `open -a mail.app myEmail`, but then I remembered you're trying to do this for iOS. I think your best shot, short of exploiting private methods, would be to either a) swizzle out a later-executing, non-private method that allows you a bit of access to the model going out (not likely), b) roll your own mail compose view controller supported by one of several libraries mentioned throughout this qa. Apple simply did not expose what you are looking for. :/ – Miles Alden Oct 05 '14 at 21:23