Alternative ways to send email without UI from ipa

2019-05-20 00:02发布

I see there are many ways to send email in the background (without UI) in iOS devices and without using MFMailComposeViewController class. Most popular examples are fill an online form and press submit button. Here are the most popular methods i see in stackoverflow..

(1) Using your own SMTP client. This approach is working for me but in my private network but not working in my company premises as in company network I am not allowed to use gmail (public emails) and don't have SMTP details of our company mail server. I am struck here in this approach.

(2) Using CTCoreMessage & Three20 Framework (open source). But here I am not able to find where to download this framework other than git, where my pc VLAN not allowing GIT. Could somebody help me to find out the Three20 Framework downloadable location and share me an example code.

(3) Using HTTP Post: Send your email body to the server and delegate the email sending to the HTTP Server. If this is simple then can somebody help me how to setup your own HTTP Server for sending emails.

Could anybody help in finding any working sample code. I am at end of my project delivery. Posted this long back. No replies till now. Could anybody help me please..

2条回答
霸刀☆藐视天下
2楼-- · 2019-05-20 00:45

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
3楼-- · 2019-05-20 00:48

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.

查看更多
登录 后发表回答