简体   繁体   中英

html contact form on azure hosted web app

I have a basic HTML template that came with phpmailer which uses STMP

The code was setup like this straight from unzipping.

<?php
session_start();

include("php/simple-php-captcha/simple-php-captcha.php");
include("php/php-mailer/PHPMailerAutoload.php");

// Step 1 - Enter your email address below.
$to = 'you@domain.com';

if(isset($_POST['emailSent'])) {

    $subject = $_POST['subject'];

    // Step 2 - If you don't want a "captcha" verification, remove that IF.
    if (strtolower($_POST["captcha"]) == strtolower($_SESSION['captcha']['code'])) {

        $name = $_POST['name'];
        $email = $_POST['email'];

        // Step 3 - Configure the fields list that you want to receive on the email.
        $fields = array(
            0 => array(
                'text' => 'Name',
                'val' => $_POST['name']
            ),
            1 => array(
                'text' => 'Email address',
                'val' => $_POST['email']
            ),
            2 => array(
                'text' => 'Message',
                'val' => $_POST['message']
            ),
            3 => array(
                'text' => 'Checkboxes',
                'val' => implode($_POST['checkboxes'], ", ")
            ),
            4 => array(
                'text' => 'Radios',
                'val' => $_POST['radios']
            )
        );

        $message = "";

        foreach($fields as $field) {
            $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
        }

        $mail = new PHPMailer;

        $mail->IsSMTP();                                      // Set mailer to use SMTP
        $mail->SMTPDebug = 0;                                 // Debug Mode

        // Step 4 - If you don't receive the email, try to configure the parameters below:

        //$mail->Host = 'mail.yourserver.com';                // Specify main and backup server
        //$mail->SMTPAuth = true;                             // Enable SMTP authentication
        //$mail->Username = 'username';                       // SMTP username
        //$mail->Password = 'secret';                         // SMTP password
        //$mail->SMTPSecure = 'tls';                          // Enable encryption, 'ssl' also accepted

        $mail->From = $email;
        $mail->FromName = $_POST['name'];
        $mail->AddAddress($to);
        $mail->AddReplyTo($email, $name);

        $mail->IsHTML(true);

        $mail->CharSet = 'UTF-8';

        $mail->Subject = $subject;
        $mail->Body    = $message;

        // Step 5 - If you don't want to attach any files, remove that code below
        if (isset($_FILES['attachment']) && $_FILES['attachment']['error'] == UPLOAD_ERR_OK) {
            $mail->AddAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name']);
        }

        if($mail->Send()) {
            $arrResult = array('response'=> 'success');
        } else {
            $arrResult = array('response'=> 'error', 'error'=> $mail->ErrorInfo);
        }

    } else {

        $arrResult['response'] = 'captchaError';

    }

}
?>

After setting every variable up, including all of Step 4. I continue to get SMTP connect() failed. even with debugging 3. Our sales email uses Outlook.

So one of the problems I think, is that this website is hosted on Azure.

Another thing I wanted to do is setup office 365 using their API for the contact form. There's a bit of javascript code but the authContext = new O365Auth.Context(); is not recognized.

We just need a way to fill out a basic contact form, and have it emailed to our sales@email.com I've run out of ideas to complete this. It doesn't matter how, but I'm new to azure and i'm stuck.

Not sure about integrating with other SMTP servers but you can use SendGrid for this task and it's free up to 25k messages. Just browse Azure Marketplace from the portal and add it to the Azure subscription.

https://azure.microsoft.com/en-us/marketplace/partners/sendgrid/sendgrid-azure/

The integration using PHP is very simple. They recommend their PHP library, but you can use Curl for this job:

<?php

$url = 'https://api.sendgrid.com/';
$user = 'USERNAME';
$pass = 'PASSWORD';

$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => 'example3@sendgrid.com',
    'subject'   => 'testing from curl',
    'html'      => 'testing body',
    'text'      => 'testing body',
    'from'      => 'example@sendgrid.com',
  );


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

// 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);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// obtain response
$response = curl_exec($session);
curl_close($session);

// print everything out
print_r($response);

?>

https://sendgrid.com/docs/Integrate/Code_Examples/php.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM