2016-07-14 45 views
0

我想使用PHP郵件功能將HTML表單發送到我的郵件。 當我運行代碼時,它沒有發生錯誤,但我沒有收到電子郵件。 我用下面的代碼:PHP郵件功能Azure網絡應用程序

<?php 
$to  = '[email protected]'; 
$subject = 'Subject'; 
$message = 'Message here'; 
$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

mail($to, $subject, $message, $headers); 
?> 

我主持的微軟Azure我的web應用程序與PHP 7.0。

+0

請指出你遇到什麼問題以及哪部分代碼失敗。 –

+0

我沒有收到我的收件箱中的電子郵件。我沒有得到任何錯誤,所以我猜代碼沒有失敗? – Dutch

回答

0

這是PHP的默認郵件代碼。可能是郵件來自垃圾郵件而不是索引。

對於索引郵件,YOu需要使用PHPMailer。

https://github.com/PHPMailer/PHPMailer

此鏈接將有助於爲發送郵件代碼...

0

我試圖使用PHP 郵件()函數,但我不能讓它工作,所以我」 VE搜索一些答案和這個作品:

https://github.com/PHPMailer/PHPMailer

你可以使用它時,你會郵件發送到Gmail帳戶或本地電子郵件服務器。

注:

確保您的PHPMailerAutoload.php路徑是正確的,當你需要。例如:

require 'assets/api/PHPMailer-master/PHPMailerAutoload.php'; 

你必須知道主機名,如果你要發送到本地電子郵件服務器。

您必須擁有一個可用於發送郵件的帳戶。

分析代碼是如何工作的,並隨時對其他問題發表評論。

我會在我開發的網站上附上一個示例代碼。

<?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 = "[email protected]"; 

//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('[email protected]', '@noreply.bpsource.com'); 

//Set an alternative reply-to address 
//$mail->addReplyTo('[email protected]', 'First Last'); 

//Set who the message is to be sent to 
$mail->addAddress('[email protected]', '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"); 
} 

?> 

希望我明白你的意思。問候!祝你好運!

+0

謝謝你有用的答案,它現在正在工作:D – Dutch

+0

@Dutch我很高興我幫助:) Goodluck! –