2015-01-02 23 views
6

Şu anda Azure'u bir SMTP sunucusu olarak kullanmaya çalışırken bir sorun yaşıyorum. Gönderdiğinizde bir e-posta gönderecek basit bir iletişim formu oluşturmaya çalışıyorum. PHP kodu basittir ve önceki bir projeden olduğu gibi başka bir sunucuda çalışır, ancak şimdi Microsoft Azure sunucusunu kullanmam gerekiyor ve okuduklarımdan cURL veya bir sendmail API çağrısı kullanmam gerekiyor. Bunu nasıl yapacağımı bilen biri, bunu işe yaramayacak gibi görünüyor. Bu, Microsoft Eğer cURL işe gitmek için kullanmak gerekir ki kod,Windows azure'da cURL ile php çağrısı

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

Bunu hayal sonra görebilirsiniz çok daha kolaydır, ama tam olarak bu cURL kodunda benim php kodu koymak nerde olsun çalışmak için? Şeylerin azure tarafında eksik olduğum başka bir şey var mı? Şimdiye kadar ihtiyacım olan her şeyi gönderdiğim için sendmail'i kurdum. Burada

eğer yardımcı olur benim php kodu zaten olan

bazı kötü kodlama uygulamaları görmek istiyor
$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

bazı ek hata ayıklama bilgilerini almak için 'curl_exec' sonra ekleyin:' (curl_errno ($ oturumu)) { 'Kıvrılma hatası:' echo eğer. curl_error ($ oturum); } '. Bunu sorunuza ekleyin ve bu sorunun çözülüp çözülmeyeceğini görelim. Http://php.net/manual/en/function.curl-errno.php adresinden – Tom

+0

Yardımlarınız için teşekkürler Tom, aslında haftanın başında çalışmayı başardım. Çalışmamı (ve hatalarım) yardım etmek için gösterdiğiniz çabayı takdir etmek için oradaki gönderiye bir cevap koyun :) –

cevap

5

??? Bu yüzden, çok fazla saç çekme ve araştırma yaptıktan sonra PHP formumun işe yaraması için bir yol buldum. Kodu kendiliğinden açıklayan değişkenler ile düzenleyeceğim, böylece kodu okuyacağım ve umarım, bazı şeylerin neden belirli yerlerin olduğu netleşecektir. Unutmayın, bu sadece bir pencere maskeleme sunucunuz varsa ve bir sebepten dolayı sunucu üzerinde çalışmak için bir php formuna ihtiyacınız varsa yararlıdır. Windows portalına sendmail'i kurmanız gerekiyor, ardından URL'yi, şifreyi ve kullanıcı adını almak için gerekli adımları takip ediyorsunuz. Hepsi bu kadar php dosyanıza gider. Şimdi bu olduğunu

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

(ben daha sonraki bir tarihte yeniden işleme edeceğiz işleri, benimki çalışıyor ama sadece bana pes diyerek orada tehlikeli şey birkaç kod gereksiz bit gidecekseniz) Çalıştığınız takdirde, kendi kullanımınız için kodumu almaktan çekinmeyin. Bu tür bir şey için windows azure kullanmamanızı ve sadece php 'mail' fonksiyonunun çok daha kolay çalıştığını fark ettiğim farklı bir sunucu almanızı öneririm. Aynı zamanda windows maskeleme ile ilgili diğer problemleri de iFrames ile duyarlı tasarım düzenlerini durdurarak buldum. (Bu olursa, sayfa kaynağınızı kontrol edin ve sayfa kaynağınızda bir bağlantı olup olmadığını kontrol edin, bağlantıyı izleyin ve yanıt sorununuzu çözüp çözmediğine bakın) Ve her zaman olduğu gibi yukarıdaki kodla ilgili herhangi bir sorunuz varsa lütfen bana e-posta göndermekten çekinmeyin. genellikle bir gün içinde size döneceğim.

Dex

+2

Merhaba, 2 gün sonra, yayınınızı buldum! Sadece kodunuzu biraz değiştirdim - ve sonunda büyük Azure PHP gizemini çözdüm. Hayatım sensiz sefil oldu. Çok teşekkürler! – ForeverLearning

+2

Çok teşekkür ederim! –