PHP mail on Linux server

I am having an issue with the PHP mail.

It’s working (finally) but only after searching through dozens of sites for clues.
This line does NOT work:

mail($email_to,$email_subject,$email_message,$headers);

But this line does:

@mail($email_to,$email_subject,$email_message,$headers);

If I don’t use the “@” the app doesn’t return an error - and doesn’t send any message or mail.

Is the “@” necessary because I’m on a Linux server?
And why is there no mention of this peculiarity anywhere in the manual?

@ tells the PHP interpreter to ignore an error / notice. Best practice to not use this :slight_smile:

Hi,

As far as I know it’s the same function but with error suppression

PHP: Error Control Operators - Manual

Edit: Oops, Kyle was faster.

Like I said: If I don’t use the “@” the app doesn’t return an error - and doesn’t send any message or mail.

So, if “@” is error suppression, and I don’t use error suppression, why does the mail not send AND I don’t get an error message?

What is the error you are seeing?

Here is the primary code.
And all the variables are properly established.
$email_to : one legitimate address
$email_subject : basic text
$email_message : also basic text, with minimal html tags.

$headers = "MIME-Version: 1.0" . "\\r\
";
$headers .= "Content-type:text/html" . "\\r\
";
$headers .= 'bcc: '.$bcc_to."\\r\
" .
$headers .= 'From: ' . $email_from . "\\r\
";

@mail($email_to,$email_subject,$email_message,$headers);

I’d love to know where this is going wrong. The format matches the examples I’ve seen in the manual.

We need to know the error it is spitting out

@imillard ;

The code looks fine and all, and I agree with everyone else that the @ shouldn’t make a difference one way or another. To that end, without knowing what the exact error message you were getting, it is hard to diagnose the issue at hand.

Other items that would be nice to see is your [mail function] setup in your php.ini.

If you could provide both of those, that would be helpful (you can skip any smtp username and password, we don’t need to know that).

Maybe the mail situation is the least of my problems, because I seem to have difficulties in communicating.

Repeat: The mail does not send, and I don’t get an error message!

The mail simply disappears without a trace.
I even put in the following check:


 if (mail($email_to,$email_subject,$email_message,$headers)) {
   echo("<p>Message successfully sent!</p>");
  } else {
   echo("<p>Message delivery failed...</p>");
  }

It claims the mail is sent successfully, but it never arrives.
If I use the following the mail arrives in seconds.


 if (@mail($email_to,$email_subject,$email_message,$headers)) {
   echo("<p>Message successfully sent!</p>");
  } else {
   echo("<p>Message delivery failed...</p>");
  }

What would cause the mail to disappear (not be sent) but not generate an error message?
And I have tried different recipient addresses, different subject lines, and plain text message body without success.

If you think it will help - here is the entire program. At least you can have a good laugh over it.

It’s a very simple contact form, with minimal verification security. In it’s present form it works.

The only secure addition is the block that strips out html tag markers.


<?php
if(isset($_POST['email'])) {

$email_to = "help@debtmasters.ca";
$bcc_to = "debtmaster@debtmasters.ca";
$email_subject = "The On-Line Help contact form";
 
function died($error) {
echo "We are very sorry, but there were error(s) found in the form you submitted. ";
echo $error."<br />";
echo "Please go back and fix these errors.<br /><br />";
die(); }
 
if(!isset($_POST['firstname']) ||
!isset($_POST['lastname']) ||
!isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.'); }

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email_from = $_POST['email'];
$phone = $_POST['phone'];

$income = $_POST['income'];
$persons = $_POST['persons'];
$debts = $_POST['debts'];

$bankloan = $_POST['bankloan'];
$creditcard = $_POST['creditcard'];
$mortgage = $_POST['mortgage'];
$studentloan = $_POST['studentloan'];
$personalloan = $_POST['personalloan'];
$otherloans = $_POST['otherloans'];
$taxdebt = $_POST['taxdebt'];
$support = $_POST['support'];
$assets = $_POST['assets'];
$gics = $_POST['gics'];
$stocks = $_POST['stocks'];
$auto = $_POST['auto'];
$rrsps = $_POST['rrsps'];
$insurance = $_POST['insurance'];
$property = $_POST['property'];
$otherassets = $_POST['otherassets'];

$comments = $_POST['comments1'];

$verif_box = $_POST['verif_box'];

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= '<br />The Email Address you entered does not appear to be valid.'; }

$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$firstname)) {
$error_message .= '<br />The First Name you entered does not appear to be valid.'; }

