1

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

$email="example@example.com"; 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 ='example@example.com';
mail($to,$subject,$body,$headers);
echo "<script>alert(' message  sent.');</script>";

?>
Community
  • 1
  • 1
James Patrick
  • 273
  • 4
  • 19
  • 2
    Could you add the rest of your code, please? – andrewsi Apr 24 '13 at 14:42
  • added code please dont downvote this question. – James Patrick Apr 24 '13 at 15:15
  • 1
    So everything is working, aside from the part where you add the 'From' line to the headers? If that line is there, it won't send the email; but if you comment it out, it will? In which case - what are you adding as an email? And what do you mean by it's not sending the email - are there errors in the SMTP log? Or does everything look like it's working, but the email never arrives? – andrewsi Apr 24 '13 at 15:20
  • Yes everything looks like its working but email is not arriving and if I remove **$headers .= 'From:'.$email. "\r\n";** it arrives but without from email in the header label the rest works perfect.I dont know why this is not working tried many variations.Please upvote the question It would help me a great deal. – James Patrick Apr 24 '13 at 15:23
  • What do your SMTP error logs say? What is the actual value of `$email` when the form is sent? In the past I've had issues with mail being discarded when the From address was an invalid email address, so might that be causing the issue? Can you try replacing `$email` with your own email address? – andrewsi Apr 24 '13 at 15:31
  • how to get SMTP error log ? $email ="example@example.com" .I will also try with valid email also. – James Patrick Apr 24 '13 at 16:00
  • I tried valid id and its still not working – James Patrick Apr 24 '13 at 16:10
  • 1
    In your question, you have the line formatted as "From: $email" - in the code you've posted, there's no space between the colon and the email address. That might be an issue. – andrewsi Apr 24 '13 at 16:12
  • 1
    Also, I'm not sure that the `Content-type:text\html;` needs a trailing semi-colon. Basically - can you split the `$headers` into three sections, and try commenting out each part to see if it's a problem with one of the other lines? – andrewsi Apr 24 '13 at 16:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28830/discussion-between-james-patrick-and-andrewsi) – James Patrick Apr 24 '13 at 16:24
  • I have posted few queries on chat. – James Patrick Apr 24 '13 at 16:32

2 Answers2

1

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., support@example.com)only. If you are using this approach for a user's email address (eg., john@example.com), chances are that the recipient might think of your email as a spam.

mccbala
  • 183
  • 1
  • 7
0

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

mail($to,$subject,$message,$headers,"-f ".$email);
maximkou
  • 5,252
  • 1
  • 20
  • 41