2017-06-12 121 views
-1

我是網絡開發的新手。我寫了一個php代碼來發送表單中的電子郵件,並將其託管在雲端。每當我點擊提交按鈕時,我都會收到HTTP ERROR 500。phpmailer在嘗試在雲端上測試電子郵件時出現HTTP ERROR 500

<?php 

    if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    # code... 
     $name = trim($_POST["fullname"]); 
     $email = trim($_POST["email"]); 
     $phone_no = trim($_POST["phone_num"]); 
     $partner_type = $_POST["Partner_type"]; 

    if ($name == "" || $email == "" || $phone_no == " ") { 
     echo "please fill the required fields name , email , phone number"; 
     } 

     if ($_POST["adds"] != "") { 
     echo "Bad form input"; 
     exit; 
     } 

     require 'class.phpmailer.php'; 

     $mail = new PHPMailer(); 

     if (!$mail->ValidateAddress($email)) { 
     # code... 
     echo "Invalid Email Address"; 
     exit; 
     } 


     $email_body = ""; 
     $email_body .= "Name " . $name . "\n"; 
     $email_body .= "Email " . $email . "\n"; 
     $email_body .= "Phone Number " . $phone_no . "\n"; 
     $email_body .= "Partner Type " . $partner_type . "\n"; 


     $mail->setFrom($email, $name); 
     $mail->addAddress('[email protected]', 'gbubemi');  // Add a recipient 

    /**$mail->addAttachment('/var/tmp/file.tar.gz');   // Add attachments 
     $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name 
     **/ 

     $mail->isHTML(false);         // Set email format to HTML 

     $mail->Subject = 'Become a Partner ' . $name ; 
     $mail->Body = $email_body; 
     $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

     if(!$mail->send()) { 
      echo 'Message could not be sent.'; 
      echo 'Mailer Error: ' . $mail->ErrorInfo; 
      exit; 
     } else { 
      echo 'Message has been sent'; 
     } 

    header("location:index.php?status=thanks"); 
} 

?> 

請真的很沮喪,我需要幫助。謝謝。

+1

檢查你的PHP錯誤日誌。 –

+0

...請註冊https://codereview.stackexchange.com/ - 您的代碼不好。 – symcbean

回答

0

你應該使用包中的函數PHPMailerAutoload ..它很簡單,工作得很好。我自己在一個項目上使用它。另外,它很容易被吸收。

function sendMail($destinaire, $code, $prename){ 

require 'phpmailer/PHPMailerAutoload.php'; 

$userMail = $destinataire; //Recipient 
$mail = new PHPMailer(); 
$mail->Host = "smtp.gmail.com"; 
$mail->isSMTP(); 
$success_send = " Le code de reservation a éte envoyé à ".$userMail; 
$failed_send = "Echec: le mail in not send verifier votre email"; 
//On ouvre l'authentification 
$mail->SMTPAuth = true; 

//Les creditentials 
$mail->Username = "[email protected]"; 
$mail->Password = "Senderpass"; 
//set the type of protection 
$mail->SMTPSecure ="ssl"; //tls 
//Definissons un port 
$mail->Port ="465"; //587 pour tls 
//L'objet du message 
$mail->Subject ="Reservation enregistré"; 
//Le message en lui meme, afin le contenu quoi 
$mail->Body ="Bonjour '.$prename.' Nous avons enregistré votre reservation. 
Veuillez presenter le Code suivant le jour du voyage ".$code; 

//Bref, qui envoi? 
$mail->setFrom('[email protected]', 'Indiza - Travel Dimension'); 
//set where we are sending this email(à qui) 
$mail->addAddress($userMail); 
//Envoyer l'email 

if($mail->send()) 
    return $success_send; 
else 
    return $failed_send;   
} 

實用

要啓動(如果您使用Gmail),你應該降低發送賬號的安全級別的信息

https://support.google.com/accounts/answer/6010255?hl=en

Github Php Mailer

而且如果有必要,這個鏈接將非常有用Codingpassive/PHPMailer

就這樣調用

$idz = sendMail("$receiverMail","anyCode","receiverName"); 

的功能是不是真的電腦?沒有紳士,我們做工藝品。

Est-ceréellementde l'informatique?非moni ami,在fait juste du bricolage上。

1

首先,您需要調試應用程序天氣電子郵件是否正在發送或可能是卡在隊列中。你可以通過在終端上運行「mailq」來檢查它。接下來的第一次嘗試發送簡單的電子郵件上Cloudways這樣的:

<?php 
require_once "vendor/autoload.php"; //PHPMailer Object 
$mail = new PHPMailer; //From email address and name 
$mail->From = "[email protected]"; 
$mail->FromName = "Full Name"; //To address and name 
$mail->addAddress("[email protected]", "Recepient Name");//Recipient name is optional 
$mail->addAddress("[email protected]"); //Address to which recipient will reply 
$mail->addReplyTo("[email protected]", "Reply"); //CC and BCC 
$mail->addCC("[email protected]"); 
$mail->addBCC("[email protected]"); //Send HTML or Plain Text email 
$mail->isHTML(true); 
$mail->Subject = "Subject Text"; 
$mail->Body = "<i>Mail body in HTML</i>"; 
$mail->AltBody = "This is the plain text version of the email content"; 
if(!$mail->send()) 
{ 
echo "Mailer Error: " . $mail->ErrorInfo; 
} 
else { echo "Message has been sent successfully"; 
} 
if(!$mail->send()) 
{ 
echo "Mailer Error: " . $mail->ErrorInfo; 
} 
else 
{ 
echo "Message has been sent successfully"; 
} 

如果它的工作原理,然後電子郵件發送嘗試通過應用SMTP憑證這樣的發送:

<?php 
require_once "vendor/autoload.php"; 

$mail = new PHPMailer; 

//Enable SMTP debugging. 

$mail->SMTPDebug = 3;                            

//Set PHPMailer to use SMTP. 

$mail->isSMTP();         

//Set SMTP host name                       

$mail->Host = "smtp.gmail.com"; 

//Set this to true if SMTP host requires authentication to send email 

$mail->SMTPAuth = true;                       

//Provide username and password 

$mail->Username = "[email protected]";              

$mail->Password = "super_secret_password";                        

//If SMTP requires TLS encryption then set it 

$mail->SMTPSecure = "tls";                        

//Set TCP port to connect to 

$mail->Port = 587;                     

$mail->From = "[email protected]"; 

$mail->FromName = "Full Name"; 

$mail->addAddress("[email protected]", "Recepient Name"); 

$mail->isHTML(true); 

$mail->Subject = "Subject Text"; 

$mail->Body = "<i>Mail body in HTML</i>"; 

$mail->AltBody = "This is the plain text version of the email content"; 

if(!$mail->send()) 

{ 

echo "Mailer Error: " . $mail->ErrorInfo; 

} 

else 

{ 

echo "Message has been sent successfully"; 

} 
?> 

您還可以設置SMTP上Cloudways平臺的服務器設置: SMTP settings for cloudways

這裏閱讀完整的文章:https://www.cloudways.com/blog/send-emails-in-php-using-phpmailer/

+0

好的謝謝你的工作 –

+0

@gbubemismith很高興爲你工作。將其標記爲正確答案plz :) –

相關問題