6

I'm try to sent email by my localhost php, but the problem is i didn't receive anything in my email, what should i configure?

Here is my code

$to="someone@gmail.com";
$name="jason";
$subject="test message";
$header="From: $name";
$message="blah blah blah";
$sentmail=mail($to,$subject,$message,$header);

echo $sentmail ? "email send" : "email send fail"?

as the result was "email send"

Jason Kuah
  • 57
  • 1
  • 2
  • 3
  • 1
    you should configure smtp on your computer. by default php mail function uses sendmail. And check /var/log/mail* - maybe you will find something there – Nemoden Jul 25 '13 at 04:39
  • Did you check your SPAM box? – vinu Jul 25 '13 at 04:42
  • Some times it could be server related issue, such as your IP may be blacklisted. Try using a different email address and see if tha works. – Zubair1 Jul 25 '13 at 04:44
  • Nemoden - i read something about that, i already set SMTP = localhost in php.ini, and smtp_server=localhost in sendmail.ini, am i right? – Jason Kuah Jul 25 '13 at 04:54
  • vinu - what is that, detail it, thanks a lot – Jason Kuah Jul 25 '13 at 04:55
  • @JasonKuah Sorry i misread your question. If you are trying to do it in `localhost` you must configure a mail server. – vinu Jul 25 '13 at 05:14

6 Answers6

4

There are 2 reason not to send email from your localhost..

  1. You don't have mail server setup in your local environment
  2. You are not using SMTP service to send the email.

So either you have to configure the mail server but I don't think that this is a handy solution.

Better you try to use SMTP service. To do this it will be better if your use PHPMailer.

Here is an example using PHPMailer class.

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';

$mail->Host       = "mail.example.com"; // SMTP server example
$mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Port       = 25;                    // set the SMTP port for the GMAIL server
$mail->Username   = "username"; // SMTP account username example
$mail->Password   = "password"; 

You can use this class for any kind of email as a alternative of PHP : mail().

Blackcoat77
  • 1,574
  • 1
  • 21
  • 31
S. Rasel
  • 2,841
  • 1
  • 16
  • 12
2

mail function will Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

mail function will not check whether mail reached in your inbox

http://php.net/manual/en/function.mail.php

You can't check whether mail has been delivered, but you can check whether the recipients opened your mail with tracking pixel https://support.google.com/dfp_premium/answer/1347585?hl=en

Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
0

You should integrate a mail server in your local machine to send mail.The php mail function return true if all parameters are correct, it will not check delivery status.

Read this, http://www.zenddeveloper.com/how-to-send-emails-from-localhost-apachephp-server/ and http://roshanbh.com.np/2007/12/sending-e-mail-from-localhost-in-php-in-windows-environment.html

Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132
0

You need to have a mail server to send emails in localhost.Check PHP : send mail in localhost

Community
  • 1
  • 1
vinu
  • 658
  • 10
  • 20
0

You need to have the content-type on your header. To make it easier, you could write simple function and then call it. Here's an example:

 function sendMail($to, $title, $url, $from, $username, $password) {
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: '.$title.' <'.$from.'>' . "\r\n";
    $subject = 'Welcome to '.$title;
    $message = 'Thank you for joining <strong>'.$title.'</strong><br /><br />Your username: <strong>'.$username.'</strong><br />Your Password: <strong>'.$password.'</strong><br /><br />You can log-in at: <a href="'.$url.'" target="_blank">'.$title.'</a>';
    return @mail($to, $subject, $message, $headers);
}
pcs
  • 1,864
  • 4
  • 25
  • 49
Adrian.S
  • 209
  • 2
  • 12
0

By default, PHP's mail() function will deliver mail to the sendmail program on Linux.

For sendmail to work, you are required to have a properly configured and functioning MTA. For example, postfix is an MTA that is relatively easy to configure.

When configuring your MTA, you can either configure it to send mail directly, as a mail server on the internet, or to relay your mail to another server.

Configuring your own MTA to deliver mail directly is not for the light-hearted. Sending email has become complicated now, requiring a lot of work to be able to have your mail accepted by major mail servers like gmail or yahoo.

If your ISP provides an outgoing mail server and is happy to relay mail for you, you can set up postfix to relay all mail via that server instead, and save yourself some configuration hassle. If you use postfix, this simply requires setting it up like the example under Postfix on a null client in the postfix configuration.

The main thing to remember, no matter how you are configuring your mail server, is to avoid setting it up to relay incoming mail on the outgoing network (ie, setting it up as an open relay). In the null mailer example configuration, the line inet_interfaces = loopback-only achieves this.

Note that an alternative to setting up postfix or something as an MTA is to uses PHP's own built in SMTP support, which essentially means you are using PHP itself as an MTA which only forwards mail to a relay.

The advantage to using a dedicated MTA like postfix is reliability. Postfix can queue email if there is a temporary problem reaching the external mail relay. It also returns as soon as the mail has been queued, so your PHP mail function will execute much faster and won't need to wait while the mail is delivered to the external mail relay.

thomasrutter
  • 114,488
  • 30
  • 148
  • 167
  • Is there a `sendmail` program in windows and mac osx? I'm using windows. – Mai Mar 28 '15 at 12:20
  • No, at least not in Windows. Setting up PHP's ability to send mail in Windows is a actually quite a different procedure, try using search to find more info. – thomasrutter Mar 29 '15 at 04:27