2012-07-25 145 views
1

有人可以告訴我如何解決此腳本,以便表單信息發送到正確的電子郵件(即,我的電子郵件....讓我們假設我的電子郵件是[email protected]) 。我希望電子郵件能夠顯示發送給我的用戶的電子郵件地址。從腳本發送電子郵件的PHP腳本

<?php 
$name = $_POST['fldName']; 
$email = $_POST['fldEmail']; 
$phone = $_POST['fldPhone']; 
$comments = $_POST['fldComments']; 
$isFormValid = false; 

if (strlen($name) && strlen($email) && strlen($phone) && strlen($comments)) $isFormValid = true; 

if ($isFormValid) 

{ 


$to  = '[email protected]'; 
$subject = 'Contact Us Form Comments'; 

$message = 'Name: '.$name."\r\n".'Email: '.$email."\r\n".'Comments: '.$comments; 

$headers = 'From: [email protected]' . "\r\n" 
     . 'Reply-To: [email protected]' . "\r\n"; 

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

header("location: thankyou.html"); 

} 
else 
{ 

echo "Please fill in the required fields"; 

} 

?> 
+0

是否有錯誤? – 2012-07-25 23:03:10

回答

1

而不是使用原生PHP郵件()來自己完成所有的事情,最好使用一些像swift郵件程序這樣的有用庫,這使得您可以更輕鬆地發送郵件而不必擔心內部工作。

例如發送郵件在迅捷郵件中,所有你需要做的是。

$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25) 
    ->setUsername('your username') 
    ->setPassword('your password'); 
$mailer = Swift_Mailer::newInstance($transport); 
// Create a message 
$message = Swift_Message::newInstance('Wonderful Subject') 
    ->setFrom(array('[email protected]' => 'John Doe')) 
    ->setTo(array('[email protected]', '[email protected]' => 'A name')) 
    ->setBody('Here is the message itself'); 
// Send the message 
$result = $mailer->send($message); 

,你可以在那裏官方網站http://swiftmailer.org/

0

更改了$headers變量使用發件人的電子郵件地址,而不是你自己的。

+0

我不知道用戶的電子郵件地址,因爲他們填寫表單時的實際時間。它不應該是一個有價值的東西嗎? – JakeSays 2012-07-26 00:08:30

+0

'$ email',有沒有機會? – 2012-07-26 00:17:19

+0

也許??????但我把它放在哪裏?在我發佈在我的問題中的代碼中,我如何確定我收到的電子郵件中的人員的電子郵件地址是正確的(非MINE),所以當我回覆電子郵件時,電子郵件的地址在TO字段中,而不是我的 – JakeSays 2012-07-27 16:59:31

1

$headers = 'From: [email protected]' . "\r\n"將顯示誰送你的電子郵件找到快捷郵件的詳細信息。它已經在你的腳本中。您不需要額外添加任何東西

+0

那個電子郵件目前是我們的。我需要它是填寫表格的人。那麼我如何從表單中添加他們的電子郵件地址? – JakeSays 2012-07-26 00:09:36