Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Thursday, April 16, 2009

Sending an email from an iPhone application

Thursday, April 16, 2009   

This certainly is not the most secure way of sending an email by any means, but it does work. So you would like your iPhone application to be able to send an email - perhaps a feedback form of some kind, etc. The example supplied here contains a UITextField and a UIButton. A UIAlertView and a UIActionSheet are also used. Oh, and there is a little snippet of PHP to put on your server someplace (that's the unsecure part here).

Your .h file:
#import <UIKit/UIKit.h>

@interface SendEmailViewController : UIViewController {
IBOutlet UIButton *button;
IBOutlet UITextField *messageText;
IBOutlet UIButton *backgroundButton;
}

@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UITextField *messageText;
@property (nonatomic, retain) UIButton *backgroundButton;

-(IBAction)sendMail:(id)sender;
-(IBAction)textFieldDoneEditing:(id)sender;
-(IBAction)backgroundClick:(id)sender;

@end
And now your .m file:
#import "SendEmailViewController.h"

@implementation SendEmailViewController

@synthesize button, messageText, backgroundButton;

-(IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}

-(IBAction)backgroundClick:(id)sender {
[messageText resignFirstResponder];
}

-(void)displayAlert {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email Sent" message:@"Thank you for contacting me" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[messageText setText:@""];
[alert show];
[alert release];
}

-(void)displaySheet {
NSString *msg = nil;
msg = [[NSString alloc] initWithFormat:@"Send Email? Your message:\"%@\"", messageText.text];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:msg delegate:self cancelButtonTitle:@"Not yet" destructiveButtonTitle:@"Yes" otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
[msg release];
}

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if( !(buttonIndex == [actionSheet cancelButtonIndex]) ){
NSString *post = nil;
post = [[NSString alloc] initWithFormat:@"message=%@",messageText.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.yourserveraddy/yourScript.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[ NSURLConnection connectionWithRequest:request delegate:self ];

[post release];
[self displayAlert];
}
}

-(IBAction)sendMail:(id)sender {
[self displaySheet];
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[button release];
[messageText release];
[backgroundButton release];
}
And lastly your PHP file:
<?php
$message = $_REQUEST['message'] ;
mail( "recipient@server.com", "Test Message", $message );
?>
You just need to set your outlets and set you should be all set.

Labels: , ,

 
 Return to the main page
Comments:

There are currently 2 Comments:

Blogger Elrasguno said...
“Hello,
I was hoping you could take a look at this code and tell me if you see a problem. I'm successfully reaching my server and, but for some reason $_POST is empty. I'm hoping there's something obvious that I'm not seeing. Here is the method:

- (void)postWordsToServer:(id)url withData:(id)hatedWordsData {
NSLog(@"postWordsToServer url: %@", url );
NSLog(@"postWordsToServer data: %@", [hatedWordsData JSONRepresentation]);

self.receivedData = [[NSMutableData data] retain];

//NSString *post = [NSString stringWithFormat:@"iPhoneAction=updateWords&wordData=%@", [hatedWordsData JSONRepresentation]];
NSString *post = @"test=ZOMGWTFBBQ";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL: [NSURL URLWithString: url]];
[request setHTTPMethod: @"P0ST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

// send it
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}”
 
Anonymous Anonymous said...
January 23, 2010 4:32 PM
“I really appreciate your good explanation about sending emails through iPhone.

Could you also please explain us how to send an email to somebody else instead of a fixed person?

I can not find out how it should be coded.
Thanks alot.”
 
 Leave a comment