Hi,
I'm trying to set a simple send an email script as I found it on the internet.
Here is the code:

<html>
<head>
<title>PHP Mail Array Loop</title>
</head>
<body>
<?php
$to = "mymail@anydomain.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "anybody@somedomain.com";
$headers = "From: $from";

$headers.= "Content-Type: text/html; charset=ISO-8859-1 ";
$headers .= "MIME-Version: 1.0 ";

if (mail($to,$subject,$message,$headers))
{
echo "Mail Sent.";
}

?>
</body>
</html>

Email addresses are substituted by real addresses.
Now, this is not working. I see the "Mail Sent." message but receive no mail.

My php skills really suck and I would appreciate if you could help in this one.

Dani AI

Generated

— a few practical points that the thread hasn't shown in full detail. The PHP mail() call only hands the message to the server MTA; seeing "Mail Sent." only means PHP got a success response from that hand-off, not that the mail reached the recipient. The two most common causes when you don't receive anything are: (1) malformed headers (missing CRLFs, wrong order) and (2) the server's mail agent isn't configured or is rejecting/queuing outbound mail. was right to mention the return value; use that knowledge to dig into server logs next.

Use properly formed headers and set the envelope sender (fifth parameter) so the MTA has a valid return address. Example:

$to = 'you@yourdomain.com';
$subject = 'Test mail';
$message = '<html><body><p>Hello — this is a test message.</p></body></html>';

$headers  = "From: Sender Name <webmaster@yourdomain.com>\r\n";
$headers .= "Reply-To: webmaster@yourdomain.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

mail($to, $subject, $message, $headers, "-fwebmaster@yourdomain.com");

Quick troubleshooting checklist:

  • Verify a mail agent is running on the server (sendmail/postfix/exim) and check its logs (/var/log/mail.log or /var/log/maillog) and queue (mailq / postqueue -p).
  • If on Windows, confirm php.ini SMTP settings are correct and sendmail_from is set.
  • Confirm the From/envelope address belongs to the sending domain (many MTAs reject spoofed domains or require authenticated SMTP).
  • If using shared hosting, ask the host whether mail() is allowed or if they require authenticated SMTP.

For reliable delivery and clearer error messages, switch to an SMTP library (PHPMailer/SwiftMailer) and send via authenticated TLS SMTP — it makes debugging and deliverability much easier. Also thanks to and for the pointers; they point you toward the right areas to check while the corrected headers and server checks above will usually reveal the root cause.

Recommended Answers

All 3 Replies

Try this:

mail($to, $subject, $body);

in place of what you have.

Also change $to = "mymail@anydomain.com"; to your real email. In addition, another possible error which I am unsure if exists is that the if element may need to be as follows:

if (mail($to,$subject,$message,$headers))
{
mail($to,$subject,$message,$headers)
echo "Mail Sent.";
}

But I am unsure about if element and have never known if the mail function will work in the if element.

, mail function works fine in if condition. The mail function returns true on success and false on failure. It works just like, if(empty($var)) { @OP, check your spam folder. On many occasions, mails sent using php function lands up in spam folder!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.