2011-01-11 222 views
2

我試圖通過PHP表單郵件將我的網站訪問者和電子郵件發送給我的工作室,然後發送一些說明和提示。 (我正在簡化一些表單字段)通過PHP表格發送HTML電子郵件

但是,HTML格式不能正常工作....我沒有正確地聲明MIME類型的字符集?

<?php 

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

      //if (empty ($_POST['name']) || empty($_POST['email'])) 
      //{ 
      //echo"<div class='error'>Error<br />You did not fill in a required field, please review your form and correct the missing information. <a class='close' href='#'>close</a></div>"; 
      //} 

      $name = $_POST['name']; 
      $email = $_POST['email']; 
      $email2 = $_POST['email2']; 
      //A bunch of other fields are here 

      //Additional Headers 

      $headers = 'MIME-Version: 1.0' . "\r\n"; 
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 


      //$yoursite = "My Site"; 
      //$youremail = $email; 

      $subject = "Website Form"; 
      $message = " 
      $name would like you to contact them about your service. 
      Contact PH: $phone 
      Email: $email 
      Legal Guardian: $legal    
      //more stuff here 
      "; 


      $subject2 = "Directions and Information"; 

          $message2 = "<html><head></head><body> 
      $message2 .= "<h1>Directions</h1> 

      <p>text</p> 

      <p><a href='http://example.com/schedules'>Click here</a> 

      <h2>How Do I find your Photo Studio?</h2> 

      <h2>What do I have to bring with me?</h2> 
      </p>"; 

      $message2 .= "</body></html>"; 



      $email3 = "[email protected]"; 
      $email4 = "[email protected]"; 




      //This email sends their details to me from the visitor 
      mail($email3, $subject, $message, "From: $email"); 


          //This email sends directions to the visitor from me 
      mail($email, $subject2, $message2, "From: $email4"); 

      echo"<div class='thankyou'>Thank you for contacting us,<br /> we will respond as soon as we can.</div>"; 


      } 
      ?> 
+2

你甚至沒有使用`$ headers`變量。加上你有一個無與倫比的雙重報價'$ message2 =「`我會建議使用一個優良的,免費的郵件類,例如PHP梅勒,斯威夫特梅勒。 – webbiedave 2011-01-11 22:25:07

回答

2

有很多隨機的垃圾,需要做一個電子郵件正確發送。我通常傾向於將所有責任外包給野外存在的預包裝類,如http://swiftmailer.org/

也許有人會有更好的類提供。

+0

這看起來更可管理,比我想要問題...在 - > setFrom(array('[email protected]'))變量中,我如何設置這個來拉取在「email」字段中輸入的數據? – 2011-01-11 23:28:37

0

我發誓PEAR Mail_Mime包。它簡單而強大。

PEAR: Mail_Mime

//Basic mail headers 
$headers['To'] = "[email protected]"; 
$headers['From'] = "[email protected]"; 
$headers['Subject'] = "Test"; 

//Set up the mail module 
$mime = new Mail_mime("\r\n"); 
$mime->setTXTBody("This is a test"); 
$mime->setHTMLBody("<p>This is a test.</p>"); 
$body = $mime->get(); 
$headers = $mime->headers($headers); 

//Send the message via SMTP 
$mail_obj =& Mail::factory('smtp', array('host' => 'mail.domain.com', 'port' => 25)); 
$mail_obj->send($headers['To'], $headers, $body);