2011-04-08 31 views
-1

這是我工作的代碼;然而,我想合併一種方式來使用我的Gmail帳戶進行SMTP身份驗證,但我無法弄清楚......幫助?您如何使用gmail驗證PHP聯繫表單?

<?php 

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

    $email_to = "[email protected]"; 
    $email_subject = "Website Inquire"; 


    function died($error) { 
     echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation expected data exists 
    if(!isset($_POST['first_name']) || 
     !isset($_POST['last_name']) || 
     !isset($_POST['email']) || 
     !isset($_POST['telephone']) || 
     !isset($_POST['comments'])) { 
     died('We are sorry, but there appears to be a problem with the form you submitted.');  
    } 

    $first_name = $_POST['first_name']; // required 
    $last_name = $_POST['last_name']; // required 
    $email_from = $_POST['email']; // required 
    $telephone = $_POST['telephone']; // not required 
    $comments = $_POST['comments']; // required 

    $error_message = ""; 
    $email_exp = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/"; 
    if (preg_match($email_exp, $email_from)) { 
    echo "Email address is valid."; 
    } 
    else 

    { 
     echo "Email address is <u>not</u> valid."; 
    } 
    $string_exp = "/^[a-zA-Z .'-]+$/"; 
    if(!preg_match($string_exp,$first_name)) { 
    $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
    } 
    if(!preg_match($string_exp,$last_name)) { 
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; 
    } 
    if(strlen($comments) < 2) { 
    $error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
    } 
    if(strlen($error_message) > 0) { 
    died($error_message); 
    } 
    $email_message = "Form details below.\n\n"; 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message .= "First Name: ".clean_string($first_name)."\n"; 
    $email_message .= "Last Name: ".clean_string($last_name)."\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\n"; 
    $email_message .= "Telephone: ".clean_string($telephone)."\n"; 
    $email_message .= "Comments: ".clean_string($comments)."\n"; 



$headers = 'From: '.$email_from."\n". 
'Reply-To: '.$email_from."\n". 
'X-Mailer: PHP/'.phpversion(); 

@mail($email_to, $email_subject, $email_message, $headers); 

} 

?> 
+0

我剛纔說我以爲這個代碼包含了每一個PHP電子郵件反模式的想法,但是你缺少頭注入。該死。它也是關於通過Gmail發送來自PHP的郵件的[至少** * ** **的幾個*]的複本(http://stackoverflow.com/search?q=%5Bphp%5D+%2Bgmail+%2Bsmtp) SMTP服務器。 – Charles 2011-04-08 00:11:23

回答

0

你想使用一個現代的PHP郵件庫,如SwiftMailer,它支持SMTP認證。

查看Google's instructions on which servers to use和SwiftMailer的文檔sending mail to a SSL/TLS SMTP serversending mail with a username/password combo

在更新代碼中使用SwiftMailer,你應該溝,一個電子郵件驗證正則表達式的可憎,它排除了許多完全有效的本地零部件和域。由building the message using method calls強加的理智直夾克將會處理你的代碼中的大部分其他潛在問題,比如奇怪的clean_string函數 - 你不需要它(我不確定你認爲它在這裏做什麼,但它並不真正,呃,讓字符串更安全)。

+0

我不明白我應該用這個swiftmailer的東西。我下載了,但我不太瞭解php或與之相關的任何信息。我只知道css和html。 – user697771 2011-04-08 00:51:50

0

你可以使用PHPMailer http://sourceforge.net/projects/phpmailer/,這裏是你如何使用GMail發送電子郵件,但HTML & CSS在這裏幫不了你。

function email($to, $subject, $body){ 
    require_once("class.phpmailer.php"); 

    $mail = new PHPMailer(); 

    $mail->SMTPAuth = true; 
    $mail->SMTPSecure = "ssl"; 
    $mail->Host = "smtp.gmail.com"; 
    $mail->Port = 465; 
    $mail->Username = "[email protected]"; 
    $mail->Password = "123456"; 

    $mail->SetFrom("[email protected]", "Name Example"); 

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

    $mail->AddAddress($to); 
    $mail->Send(); 
    unset($mail); 
} 
0

我想用gmail;谷歌更改那裏的身份驗證方法,我能夠成功驗證以下第一步: Video link這是2步驟的過程,允許外部應用程序

您將需要生成一個應用程序的密碼。

然後將該密碼用於您的PHP應用程序。

0
This is a complete working example.  
<?php 
    // this is for troubleshooting errors 
    // set the 1 to 0 to turn off (must set to 0 when in production mode or when you run this live 
    error_reporting(E_ALL); 
    ini_set('display_errors', '1'); 
?><?php 
    if(isset($_POST['email'])) { 
     $email_to  = $_POST['email']; 
     $email_subject = "E-mail from website visitor."; 

     // try adding a useful/relevent message to errors output to users 
     function died($error) { 
      echo "We are very sorry, but there were error(s) found with the form you submitted:\n\t "; 
      echo $error."<br /><br />"; 
      echo "Please go back and fix these errors.<br /><br />"; 
      die(); 
     } 

     // validation expected data exists 
     // since all post are submit, check if they are empty or equal to an empty string 
     if(isset($_POST['email'])){ 
      if($_POST['email'] == "" || $_POST['first_name'] == "" || $_POST['last_name'] == "" || $_POST['telephone'] == "" || $_POST['comments'] == ""){ 
       died('Please fill out the entire form'); 
      } 
     } 

     $first_name = $_POST['first_name']; // required 
     $last_name = $_POST['last_name']; // required 
     $email_from = $_POST['email']; // required 
     $telephone = $_POST['telephone']; // not required 
     $comments = $_POST['comments']; // required 

     $error_message = ""; 
     $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
     $string_exp = "/^[A-Za-z .'-]+$/"; 

     if(!preg_match($email_exp,$email_from)) 
      $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
     if(!preg_match($string_exp,$first_name)) 
      $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
     if(!preg_match($string_exp,$last_name)) 
      $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; 
     if(strlen($comments) < 2) 
      $error_message .= 'The Message you entered do not appear to be valid.<br />'; 
     if(strlen($error_message) > 0) 
      died($error_message); 
     $email_message = "Form details below.\n\n"; 

     function clean_string($string) { 
      $bad = array("content-type","bcc:","to:","cc:","href"); 
      return str_replace($bad,"",$string); 
     } 
     $email_message .= "First Name: ".clean_string($first_name)."\n"; 
     $email_message .= "Last Name: ".clean_string($last_name)."\n"; 
     $email_message .= "Email: ".clean_string($email_from)."\n"; 
     $email_message .= "Telephone: ".clean_string($telephone)."\n"; 
     $email_message .= "Comments: ".clean_string($comments)."\n"; 

     // create email headers 
     // changed up your email a bit 
     $headers = "From: $email_from\n"; 
     $headers.= "MIME-Version: 1.0\n"; 
     $headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
     mail($email_to, $email_subject, $email_message, $headers); 
     header('Location: http://www.cnn.com'); 
     exit(); 
    } 
?> 
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> 
    Firstname:<br> 
    <input type="text" name="first_name"><br> 
    Lastname:<br> 
    <input type="text" name="last_name"><br> 
    Email:<br> 
    <input type="text" name="email"><br> 
    Phone Number:<br> 
    <input type="text" name="telephone"><br> 
    Comments:<br> 
    <input type="textarea" name="comments"><br><br> 
    <input type="submit" value="Submit"> 
</form> 
+0

您可以更改標題以將用戶重定位到您的登錄腳本或主頁,而不是cnn。 – les 2014-01-02 02:24:08