if(!preg_match($string_exp,$lastname)) {
$error_message .= '<br />The Last Name you entered does not appear to be valid.'; }

if($verif_box <> $_COOKIE['debtmasters']) {
$error_message .= '<br />The Verification Code you entered was not correct.<br />'; }

if(strlen($error_message) > 0) {
died($error_message); }

$email_message = "<html><body>";
$email_message .= "<b>DebtMasters On-Line Help contact form:</b><p>";
$email_message .= "Name: ".$firstname." ".$lastname."<br>";
$email_message .= "Email: ".$email_from."<br>";
$email_message .= "Phone: ".$phone."<p>";

$email_message .= "Income: ".$income."<br>";
$email_message .= "Total Persons: ".$persons."<br>";
$email_message .= "Total Debt: ".$debts."<p>";

$email_message .= "Debts: ". $bankloan." ".$creditcard." ".$mortgage." ".$studentloan." ".$personalloan." ".$otherloans." ".$taxdebt." ".$support."<p>";

$email_message .= "Assets: ". $assets." ".$gics." ".$stocks." ".$auto." ".$rrsps." ".$insurance." ".$property." ".$otherassets."<p>";

$email_message .= "Comments: <br>";

$comm2 = str_replace("<","[",$comments);
$comm3 = str_replace(">","]",$comm2);
$comm4 = stripslashes($comm3);
$hlr = chr(13);
$comments = str_replace($hlr,"<br>",$comm4);

$email_message .= $comments."<p>";
$email_message .= " </html>";

$headers = "MIME-Version: 1.0" . "\\r\
";
$headers .= "Content-type:text/html" . "\\r\
";
$headers .= 'bcc: '.$bcc_to."\\r\
" .
$headers .= 'From: ' . $email_from . "\\r\
";

@mail($email_to,$email_subject,$email_message,$headers);

include "1Fonlinepost.php" ;

setcookie('debtmasters','');

}
?>

It sends - but the requirement for the “@” still bugs me - along with the 160+ trials it took to get it working.

Ah, chances are you do not have send_mail setup properly. And it is sitting in a queue on your linux machine. I’ve used mutt in the past to look at the queue, but there is another command … oh yeah, mail

  1. Did you test your sendmail configuration from the command line to make sure you could send e-mails before trying to use PHP?
  2. Did you restart apache after updating the php.ini to use sendmail?

It’s a nice idea, but it’s not “my server”.

This page, along with 43 others that I work on is hosted at GoDaddy, and their technical support is unable to identify any issues with the hosting setup, the PHP installation, or the page code.
All the tests I can perform seem to indicate there is no problem, and the queue is empty.

I guess I’m stuck with it.

I’ve tried your code on my linux server and it works perfectly with or without the @symbol - strange thing is, if I run the identical code on my windows/iis server, I see almost the same results as you do on linux.

with the error suppressed, the page loads cleanly, but no mail is received. If I remove the suppression I get the following error:
Warning: mail() [function.mail]: SMTP server response: 530 SMTP authentication is required
I get this error with genuine and fake from addresses.

Could it be that you too have authentication issues but the error isn’t showing for some reason?

Just thought of something, although it doesn’t explain why @ works and without @ doesn’t. One of the hosts I just recently worked with would only allow mail() to work if you provided an additional parameter of ‘-f<from_email_address_here>’.

That would result in your final code being:

mail($email_to,$email_subject,$email_message,$headers,'-f' . $email_from);

You may want to try that with the @ and without it.

2ndmouse: That is interesting. I know the GoDaddy mail servers require SMTP authentication, but I always associated that with using external POP programs like Thunderbird.

It’s entirely possible I have authentication issues but the error isn’t showing. Armed with this idea I’ll contact their tech support again.

I will also try providing the additional parameter of “From.” Although, I would be surprised if this made a difference, as that parameter is already supplied in the header. And the From address I supply in the header does change the reported source in the received message.

Believe it or not, I had to provide both, as the headers are not considered by sendmail in verifying outbound e-mails, but the additional parameter is specific to sendmail and tells it, it is okay (some hosts are setup this way, others are not, just wanted to relay my experience).

If you get this error is because your mail server needs authetication. This is a configuration issue or you’ll need to add the sender’s mail username and password to verify that he’s sending the e-mail to the script