【发布时间】:2015-04-11 13:41:56
【问题描述】:
目前我在尝试将 Azure 用作 SMTP 服务器时遇到问题。我正在尝试创建一个简单的联系表格,当您点击发送时会发送一封电子邮件。 PHP 代码很简单,并且可以在另一个服务器上运行,因为它来自以前的项目,但是我现在需要使用 Microsoft Azure 服务器,从我阅读的内容来看,我需要使用 cURL 或 sendmail API 调用。有谁知道如何做到这一点,因为我似乎无法让它工作。这是微软说你需要用来让 cURL 工作的代码,
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
// print everything out
print_r($response);
我想这比我看到的要简单得多,但是我究竟应该把我的 php 代码放在这个 cURL 代码中的什么地方才能让它工作呢?在天蓝色的一面,我还缺少什么吗?我已经在我的帐户上安装了 sendmail,这正是他们所说的我所需要的。
如果有帮助,这里是我的 php 代码
$url = 'https://api.sendgrid.com/';
$user = 'azure_0386579c9@azure.com';
$pass = 'password7';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => 'hidden@gmail.com',
'subject' => 'testing from curl',
'html' => 'testing body1',
'text' => 'testing body2',
'from' => 'hidden@gmail.com',
);
$request = $url.'api/mail.send.json';
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Contact Form';
$to = 'hidden@gmail.com';
$subject = 'Message from Contact Form ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName || !$errEmail || !$errMessage || !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
【问题讨论】:
-
在你的
curl_exec之后添加这个以获得一些额外的调试信息:if (curl_errno($session)) { echo 'Curl error: ' . curl_error($session); }。将其添加到您的问题中,看看我们是否可以解决此问题。来自php.net/manual/en/function.curl-errno.php -
感谢您的帮助,Tom,实际上我在本周开始时就设法让它工作了。把帖子的答案放在那里解释我的工作(和错误)感谢你的帮助:)