PHP mail () function does not use "from" address

$to = "me@mydomain.com";
$subject = "Auction Registration Confirmation";
$from = "From: donot-reply@mydomain.com";
$body = "Test Message";
if (mail($to, $subject, $body, $from)) {
    echo("<b>Message sent</b>");
    header( "Location: http://www.mydomain.com/thankyou.html" );
}  else {
    echo("<b>Message failed</b>");
}

      

Now the problem is that when the email is sent, the address is not what I require, but the server login.

Any ideas regarding replacing the server login with an email id.

0


a source to share


4 answers


There should be nothing wrong with your PHP code - I have very similar code doing the same job and it works great.



Perhaps there is something misconfigured with your email setup?

0


a source


The PHP mail function is based on the MTA running on your server. I would bet your MTA is set up to force the $ variable from variable to your login.



0


a source


Have you tried this? it works well for me

<?php
$to = "email@to.com";
$subject = "subject";
$message = "message";
$from = "email@from.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?> 

      

0


a source


You can try

mail($to, $subject, $body, $from, "-f $from");

      

this works in some configurations, really depending on the server setup. If not, edit your MTA settings as recommended above, or skip mail () altogether and use a PHPmailer-like class that connects directly to the SMTP server.

0


a source







All Articles