我也許能提供第三種選擇一些幫助,使用HTTP 後,因爲我做的一個項目一旦實現它。
首先,我使用this nice and simple iOS class來照顧我的發帖。 接着,下面的iOS代碼段應你展示它是如何做
NSString* from = @"[email protected]";
NSString* to = @"[email protected]";
NSString* mailCc = @"[email protected]";
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");
}
對於PHP文件,這應該做的伎倆
$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");
}
請記住,我不是PHP的專家,所以該代碼可能會得到改進,特別是在安全部分。
PHP郵件應該已經在大多數主要託管解決方案中啓用,無論是廉價的共享帳戶,VPS還是專用服務器。但是,如果您打算使用此方法發送大量電子郵件,則建議使用專用服務器。
但是,您可以發送的電子郵件數量有限,且功能比mail
功能更好。你可以找到更多關於這個here的信息。
[稍後編輯] 似乎作者刪除了SimplePost類。然而,同一作者提出了一個應該幫助的替代方案,稱爲SimpleHTTPRequest。其餘應該保持不變
嗨,對不起,遲到的迴應。我想這種實現方式需要一個專用的網絡服務器。你能粗略地告訴我設置這臺服務器需要多少努力嗎?也沒有服務器設置,我不能嘗試這種方法,我猜。 – Srivathsa
檢查我的編輯。簡短的回答是:並不是真的,你可以毫不費力地做到這一點,但主要取決於你將發送多少封電子郵件。我使用這種方法的內部應用程序,可能在極端情況下,每小時會發送幾十封電子郵件,所以這不是問題 – BBog
嗨,m測試你的代碼,你想告訴我什麼是「SimplePost」,因爲使用「NSMutableURLRequest *請求= [SimplePost urlencodedRequestWithURL:url」 –