如何通过托管在azure上的PHP联系表单接收电子邮件? [关闭]

问题描述:

I'm not receiving emails from my contact form on my website and not sure how to fix this? The website is hosted on Microsoft Azure. How would I go about resolving this?

The code I currently have is:

<?php
if (isset($_POST['contact_name']) && isset($_POST['contact_email']) 
&& isset($_POST['contact_text'])) {
$contact_name = $_POST['contact_name'];
$contact_email = $_POST['contact_email'];
$contact_text = $_POST['contact_text'];
if(!empty($contact_name) && !empty($contact_email) && !empty($contact_text)) {

    $to = 'email@domain.com';
    $subject = 'Message from website.';
    $body = $contact_name."
".$contact_text;
    $headers = 'From: ' .$contact_email;

    if(@mail('email@domain.com', $contact_text, $body, $headers)) {
        echo 'Thank you';
    } else {
        echo 'error.';
    }
}
else {
    echo 'You're missing something';
     }
   }
  ?>

<form action="" method="POST">
    Name: <input type="text" name="contact_name">
    Email: <input type="text" name="contact_email">
Message: <textarea name="contact_text" rows="5" cols="20"></textarea>
<input type="submit" value="Send">

</form>

Thank you.

Azure Web Apps Service doesn't provide the STMP server and also we do not have permission to build one on it, so if you remove the @, you may get the exception like: Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set().

To send mails from the PHP applications on Azure Web Apps, you can leverage some other 3rd part mail services like SendGrid.

You can easily create a SendGrid Account with Azure management portal. Please refer to https://azure.microsoft.com/en-us/documentation/articles/store-sendgrid-php-how-to-send-email/ for details.

Any further concern, please feel free to let me know.