-2

I have a simple form on my website. People leave a name and e-mailadres. They get send a link to a download, and I get send their data.

All of this works on my test server, but not live. My provider is picky about sending mail and wants a proper From adres stated.

Question: how do I go about this. I have a from adres is the header:

 $from = 'name@example.com';
 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .= 'From: In2Motivation <{$from}>' . "\r\n";
 mail($to, $subject, $message, $headers,);

I have also tried: mail($to, $subject, $message, $headers, '-f name@example.com');

But it still won't send anything. Where does this go wrong? Is it my script or is it something on my providers end?

Everyone can help me? Thanks a lot!!!

Andrea php
  • 393
  • 4
  • 21
  • Might be a silly question. Do you know if a mail server is set up on your live machine? – castis May 03 '16 at 16:34
  • Your host may require authorization prior to send; or may require you to logon and then send. Many possibilities here; have you checked the log files? Check your php.ini and sendmail.ini settings if you can get access (mosts hosts will allow you to at least view these). –  May 03 '16 at 16:36
  • It's 2016 and people still tries to make `mail()` work... – Marcin Orlowski May 03 '16 at 16:38
  • I would highly recommend using a mailing library for this, as the built-in mail functionality in PHP can sometimes (seemingly inexplicably) just not work. I've had instances where I haven't changed the code at all, and it works one week but not the next, then it will work a few times again, but then it won't. I highly recommend [PHPMailer](https://github.com/PHPMailer/PHPMailer). – shamsup May 03 '16 at 16:46

1 Answers1

1

Variable $from isn't being expanded inside single quotes, use double quotes instead:

<?php
//debug start - comment on production
error_reporting(E_ALL);
ini_set('display_errors', '1');
//debug end
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = "From: In2Motivation <{$from}> \r\n" .
           'MIME-Version: 1.0' . "\r\n" .
           'Content-type: text/html; charset=iso-8859-1' . "\r\n".
           'Reply-To: webmaster@example.com' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();

if(mail($to,$subject,$message,$headers)){
   echo "email sent"; 
}else{ 
   echo "email failed";
}
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • See [the docs](http://php.net/manual/en/language.types.string.php#language.types.string.parsing) for more info on string interpolation. – shamsup May 03 '16 at 16:49