2015-01-02 13 views
6

hiện tại tôi đang gặp sự cố khi cố gắng sử dụng Azure làm máy chủ SMTP. Tôi đang cố tạo một biểu mẫu liên hệ đơn giản sẽ gửi email khi bạn nhấn gửi. Mã PHP đơn giản và hoạt động trên một máy chủ khác vì nó là từ một dự án trước đó, nhưng tôi cần sử dụng máy chủ Microsoft Azure ngay bây giờ và từ những gì tôi đọc tôi cần sử dụng cURL hoặc một cuộc gọi API sendmail. Có ai biết làm thế nào để làm điều này như tôi không thể có vẻ để làm cho nó hoạt động. Đây là mã mà Microsoft nói rằng bạn cần phải sử dụng để có được cURL để làm việc,Cuộc gọi php với cURL trong cửa sổ azure

// 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); 

tôi tưởng tượng đây là đơn giản hơn nhiều sau đó tôi có thể nhìn thấy, nhưng nơi chính xác để tôi đặt mã php của tôi trong mã cURL này để có được nó hoạt động? Có điều gì khác mà tôi đang thiếu ở phía bên phải của sự vật không? Tôi đã cài đặt sendmail trên tài khoản của tôi, đó là tất cả những gì họ nói tôi cần.

đây là mã php của tôi anyway nếu nó giúp

$url = 'https://api.sendgrid.com/'; 
$user = '[email protected]'; 
$pass = 'password7'; 

$params = array(
     'api_user' => $user, 
     'api_key' => $pass, 
     'to' => '[email protected]', 
     'subject' => 'testing from curl', 
     'html' => 'testing body1', 
     'text' => 'testing body2', 
     'from' => '[email protected]', 
    ); 

$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 = '[email protected]'; 
     $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>'; 
    } 
} 
    } 
+1

Thêm số này sau 'curl_exec' để nhận thêm một số thông tin gỡ lỗi:' if (curl_errno ($ session)) {echo 'Lỗi curl:'. curl_error ($ session); } '. Thêm điều đó vào câu hỏi của bạn và cho phép xem liệu chúng tôi có thể giải quyết vấn đề này hay không. Từ http://php.net/manual/en/function.curl-errno.php – Tom

+0

Cảm ơn sự giúp đỡ của bạn Tom, tôi thực sự quản lý để làm cho nó hoạt động ngay từ đầu tuần. Đặt câu trả lời cho bài đăng lên đó để giải thích hoạt động của tôi (và những sai lầm) đánh giá cao nỗ lực của bạn để giúp đỡ :) –

Trả lời

5

Ai muốn xem một số thông lệ mã hóa xấu ??? Vì vậy, sau khi nhiều tóc kéo và nghiên cứu tôi đã tìm ra một cách để có được hình thức PHP của tôi để làm việc. Tôi sẽ chỉnh sửa mã bằng cách tự giải thích các biến để đọc qua mã và hy vọng nó sẽ trở nên rõ ràng tại sao mọi thứ ở một số nơi nhất định. Hãy nhớ điều này chỉ hữu ích nếu bạn có một máy chủ cửa sổ xanh và bạn cần một biểu mẫu php để làm việc trên máy chủ vì một số lý do. Bạn cần phải cài đặt sendmail trên cổng thông tin cửa sổ của bạn sau đó bạn làm theo các bước để có được url, mật khẩu và tên người dùng. điều này tất cả đi vào tập tin php của bạn như vậy. (Tốt, tôi làm việc nhưng theres một vài bit dư thừa mã trong đó, không có gì nguy hiểm chỉ cho tôi nói rằng tôi bỏ cuộc, nó hoạt động, tôi sẽ làm lại nó vào một ngày sau)

$url = 'https://api.sendgrid.com/'; 
$user = 'this is provided user attribute'; 
$pass = 'password1'; 

$params = array(
     'api_user' => $user, 
     'api_key' => $pass, 
     'to' => '[email protected]', 
     'subject' => 'subject of the email', 
     'html' => 'I am the HTML parameter', 
     'text' => 'I am the text parameter', 
     'from' => $email, 
    ); 

$request = $url.'api/mail.send.json'; 

