Attaching UIImage to email iOS - ios

I am trying to email a UIImage from my app. In my app I take a picture and then set a UIImageView to the picture that was just taken. Then I have a button which should email that picture as an attachment, like this:
- (IBAction)emailPhoto:(UIButton *)sender {
sendImage = imageView.image;
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
if([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"JanJaap#Korteweg.nl",nil]];
[composer setSubject:#"A nice subject"];
[composer setMessageBody:#"Hi,\n\nI'm sending you this beautiful photograph." isHTML:NO];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
NSData *data = UIImageJPEGRepresentation(sendImage,1);
[composer addAttachmentData:data mimeType:#"image/jpeg" fileName:#"Photograph.jpg"];
[self presentViewController:composer animated:YES completion:nil];
}
}
For some reason when I call this I get the composer window but it remains kind of greyed out and I can't edit anything (put in email address, type in body, etc). Also no attachment shows up. I deleted the image code and tried again and it all worked so the problem is somewhere in my handling of the image, but I am at a loss as to what that might be and I get no errors at all or warnings. I have tried a number of other strategies including bas64 encoding and saving the photo to the photo library and then retrieving it but they seem too complicated and result in never ending chains of warnings and errors. Any help appreciated. Thanks!

Related

Displaying pdf document in MFMailComposeViewController

I've read posts that say the the `MFMailComposeViewController should display your attached PDF if its a 1 page document but for some reason my PDF is always showing up as just an icon. I want it to display the PDF since its only 1 page. The PDF seems to be fine since I can send the email and I can open it in the mail app or on my computer.
Is this not enabled by default in iOS 6.0 or later or is there something im missing?
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
// create the message body.
NSMutableString *body = [[NSMutableString alloc] init];
// add the user name to the email body.
[body appendFormat:#"Name: %#", userName];
// add the pdf to the email.
NSData *data = [NSData dataWithContentsOfFile:path];
[controller addAttachmentData:data mimeType:#"application/pdf"
fileName:#"test.pdf"
includeExtension:YES]];
// add the body string to the email body.
[controller setMessageBody:body isHTML:NO];
// show the email controller.
[self presentViewController:controller animated:YES completion:nil];

MFMailComposeViewController canSendMail returns YES but not sending mail in iOS

The following is my code to send an attachment in a mail. This works fine. I am able to send mails but I don't always receive the mails.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// Set the subject of email
[picker setSubject:#"My data file"];
// Add email addresses
[picker setToRecipients:[NSArray arrayWithObjects:emailId, nil]];
// Fill out the email body text
NSString *emailBody = #"Hello, \n Please find the data from the iOS app in the attachments.\n\n Thank you.\nMy Team.";
// This is not an HTML formatted email
[picker setMessageBody:emailBody isHTML:NO];
// Create NSData object from file
NSData *exportFileData = [NSData dataWithContentsOfFile:filePath];
// Attach image data to the email
[picker addAttachmentData:exportFileData mimeType:#"text/csv" fileName: [self.CSVNameTextField text]];
// Show email view
if ([MFMailComposeViewController canSendMail]) {
[self presentModalViewController:picker animated:YES];
}
After you send the Mail with your App go to the Mail-Software on your iPhone, you will most likely find the mail in the Outbox.
Cause the MFMailComposeViewController will just forward the Mail to the Mail-Software and it doesn't care what happens next to the message. So it's up to your Mail-Software how and when the Outbox will be updated.
I had the same problem, I found that it would go through the mail box and say it had sent and then nothing would come through.
About 20-30 minutes later the first one came through and then gradually the rest I sent came through too.
I don't know why it takes so long, if I find out I will edit this answer, but definitely wait up to an hour before assuming your code is broken.
Hope this helps someone who, like me, might be trawling their code over and over
In my case i had to manually open up the iphone mail app, then mails were sent and received immediately.

issue when multiple recipients for my app mail- Objective c

I am facing a problem when multiple recipients for my mail, i have two attachments there by default.Is there anything i have to do when iam sending a mail to multiple recipients other than the below code; (I have to select or type recipient id's from UI)
if ([MFMailComposeViewController canSendMail])
{
[self printPdfAndCsv];// code to generate pdf & csv
MFMailComposeViewController* mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
// attaching PDF File.
[mailComposer addAttachmentData:[NSData dataWithContentsOfFile:self.pdfFilePath]
mimeType:#"Application/pdf" fileName:[NSString stringWithFormat:#"pdfName-%#.pdf", selectedProjectName ]];
// attaching CSV File.
[mailComposer addAttachmentData:[NSData dataWithContentsOfFile:self.csvFilePath]
mimeType:#"text/csv" fileName:[NSString stringWithFormat:#"csvName-%#.csv", selectedProjectName ]];
[self presentViewController:mailComposer animated:YES completion:nil];
}
Iam a starter in iPhone development, so i need your valuable help.
Try this
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
NSArray *toRecipients = [NSArray arrayWithObjects:#"abc#gmail.com",#"xyz#gmail.com",nil];
[picker setToRecipients:toRecipients];
If you want to sent mail to multiple user then you can use:
[mailController setToRecipients:[NSArray arrayWithObject:#"email#address.com",#"email1#address.com",#"email#address.com",nil]];
I got a solution from rmaddy on his comment,
There may be problem with one of the email addresses we tested.
Maybe the email ended up appearing as junk mail (spam).
Once the user taps Send, it's out of our control.
(and in my case; i found the mails that i have sent were in spam box)

Attaching a PKPass to Email in Code

I am using PassSlot which creates a Pass on the fly that can be added to passbook. I am trying to get it downloaded to the device to allow attaching to an email. Here is what I have so far:
[PassSlot passFromTemplateWithName:#"LoveCouponCards" withValues:values pass:^(PSPass *pass) {
[PassSlot downloadPass:pass pass:^(PSPass *pass) {
PKPass *pkpass = [pass performSelector:#selector(pass)];
NSLog(#"Pass: %#", pkpass);
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObject:#"friend#example.com"];
[picker setToRecipients:toRecipients];
[picker addAttachmentData:pkpass mimeType:#"application/vnd.apple.pkpass" fileName:#"HI"];
// Fill out the email body text
NSString *emailBody = \\
[picker setMessageBody:emailBody isHTML:NO];
[self presentViewController:picker animated:YES completion:nil];
}];
}];
The issue is that in the addAttachment part for the email, it throws an error that NSData can't relate to PKPass basically. How can I get pass converted to NSData so I can attach it?
UPDATE:
I tried doing
NSURL *url = pkpass.passURL;
NSData *so = [NSData dataWithContentsOfURL:url];
and then putting 'so' as the addAttachment, but it attached nothing to the email.
Firstly, the passURL property of PKPass doesn't quite work the way you think. It is not a URL to the pass itself. It is a URL that opens up the Passbook app and loads up that requested pass.
You can create a PKPass with NSData, but you can't reverse that process. It sounds as if you are trying to get a pass on device, and then e-mail it. That's not allowed - if it was, people could easily copy and distribute passes around (which isn't necessarily a good thing).
If you want to e-mail a user a pass you need to do it server, rather than client side. I'm afraid that what you're trying to do isn't possible using PassKit. Sorry!
Unfortunately the PassKit library does not provide a way to get back the NSData from a PKPass.
We already provide an API call that allows you to get the raw data of a pass.
We will extend our PassSlot SDK with a method that allows you to get the NSData without having the manually call this API method.
Update
The new SDK version 0.5 is now released. You can attach the pass with the following code:
[PassSlot passFromTemplateWithName:#"LoveCouponCards" withValues:values pass:^(PSPass *pass) {
[PassSlot downloadPass:pass pass:^(PSPass *pass) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setToRecipients:#[#"friend#example.com"]];
[picker addAttachmentData:pass.data mimeType:#"application/vnd.apple.pkpass" fileName:#"LoveCouponCard.pkpass"];
[picker setMessageBody:emailBody isHTML:NO];
[self presentViewController:picker animated:YES completion:nil];
}];
}];

Clickable link (Hyperlink)?

I'm sorry if this is a stupid question but I cannot find a answer. Is there a way to create a clickable link say in a MFMailComposeController or in any other NSString? I just want it to be blue and the user will be able to click it and it will go to that link.
Thanks!
in the mail msg body try this - [mailComposer setMessageBody:msg isHTML:YES];. So when you put the message compose it of HTML & put this setting so that the mail is sent as HTML...
MFMailComposeViewController *mailComposer;
mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setModalPresentationStyle:UIModalPresentationFormSheet];
[mailComposer setSubject:#"Mail subject"];
[mailComposer setMessageBody:msg isHTML:YES];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
return;

Resources