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:
Your .h file:
#import <UIKit/UIKit.h>And now your .m file:
@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
#import "SendEmailViewController.h"And lastly your PHP file:
@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];
}
<?phpYou just need to set your outlets and set you should be all set.
$message = $_REQUEST['message'] ;
mail( "recipient@server.com", "Test Message", $message );
?>
Labels: email, Objective-C, Xcode
Comments:
There are currently 2 Comments:
-
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];
}”