php
2010-05-25 80 views 5 likes 
5

,我有以下代码如何更改默认通过邮寄:地址在PHP邮件()

$subject = "Subject Here"; 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
// Additional headers  
$headers .= 'From: Domain Name <[email protected]>' . "\r\n";   
$to = $email; 
$body = ' 
My Message here 
'; 
mail($to, $subject, $body, $headers); 

,并正确地发送邮件,但是当我在Gmail电子邮件查看详细信息... 它显示

从域名 到[email protected] 日星期二,2010年5月25日下午12时41 主题我的主题在这里 寄件人mars.myhostingcompany.net

,而我想显示我自己的地址邮寄的部分,以便它应该是mydomain.com而不是mars.myhostingcompany.net

回答

4

我把它你在共享主机,所以它显示你的主机的电子邮件地址的原因是因为当你配置PHP时,有一个名为“sendmail_from”的设置,它是在你的代码中没有提供地址的情况下发送邮件的默认地址。

你出现在你的代码中指定适当的标题,所以我只能想到一种可能性(即我不能从该计算机测试)。尝试在您的电子邮件地址附近删除<>它可能试图将其作为HTML阅读,因此您什么都没有。这可能发生在Windows机器上,因为PHP本身解析自定义标头而不是MTA(邮件传输代理),PHP会将任何<>视为HTML。

我知道这看起来并不专业,但如果你从Windows机器运行有很少否则你可以做,除非你(因为当它接收电子邮件的电子邮件客户端将不显示名称)切换到另一个邮件包。

$subject = "Subject Here"; 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
// Additional headers  
$headers .= 'From: [email protected]' . "\r\n";   
$to = $email; 
$body = ' 
My Message here 
'; 
mail($to, $subject, $body, $headers); 
+0

这不应该被标记为正确答案。这完全是不真实的。我通过谷歌发现了这个,所以我觉得我需要添加这个评论 - 我的意思是没有任何攻击你@Jarrod。我已经添加了一个正确的答案。 – LeonardChallis 2014-06-13 20:31:54

+0

@LeonardChallis然后你应该编辑这个答案。迟到的答案几乎从未被接受。 :/ – 2014-06-17 00:31:34

0

// FORM PAGE:

<form method="POST" action="mailer.php"> 
    <p>Please feel free to contact me on the form below or my direct email address: [email protected]<br> 
     <br><br> 
     <br> 
     <br> 
     <br> 
    </p> 
    <table width="327" border="0"> 
     <tr> 
     <td width="102">Name:</td> 
     <td width="215"><input type="text" name="name" size="19"></td> 
     </tr> 
     <tr> 
     <td>Company: 
     <label for="company"></label></td> 
     <td><input type="text" name="company"></td> 
     </tr> 
     <tr> 
     <td>Email: </td> 
     <td><input type="text" name="email" size="19"></td> 
     </tr> 
     <tr> 
     <td>Telephone No: 
     <label for="telephone"></label></td> 
     <td><input type="text" name="telephone"></td> 
     </tr> 
    </table> 
    <p><br> 
     Enquiry:<br> 
     <textarea rows="9" name="message" cols="65"></textarea> 
     <br> 
     <br> 
     <input type="submit" value="Submit" name="submit"> 
    </p> 
</form> 

// PHP MAILER PAGE

<?php 
if(isset($_POST['submit'])) { 

//SEND TO 
// Send the completed form to the below email address: 
    $to = "[email protected]"; 

//SUBJECT 
// Subject of the email form: 
    $subject = "Jason Kench - Web Developer"; 

//NAME 
//POST the details entered into the name box 
    $name = $_POST['name']; 
//COMPANY NAME 
// 
    $company = $_POST['company']; 
//EMAIL 
// 

    $email = $_POST['email']; 
//TELEPHONE NUMBER 
// 
    $telephone = $_POST['telephone']; 
//MESSAGE/ENQUIRY 
    $message = $_POST['message']; 

//Headers from a online site may help not sure 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
//FROM EMAIL ADDRESS: 

// Additional headers to change the FROM EMAIL ADDRESS 
$headers .= 'From: [email protected]' . "\r\n";   



// BODY 
// This is the body of the message that will be sent to my email address with their details. 
    $body = " 
    You have received a message from the online contact form at http://www.jasonkench.co.uk\n 
    Details Below: \n \n 
    From: $name\n 
    Company: $company\n 
    $headers 
    Email Address: $email\n 
    Telephone No: $telephone\n 
    Message: $message\n"; 
// FORM SENT 
// This will alert the customer the form has been successfully sent. 
    echo "Your details have been sent, I will contact you within 48 hours."; 
// Use the mail function to email the following variables to my $to email address. 
    mail($to, $subject, $body, $headers); 

} else { 
    // Display error message if there is a problem. 
    echo "Sorry there seems to be a problem. Please email me direct at: $to thank you."; 
} 
?> 
4

有两种类型的发送者(从)中,MIME头发送者和发送者envelope

您可以在mail()函数的第4个参数的标头中发送MIME发件人。你做得很好。

enveloper发送者(一个你可以通过发送sendmail或一个兼容sendmail的包装电子邮件时发送)与-f标志,被设置在第5 mail()参数,additional_parameters,格式为你传递给它的命令行:[email protected]

所以你的邮件功能最终会看起来像这样:

mail($to, $subject, $body, $headers, "[email protected]"); 
相关问题