2015-01-02 12 views
6

現在AzureをSMTPサーバーとして使用しようとすると問題が発生しています。私はあなたが送信したときに電子メールを送信する簡単な連絡フォームを作成しようとしています。 PHPコードはシンプルで、以前のプロジェクトのものと同じように別のサーバー上で動作しますが、Microsoft Azureサーバーを使用する必要があります。また、cURLまたはsendmail API呼び出しを使用する必要があります。私はそれを動作させるように見えないので、誰もこれを行う方法を知っていますか?これは、マイクロソフトでは、カールが仕事を得るために使用する必要があると言うコード、WindowsでcURLを使用したphp呼び出し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); 

私はこれを想像するが、私は見ることができますはるかに簡単ですが、まさに私は、このcURLのコードで私のPHPコードを入れない場所を取得するにはそれは働く?私は物事の青空の側にも欠けているものがありますか?私はすでに自分のアカウントにsendmailをインストールしています。それはいくつかの悪いコーディングプラクティスを見たい

$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

いくつかの追加のデバッグ情報を取得するために、あなたの 'curl_exec'後にこれを追加:'場合(curl_errno($セッション)){エコー 'カールエラー:' を。 curl_error($ session); } '。あなたの質問にそれを加えて、この問題を解決できるかどうかを見てみましょう。 http://php.net/manual/en/function.curl-errno.php – Tom

+0

あなたの助けてくれてありがとうTom、私は実際に週の初めにそれを稼働させることができました。あなたの努力(と間違い)を助けるためにあなたの努力を感謝するためにそこに投稿のための答えを入れて: –

答えて

5

を助けている場合、ここで

は私のPHPコードは、とにかくです?だから、たくさんの髪を引っ張って研究した後、PHPフォームを動作させる方法を見つけました。自己説明変数を使ってコードを編集してコードを読み、うまくいけば、物事が特定の場所にある理由が明らかになるはずです。これは、Windows Azureサーバーがあり、何らかの理由でサーバーで作業するためにPHPフォームが必要な場合にのみ便利です。 Windowsのポータルにsendmailをインストールして、URL、パスワード、ユーザー名を取得する手順を実行する必要があります。このようにすべてはあなたのPHPファイルに入ります。今、私はこれを持っていることを

$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> 

を(それは私が後日それを手直しよ、働き、よく、鉱山は動作しますが、ちょうど私が私があきらめると言って、そこに危険なものをコードするいくつかの冗長ビットをtheresの)あなたが必要な場合は、自分の使用のために自分のコードを持って自由に感じてください。私は徹底的にWindows Azureを使用せず、php 'mail'機能がはるかに簡単な別のサーバを手に入れることをお勧めします。また、iFramesがレスポンシブなデザインレイアウト(あなたのページソースをチェックし、あなたのページソースのリンクを見て、リンクをたどってそれがあなたの反応の問題を解決するかどうかを見てください)そして、いつものように誰かが上記のコードに関する質問をしているなら、通常、1日以内にあなたに戻ってきます。

デックス

+2

こんにちは、2日後、私はあなたの記事を見つけた!あなたのコードをちょっと修正しました。そして、ついに偉大なAzure PHPミステリーを解決しました。私の人生はあなたなしでは惨めです。どうもありがとう! – ForeverLearning

+2

ありがとうございました! –

関連する問題