Who wants to see some bad coding methods ??? So, after repeatedly pulling my hair and researching, I figured out how to make my PHP form work. I will edit the code using the explanatory variables themselves, so read the code and, I hope, he will understand why everything happens in certain places. Remember that this is only useful if you have an azure server for Windows, and for some reason you need a PHP form to work on the server. You need to install sendmail on your Windows portal, then follow the steps to get the URL, password and username. all this is included in your php file. (well, my work works, but there are a few extra bits of code, nothing dangerous, only I say that I give up, it works, I will rework it later)
$url = 'https://api.sendgrid.com/'; $user = 'this is provided user attribute'; $pass = 'password1'; $params = array( 'api_user' => $user, 'api_key' => $pass, 'to' => ' thisiswhereitsgoingto@gmail.com ', '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 = ' thisiswhereitsgoing@gmail.com '; $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 ie 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' => ' whereEmailIsGoing@gmail.com ', '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 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>
Now that I have developed this, feel free to take my code for your purposes if you need to. I highly recommend you not to use windows azure for this kind of thing and just get another server running the php 'mail' function, which is much simpler, I also found other problems with azure windows that can be done with iFrames that stop responsive design layouts (check the source of the page if this happens, and see if there is a link in the source of your page, follow the link and see if it solves your responsive problem). And as always, if anyone has any questions about the code above, please write to me. I usually come back to you during the day.
Dex