2015-10-04 43 views
0

我試圖使用PHP mail從一個接觸的形式發送電子郵件:不能從地址PHP的郵件更改

<?php 
    $email = $_POST["email"]; 
    $msg = $_POST["msg"]; 
    $msg = nl2br($msg); 
    $msg = stripslashes($msg); 
    $headers = 'From: [email protected]' . "\r\n" . 
       'Reply-To: [email protected]' . "\r\n" . 
       'X-Mailer: PHP/' . phpversion(); 
    $headers = "MIME-Version: 1.0\r\n"; 
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 
    $to = "[email protected]"; 
    $mseg = "<!DOCTYPE html><html><head></head><body style='font-family:sans-serif;'><div class='header' style='padding:20px;background:#dea544;border:2px inset #fff;'><h1>Tricks for the Web</h1></div><hr><div style='background:#dea;padding:1em'><p>$name contacted you. They left this message: </p><div class='msg-container' style='background:#fff;border:1px solid #000;padding:1.3em'><p class='msg' style='font-family:sans-serif;'>$msg</p></div></div></body></html>"; 
    $subj = "Contact"; 
    if(isset($_POST["copy"])){ 
     $o = $_POST["email"]; 
     $ss = "<!DOCTYPE html><html><head></head><body style='font-family:sans-serif;'><div class='header' style='padding:20px;background:#dea544;border:2px inset #fff;'><h1>Tricks for the Web</h1></div><hr><div style='background:#dea;padding:1em'><p>Thanks for contacting us. Here's your copy of that message you left us. </p><div class='msg-container' style='background:#fff;border:1px solid #000;padding:1.3em'><p class='msg' style='font-family:sans-serif;'>$msg</p></div></div></body></html>"; 
     mail($to, $subj, $mseg,$headers); 
     mail($o, $subj, $ss, $headers); 
     echo "Your message was submitted successfully. Please note that your copy may take time to reach you."; 
    } else { 
     mail($to, $subj, $mseg, $headers); 
     echo "Your message was submitted successfully.";  
    } 
?> 

電子郵件被髮送,但是從地址仍然是默認的,它不是」我設定的那個。爲什麼這不起作用,我該如何解決這個問題。
Reference
Live Page

+0

你在這裏覆蓋你的$ headers變量'$ headers =「MIME -...',把它改爲'$ headers。='就像你有它下面那樣 – rjdown

+0

啊,謝謝。沒有注意到。 – TricksfortheWeb

+0

你可以使用任何50個優秀的郵件庫可用於PHP - phpmailer,switfmailer等 –

回答

2

當主持人將覆蓋From:頭,你可以使用著名的第五個參數用於郵件:

mail($to,$subject,$message,$headers,"-f [email protected]"); 

我真的建議給予PHPMailer的或在某一時刻swiftmailer一試。

+0

我只是忽略了一段時間,見上面的評論。 – TricksfortheWeb

+0

@TricksfortheWeb啊一切都很好。 – 2015-10-04 19:35:54

相關問題