2012-12-31 50 views
0

如果用戶想要發送郵件到我的webmail,這個代碼是否足夠了?或者我需要做出改變?PHP郵件表單調整

<?php 
$mail = $_POST['mail']; 
$name = $_POST['name']; 
$subject = $_POST['subject']; 
$text = $_POST['text']; 

    $to = "[email protected]"; 

$message =" You received a mail from ".$name; 
$message .=" Text of the message : ".$text; 

if(mail($to, $subject,$message)){ 
echo "Your message was sent successfully."; 
} 
else{ 
echo "there's some errors to send the mail, verify your server options"; 

} 

?> 
+0

從第一眼看就夠了! – Rikesh

+0

@syneidesys:是否有任何錯誤? –

+0

如果你已經smtp設置,那麼上面的代碼應該工作其他明智的你必須設置smtp,[這](http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm)可能對你有用 –

回答

1

我建議在使用編碼(UTF8)的電子郵件中添加一個標題,對主題行進行編碼,這樣就不會出現亂碼(如果您使用其他非拉丁字符)並處理基本事件,是否成功。

<?php 
$name = $_POST['name']; 
$text = $_POST['text']; 
$from = $_POST['mail']; 
$to = "[email protected]"; 
$subject = "=?utf-8?B?".base64_encode($_POST['subject'])."?="; 
$message = " You received a mail from ".$name; 
$message .= " Text of the message : ".$text; 

$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/html; charset=utf-8\r\n"; 
$headers .= "To: <$to>\r\n"; 
$headers .= "From: $name <$from>\r\n";  

if (mail($to,$subject,$message,$headers)) { 
    // Do something if the email is sent 
} else { 
    // Do something if there's an error 
} 
?> 
0

這是簡單的郵件沒關係。但是mail()函數不適用於循環中的大量電子郵件。該函數打開並關閉每封電子郵件的SMTP套接字,但效率不高。要發送大量電子郵件,請參閱PEAR::MailPEAR::Mail_Queue包。

+0

確定我會在晚些時候學習它們! – syneidesys

-1

如果您想通過在標題中設置您的類型的內容類型來發送電子郵件,並且郵件的收件人必須知道郵件的發件人,否則此代碼是不夠的。代碼如下:

$to = "[email protected]"; 

$message =" You received a mail from ".$name; 
$message .=" Text of the message : ".$text; 
$headers = "Content-Type: text/html; charset=iso-8859-1\r\n"; 
$headers = "From: ". Please enter the name of sender . "\r\n"; 

if(mail($to, $subject,$message,$headers)){ 
echo "Your message was sent successfully."; 
} 
else{ 
echo "there's some errors to send the mail, verify your server options"; 

} 
1

此代碼一定適用於您。

<?php 
    $to = '[email protected]'; 
    $subject = "Your Subject"; 
    $message ="<html><body> 
    <div>Here Write Your Message</div> 
    </body></html>"; 

    $header=''; 
    $header .= 'MIME-Version: 1.0' . "\r\n"; 
    $header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
    $header .= 'From: [email protected]'. "\r\n"; 

    mail($to,$subject,$message,$header); 
?> 

注:郵件功能只在直播服務器在本地服務器工作不。

+0

很好的一個完美的作品 – Jalpesh