James Patrick
James Patrick

Reputation: 263

not sending email in php with header from email id

I am using the following code along with HTML for header .

$email="[email protected]"; in the example and I want to implement variable in its place. Code as posted below but its not showing error nor sending email.

I have tried the following links PHP email form not sending information , PHP Sending Emails with File Attachments - Email Not Sending At All .

I have tried

$headers .= "From: <".$email.">\n";

and

$headers .= $email;

This displays $email in the label from header in email.

But its working fine till this line:

$headers .= 'From: ' .$email. "\r\n";

This above line is not sending email if I remove this line it works but it does not add From email id to the header.

Please help me out it does not show any error and I have tried many variations to the above code but still stumped.

<?php

$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['contact'];

$subject = "feedback";
$question = $_REQUEST['question'];


$body = "<html>
<head>

</html>";
$mime_boundary = "<<<--==+X[".md5(time())."]\r\n\r\n";
$headers = "MIME-Version: 1.0"."\r\n" ."Content-Type:text/html;"."\r\n";
$headers .= 'From:'.$email. "\r\n";


$to ='[email protected]';
mail($to,$subject,$body,$headers);
echo "<script>alert(' message  sent.');</script>";

?>

Upvotes: 1

Views: 1321

Answers (2)

mccbala
mccbala

Reputation: 183

I had the same issue with one of the servers I were dealing with. Apparently in my server, not specifying "From" header sends email from the default mail address of your account (in case of a shared hosting). I chose this solution as an ad-hoc fix and specified my actual from address in "Reply-To" header. This way, I can still receive the replies sent to those email threads.

This method seems feasible for just functional email addresses (eg., [email protected])only. If you are using this approach for a user's email address (eg., [email protected]), chances are that the recipient might think of your email as a spam.

Upvotes: 1

maximkou
maximkou

Reputation: 5332

if you use standard mail function, then try this(do not add "From" header to $headers):

mail($to,$subject,$message,$headers,"-f ".$email);

Upvotes: 0

Related Questions