Alternative ways to send email without UI from ipad or iphone

旧巷老猫 提交于 2019-12-06 11:12:40
BBog

I might be able to provide some help for the third option, using HTTP Post, since I did implement it once in a project.

First, I used this nice and simple iOS class to take care of the posting for me. Then, the following iOS code snippet should show you how it's done

NSString* from = @"sender@email";

NSString* to = @"receiver@email";

NSString* mailCc = @"cc@email";

NSString* message = @"my message"

NSString* subject = @"my subject";

NSURL* url = [NSURL URLWithString:@"http://yourtestsite.com/my_email_script.php"];

//these are $_POST variables sent, so 'from' would be $_POST['from']        
NSArray *keys = [[NSArray alloc] initWithObjects:@"from", @"to", @"cc", @"subject", @"message", nil];
NSArray *objects = [[NSArray alloc] initWithObjects:from, to, mailCc, subject, message, nil];  
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];


NSMutableURLRequest* request = [SimplePost urlencodedRequestWithURL:url andDataDictionary:dictionary];
NSURLResponse* response = [[NSURLResponse alloc] init];
NSError* error = [[NSError alloc] init];

NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];

NSString* result = [[NSString alloc] initWithData:returnData encoding:NSStringEncodingConversionAllowLossy];

//I'm checking for 1 because my php script was set to write 1 to the page in case of success and 0 otherwise, so this is simply my implementation
if([result isEqualToString:@"1"]) {
    NSLog(@"success");
} else {
    NSLog(@"error");

}

For the PHP file, this should do the trick

$from = filter_var($_POST['from'], FILTER_SANITIZE_EMAIL);

$to = filter_var($_POST['to'], FILTER_SANITIZE_EMAIL);

$cc = filter_var($_POST['cc'], FILTER_SANITIZE_EMAIL);

$subject = htmlspecialchars(utf8_decode($_POST['subject']));

$message = utf8_decode($_POST['message']);

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'From: ' . $from . "\r\n";

$headers .= 'Cc: ' . $cc . "\r\n";

// Mail it
if(mail($to, $subject, $message, $headers)) {
    echo("1");
} else  {
    echo("0");

   }

Keep in mind, I'm no PHP expert, so that code might be improved, especially on the security part.

[edit] PHP mailing should already be enabled in most major managed hosting solutions, be it a cheap shared account, a VPS or a dedicated server. But if you plan to send A LOT of emails with this method, then a dedicated server is recommended.

However, there is a limit of emails you can send and better options than the mail function. You can find more info about this here.

[later edit] It seems the author deleted the SimplePost class. However, the same author made an alternative which should help, called SimpleHTTPRequest. The rest should remain the same

I would look into using Mailgun, it will make sending emails much easier and handles up to 10k a month for free. They allow you to use their subdomain if you dont have your own, but can easily setup your own to work with theirs.

Its as easy as installing up a cocoapod and adding 5 lines of code or so.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!