2016-10-04 73 views
1

在這個郵件形式的一切工作,除了沒有發件人和我的郵件直接去垃圾!是否因爲這個郵件表單上沒有發件人?我想從發件人那裏得到信息,所以想知道我在哪裏填寫發件人的代碼?PHP郵件與發件人

<?php 
/* Set e-mail recipient */ 
$myemail = "[email protected]"; 

/* Check all form inputs using check_input function */ 
$name = check_input($_POST['inputName'], "Your Name"); 
$email = check_input($_POST['inputEmail'], "Your E-mail Address"); 
$subject = check_input($_POST['inputSubject'], "Message Subject"); 
$message = check_input($_POST['inputMessage'], "Your Message"); 

/* If e-mail is not valid show error message */ 
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) 
{ 
show_error("Invalid e-mail address"); 
} 
/* Let's prepare the message for the e-mail */ 


$message = " 

Someone has sent you a message from xxxxxxx.com: 

Name: $name 
Email: $email 
Subject: $subject 

Message: 
$message 

"; 

/* Send the message using mail() function */ 
mail($myemail, $subject, $message); 

/* Redirect visitor to the thank you page */ 
header('Location: http://www.xxxxxxx.com/confirmation.html'); 
exit(); 

/* Functions we used */ 
function check_input($data, $problem='') 
{ 
$data = trim($data); 
$data = stripslashes($data); 
$data = htmlspecialchars($data); 
if ($problem && strlen($data) == 0) 
{ 
show_error($problem); 
} 
return $data; 
} 

function show_error($myError) 
{ 
?> 
<html> 

    <body> 

    <p>Please correct the following error:</p> 
    <strong><?php echo $myError; ?></strong> 
    <p>Hit the back button and try again</p> 

    </body> 
</html> 
<?php 
exit(); 
} 
?> 
+0

從手冊(郵件):發送郵件時,郵件必須包含From頭。這可以使用additional_headers參數來設置,或者可以在php.ini中設置默認值。 – Progrock

+0

話雖如此,我剛剛檢查了我的LAMP設置,並且沒有在我的配置中定義的'sendmail_from'值。我的郵件程序(exim4)正在爲運行腳本的用戶添加一個From字段,前提是我不添加標題。你有沒有From標題? – Progrock

回答

2

您可以使用「附加頭」的說法,以郵件發送者的電子郵件地址添加:

mail($myemail, $subject, $message, "From: [email protected]"); 

編輯:你的情況,我認爲你需要在$電子郵件傳遞在代碼中定義的變量。這將顯示電子郵件來自輸入表單中的電子郵件地址。

mail($myemail, $subject, $message, "From: " . $email); 

參見:http://php.net/manual/en/function.mail.php

+0

感謝您的時間!但是當我收到一封郵件時,希望看到標題是誰來的。在這個例子中,它表示[email protected]或我填寫的電子郵件。 – Riesbeck

+0

非常感謝Will! – Riesbeck