if ($_POST["submit"]) { 
     $name = $_POST['name']; 
     $email = $_POST['email']; 
     $message = $_POST['message']; 
     $human = intval($_POST['human']); 
     $from = "From: Contact Form"; 
     $mobile = $_POST['number']; 

     $to = '[email protected]'; 
     $subject = 'Message for subject line of email'; 

     $humanBool=66; 

     $body = "From: $name\n E-Mail: $email\n Message:\n $message"; 

     // now we go through some validation of the parts in the form 
     // to check everything was entered. In hindsight HTML 5 
     // 'required' attribute is much easier and fulfills exactly 
     // what I did here anyway. 
     // 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'; 
     }else{ 
      $humanBool = 66; 
     } 

     // If there are no errors in the data fields i.e. missing data 
     if (!$errName && !$errEmail && !$errMessage && !$errHuman) { 
      //and the human anti spam field is correct. 
      if($humanBool == 66){ 
       //do the email sending magic. 
       //create url for api call 
       // ready for that repetitive code? 
       $url = 'https://api.sendgrid.com/'; 
       //create array params for api call 
       //these params are what appear in the email that arrives into your email account. 
       $params = array(
        'api_user' => $user, 
        'api_key' => $pass, 
        'to'  => '[email protected]', 
        'subject' => 'Subject', 
        'html'  => "From: $name\n\r Message:\r\n $message", 
        'text'  => 'this is the text element', 
        'from'  => $email, 
       ); 

       // I don't why I concatenated this but one of the 
       // resources I used while researching said to do it. It 
       // worked, it's also unneccessary. $request is just 
       // https://api.sendgrid.com/api/mail.send.json. I think 
       // the founder of that article is just having a private 
       // joke at people using his code for help. 

       //concatenate api url to url above 
       $request = $url.'api/mail.send.json'; 

       //debugging 
       //$error = error_get_last(); 
       //echo this to see what errors are happening in the file 

       // Repetitive code..... 
       $url2 = 'https://api.sendgrid.com/api/mail.send.json'; 


       // Okay queue magic time. I'll explain it as overview 
       // here and you guys can step through after the 
       // explanation. 1) magic. 2) Sorcery. I don't quite get 
       // all of it so my explanation would be poor but I 
       // will say HOW it works overall. All previous arrays 
       // and variables are packaged up in one pack and then 
       // a service is called and they are sent as $result 



       // use key 'http' even if you send the request to https:// 
       $options = array(
        'http' => array(
         'header' => "Content-type: application/x-www-form-urlencoded\r\n", 
         'method' => 'POST', 
         'content' => http_build_query($params), 
        ), 
       ); 
       $context = stream_context_create($options); 
       $result = file_get_contents($url2, false, $context); 

       // debugging code if something goes wrong 
       // var_dump($result); 
       $result='<div class="alert alert-success">Thank You! I will be in touch</div>'; 

       // this is here to reset the page and clear the fields 
       // of the form once mail has been sent. 
       $page = $_SERVER['PHP_SELF']; 
       $sec = "3"; 
       header("Refresh: $sec; url=$page"); 

      }else{ 
        $result='<div class="alert alert-danger">Human checked failed</div>'; 
      } 


      }else{ 
       $result='<div class="alert alert-danger">Validation error</div>'; 
      } 
} 


?> 
// after this goes the HTML form here is one box from the form as its 
// all the same no need to repeat it all I think. 

<div class="form-group"> 

         <div class="col-xs-10 col-xs-offset-1"> 
          <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" style="text-transform:capitalize" value="<?php echo htmlspecialchars($_POST['name']); ?>" required> 
          <?php echo "<p class='text-danger'>$errName</p>";?> 
         </div> 

Bây giờ tôi có điều này làm việc ra xin vui lòng lấy mã của tôi để sử dụng riêng của bạn nếu bạn cần. Tôi khuyên bạn không nên sử dụng windows azure cho loại điều này và chỉ nhận được một máy chủ khác, nơi chức năng 'mail' của php hoạt động dễ dàng hơn nhiều, tôi cũng tìm thấy các vấn đề khác với windows azure để làm với iFrames ngừng bố trí thiết kế đáp ứng (kiểm tra nguồn trang của bạn nếu điều đó xảy ra và xem liệu có liên kết trong nguồn trang của bạn hay không, hãy nhấp vào liên kết và xem liệu có giải quyết được sự cố đáp ứng của bạn không) Và như mọi khi có bất kỳ câu hỏi nào về mã ở trên, vui lòng gửi email cho tôi thường sẽ lấy lại cho bạn trong vòng một ngày.

Dex

+2

Xin chào, sau 2 ngày, tôi đã tìm thấy bài đăng của bạn! Chỉ cần sửa đổi một chút mã của bạn ngay bây giờ - và cuối cùng đã giải quyết được bí ẩn Azure PHP tuyệt vời. Cuộc sống của tôi đã khốn khổ mà không có bạn. Cảm ơn rất nhiều! – ForeverLearning

+2

Cảm ơn bạn rất nhiều! –

Các vấn đề liên quan