1 回答
TA贡献1828条经验 获得超4个赞
以下是通过远程服务器上的 PHP 文件发送电子邮件的示例。
// View controller //
import UIKit
class HomeViewController: UIViewController {
@IBAction func sendTapped(_ sender: UIButton) {
DispatchQueue.global().async() {
var request = URLRequest(url: URL(string: "https://www.MyApp.com/file.php")!)
request.httpMethod = "POST"
let to = "tom123@apple.com"
let sub = "Just testing..."
let msg = "How are you doing?"
let from = "George H. Aniston <ghaniston@gmail.com>"
let postString = "a=\(to)&b=\(sub)&c=\(msg)&d=\(from)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
}
task.resume()
}
}
}
// file.php //
<?php
$to = $_POST['a'];
$sub = $_POST['b'];
$msg = $_POST['c'];
$from = $_POST['d'];
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
$headers = 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// send email
mail($to, $sub, $msg, $headers);
?>
- 1 回答
- 0 关注
- 95 浏览
添加回答
举报