2013-01-18 35 views
-3

領域我發送郵件下面的代碼:如何包括電子郵件

$email_to = '[email protected]'; 
$name = $_POST['name']; 
$email = $_POST['email']; 
$phone = $_POST['phone']; 
$subject = $_POST['subject']; 
$message = $_POST['message']; 

$headers = 'From: ' . $name . ' <' . $email_to . '>' . "\r\n" . 'Reply-To: ' . $email; 
if(mail($email_to, $subject, $message, $headers)) { 
    echo 'sent'; // sending this text to the ajax request telling it that the mail is sent..  
} else { 
    echo 'failed'; // ... or this one to tell it that it wasn't sent  
} 

郵件發送正常,但我需要與消息一起顯示的電話號碼。我相信這是一個簡單的解決方法,我會全部「doh!」當有人教我時,讓我們來解決吧! :)

+2

此代碼可通過SMTP注入攻擊:https://www.hellboundhackers.org/articles/867-smtp-injection.html –

+0

不理解爲什麼-3票。這是一個101個問題,但有效的是無效的。 – 86Stang

回答

2

你需要的電話號碼追加到消息的字符串。

$email_to = '[email protected]'; 
$name = $_POST['name']; 
$email = $_POST['email']; 
$phone = $_POST['phone']; 
$subject = $_POST['subject']; 
$message = $_POST['message'] . "\n\n" . $phone; 

$headers = 'From: ' . $name . ' <' . $email_to . '>' . "\r\n" . 'Reply-To: ' . $email; 
if(mail($email_to, $subject, $message, $headers)) { 
    echo 'sent'; // sending this text to the ajax request telling it that the mail is sent..  
} else { 
    echo 'failed'; // ... or this one to tell it that it wasn't sent  
} 
+0

請參閱! SOOOO顯而易見!在尋求幫助之前,任何人都會'想一些東西一分鐘'。書鋪設?謝謝,Dean。 – 86Stang

+0

請務必「接受」此答案並查看有關SMTP注入的其他評論。 – Dean

0

將電話號碼置於$message變量中。像

$message = $_POST['message']."<br/><br/>Your phone number is: ".$phone; 

東西還要添加適當的報頭,支持HTML:

$headers .= "Content-Type: text/html\r\n"; 
+0

這不起作用,因爲標題未設置爲HTML電子郵件。 – Dean

+0

謝謝,補充了一個修復。 – Charles

0

您需要將電話號碼與郵件連接以便發送郵件。

$message = $_POST['message']."\n\n$phone"; 

你也應該查找功能,以防止電子郵件注射,並且還運行在每一個崗位價值用strip_tags防止表單上XSS攻擊。

+0

我很好奇,爲什麼-1 ... – Eric

+0

我沒有讓你失望,但讓你知道它應該是'$ message = $ _POST ['message']。「\ n \ n」。 – Charles

+0

您可以將變量放在雙引號中,並且由於您必須使用雙引號才能使用\ n,爲什麼不呢? – Eric

相關問題