开发者

how to change default mailed by : address in php mail()

开发者 https://www.devze.com 2023-01-01 13:36 出处:网络
i have the following code $subject = \"Subject Here\"; $headers= \'MIME-Version: 1.0\' . \"\\r\\n\"; $headers .= \'Content-type: text/html; charset=iso-8859-1\' . \"\\r\\n\";

i have the following code

$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);

and it send mail correctly but when i see details in the email in gmail ... it shows

from Domain Name to myemail@myemail.com date Tue, May 25, 2010 at 12:41 PM subject my subject here maile开发者_StackOverflow社区d-by mars.myhostingcompany.net

while i want to show my own address in mailed by section so that it should be mydomain.com instead of mars.myhostingcompany.net


There are two types of sender (From), the MIME header sender and the envelope sender.

You send the MIME sender with in the headers in the 4th parameter of the mail() function. You are doing this fine.

The enveloper sender (the one you can send when sending email through sendmail or a sendmail compatible wrapper) with the -f flag, is set in the 5th mail() parameter, additional_parameters, in the format you'd pass it on the command line: -femail@address.tld.

So your mail function would end up looking like this:

mail($to, $subject, $body, $headers, "-fdomain@domain.com");


I take it you're on shared hosting so the reason it shows your hosts email address is because when you configure PHP there is a setting called "sendmail_from" which is a default address to send mail through in the event that no address is provided in your code.

You appear to be specifying the proper headers in your code so I can only think of one possibility (that I can't test from this computer). Try removing the < > around your email address - it may be trying to read that as HTML and so you have nothing. This can occur on Windows machines because PHP itself parses the custom headers and not the MTA (message transfer agent) and PHP treats any < > as HTML.

I realize that it doesn't look as professional (since the email client won't show the name when it receives the email) but if you're running from a Windows machine there's very little else you can do unless you switch to an alternative mail package.

$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);


//FORM PAGE:

<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.";
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号