如何更改默认邮件:php mail()中的地址

时间:2010-05-25 19:57:25

标签: 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 <domain@domain.com>' . "\r\n";           
$to = $email;
$body = '
My Message  here
';
mail($to, $subject, $body, $headers);

并且它正确发送邮件但是当我在gmail中看到电子邮件中的详细信息时... 它显示

来自域名 到myemail@myemail.com 日期:星期二,2010年5月25日下午12:41 在这里讨论我的主题 邮寄mars.myhostingcompany.net

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

3 个答案:

答案 0 :(得分:4)

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

您似乎在代码中指定了正确的标题,因此我只能想到一种可能性(我无法从此计算机进行测试)。尝试删除&lt; &GT;在您的电子邮件地址周围 - 它可能试图将其作为HTML读取,因此您什么都没有。这可以在Windows机器上发生,因为PHP本身解析自定义标头而不是MTA(邮件传输代理),而PHP会处理任何&lt; &GT;作为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: domain@domain.com' . "\r\n";           
$to = $email;
$body = '
My Message  here
';
mail($to, $subject, $body, $headers);

答案 1 :(得分:4)

有两种类型的发件人(发件人),MIME表头发件人和envelope发件人。

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

使用enveloper标记的-f发件人(您通过sendmail或sendmail兼容包装器发送电子邮件时可以发送的发件人)在第mail()个参数{{1}中设置1}},使用您在命令行上传递的格式:additional_parameters

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

-femail@address.tld

答案 2 :(得分:0)

//表格页面:

<form method="POST" action="mailer.php">
    <p>Please feel free to contact me on the form below or my direct email address: jkench@jasonkench.co.uk<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 = "myemail@mydomain.co.uk"; 

//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: Web-Contact-Form@mydomain.co.uk' . "\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.";
}
?>