问题
I want to use the PHP mail function to send a HTML form to my mail. When I run the code it no error occurs, but I don't receive an email. I used the following code:
<?php
$to = 'email@email.com';
$subject = 'Subject';
$message = 'Message here';
$headers = 'From: email@email.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
I'm hosting my web app on Microsoft Azure with PHP 7.0.
回答1:
I assume Microsoft turned PHP mail off. (many hosting providers do)
Microsoft says that you should use SendGrid. You can read the full tutorial here: https://azure.microsoft.com/en-us/documentation/articles/store-sendgrid-php-how-to-send-email/
回答2:
I've tried to use the php mail() function but I can't get it working so I've searched for some answers and this works:
https://github.com/PHPMailer/PHPMailer
You can use it when you will send a mail to a gmail account or for local email servers.
Notes:
Make sure your path for PHPMailerAutoload.php is correct when you are requiring. For example:
require 'assets/api/PHPMailer-master/PHPMailerAutoload.php';
You must know the host name if you are going to send to a local email server.
You must have an account that you can use to send a mail.
Analyze how the code works and feel free to comment for further questions.
I'll attach a sample code here from a website I developed.
<?php
$strFullname = $strEmail = $strMobile = $strPosition = "";
require 'assets/api/PHPMailer-master/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer(true);
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
//$mail->SMTPDebug = 1;
//Ask for HTML-friendly debug output
//a$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'secure.emailsrvr.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMsTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "myemail@mailserver.com";
//Password to use for SMTP authentication
$mail->Password = "myaccountpassword";
//Set who the message is to be sent from, you can use your own mail here
$mail->setFrom('bpsourceph@gmail.com', '@noreply.bpsource.com');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('testmail@mailserver.com', 'Firstname Lastname');
//Set the subject line
$mail->Subject = 'New application form sent from ***** Career page';
$mail->IsHTML(true);
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$strFullname = $_POST['strFullname'];
$strEmail = $_POST['strEmail'];
$strMobile = $_POST['strMobile'];
$strPosition = $_POST['strPosition'];
//This part is where you will create your mail
$mail->msgHTML("Fullname: ".$strFullname."\nEmail: ".$strEmail."\nMobile Number: ".$strMobile."\nDesired Position: ".$strPosition);
//This part is for sending the mail
if (!$mail->send()) {
//If you want to check for errors. Uncomment the line below.
//echo "Mailer Error: " . $mail->ErrorInfo;
echo "<script>alert('Some error occured. Please try again later');</script>";
header("Refresh:2");
}
echo "<script>alert('Application form successfully sent!');</script>";
header("Refresh:2");
}
?>
Hope I am getting things clear for you. Regards! Goodluck!
来源:https://stackoverflow.com/questions/38375691/how-to-send-php-mail-function-on-azure-web-app