9

How do I set the from address in the MFMailComposeViewController?

MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"In app email..."];
[controller setMessageBody:@"To FirstName LastName: " isHTML:NO];

// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"eamil@somemail.com"]; // (NSString *) [feed valueForKey:@"email"]]; 
[controller setToRecipients:toRecipients];
NSNoob
  • 5,548
  • 6
  • 41
  • 54
HardCode
  • 2,025
  • 4
  • 33
  • 55

4 Answers4

10

There isn't a way to do what you want with the From field. The from address will default to whatever mail account the user has specified as "default" in Settings. Obviously if the user has only one mail account set up then it will be that account.

Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65
  • So, basically every user will have an email address setup to the device? or is there a possibility they don't have an email set up? I want to use the email address that user assign to our app. – HardCode Nov 02 '11 at 15:11
  • If they don't have a mail account setup on the device then they will not be able to send mail. You will only be able to use the email address that the user assigns to your app if they have it set up on their device. – Brian Driscoll Nov 02 '11 at 15:13
  • This is now possible, please refer to my answer – heyfrank Nov 20 '18 at 11:08
4

This is possible since iOS 11.0

Use setPreferredSendingEmailAddress(_:) to define a preferred address which will automatically select a corresponding account if available.

From the documentation:

Sets the preferred email address to use in the From field, if such an address is available.

If the user does not have an account with a preferred address set up, the default account is used instead. Call this method before you display the mail composition interface only. Do not call it after presenting the interface to the user.

Community
  • 1
  • 1
heyfrank
  • 5,291
  • 3
  • 32
  • 46
0

I don't think you can change "from" if you use MFMailComposeViewController in app email in iOS Another workaround, you may use this, a standalone email client, instead. You will have better control of the sender programmatically.

androidkc
  • 689
  • 2
  • 7
  • 16
0

As far as I know, it's not possible. When the mail composer is open, you will be able to fetch the "from" field from the list of e-mails configured in the device, and the user selected default will be initially set.

I know that it would be useful to know the list of available "from" accounts and set the composer with a more appropriate user choice. Probably Apple doesn't want to give the app this possibility. You might open a "radar" with Apple, e.g. adding a special view controller to let the user define a different default address for the specific app, this would be a nice addition to the iOS.

viggio24
  • 12,316
  • 5
  • 41
  • 34