Mail() not working, any ideas why? - php

Hi guys i am using the mail() from the contact form and for some reason it is not working.
The php coding i have setup is as follows:
// sending email to sales#xxx.com
$to = "hello#xxx.com";
$subject = 'Email From {$name} from your website';
$message = "$name has contacted you from the website and says:
$mcontent
$name 's contact email address is: $email";
$headers = $email . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail($to, $subject, $message, $headers) or die ("Failure");
// end of sending mail
returnheader("/xxx/email-sent");
i get no errors at all, it even goes to the success page when completed.
Any ideas why this would not work.

I have checked your code and it is working fine on my server , i am getting e-mail.
Here , it looks like some problem with your SMTP server settings.
There is nothing wrong with your PHP script.
You can find your solution here.
php.ini & SMTP= - how do you pass username & password
Also in windows environment ,
http://www.ruhanirabin.com/php-sendmail-setup-with-smtp-iis-and-windows-servers/

If you are on WIndows, make sure you have an SMTP server in your
php.ini
If you are una Unix, make sure the MTA is running: If it is (at least partly) installed but not runnng, you will get exactly this effect
Edit
If your MTA is not running, and you start it, the mails sent with PHP will go out! They have beein queued, but not processed.

This is probably related to the mail setup. Do you have a mailserver running on the machine? Check sendmail / smtp_server settings in php.ini.

mail() uses the sendmail, by default: sendmail -t -i
It returns TRUE if the mail has been accepted, not if it has been sent:
Return Values
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
I would suggest using http://swiftmailer.org/ with SMTP rather than mail().

The first line of $headers is invalid if $email is just an email address. Presumably you would want something like:
$headers = "Cc: " . $email . "\r\n";
or
$headers = "From: " . $email . "\r\n";

The cause can be for example, you don't have setup a mail server, or configuration of firewall if you has it.

You should use double quotes instead of single quotes in the variable $subject
$subject = "Email From {$name} from your website";

Related

PHP mail() not sending

I have a WebFaction server and the following code:
$to = "outreach#bmun.org";
$reply_to = "From: " . $_POST['email'];
$name = $_POST['name'];
$subject = "Outreach Request Session for " . $_POST['school'] . " on " . $_POST['date'];
$em = $_POST['message'] . "\n-" . $name;
$sentmail = mail($to, $subject, $em, $reply_to);
$sentmail returns true, but the email is not sending for some reason.
$sentmail returns true, but the email is not sending for some reason.
The mail function in PHP simply sends the mail via an MTA (mail transfer agent) on the server. A true can just mean the local MTA accepted it. But that is not all you need.
First, does your hosting provider actually allow outgoing mail? Or are messages sent to a virtual “black hole?”
Now, let’s assume that your local MTA—most likely sendmail—works, and the mail jumped off of the server & made it into the real world. Okay, great!
But not so fast…
The thing is just because you send a mail, it doesn’t mean that the receiving end thinks the mail is valid. And chances are the receiving end has decided a random e-mail sent off of a random server is simply SPAM.
I have posted a more detailed answer here, but when it comes to SPAM it basically boils down to this: Do you have an SPF (Sender Policy Framework) record setup for your domain? Do you also have a PTR (reverse DNS) record set for that domain?
If you do not have an SPF or PTR record, the chance of your message simply being flagged as SPAM is quite high.
If you are serious about sending mails off of your server, you need to at least get your SPF record & PTR record set.
You don't have any headers in your email.
Although they don't seem to be required, your emails will be blocked by spamcheckers a whole lot sooner.
This is an example:
<?php
$headers = "MIME-Version: 1.0\n" ;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$headers .= "X-Priority: 1 (Highest)\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "Importance: High\n";
$status = mail($to, $subject, $message,$headers);
?>

Configuring PHP to Function with SMTP

I have a register script that sends an email to who ever signs up.
//// Set headers ////
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= "Content-type: textrn";
$headers .= "From: $from";
/// Send the email now ////
mail($email1, $subject, $message, $headers, '-f nleachman#stayontrack.net');
//mail($email1, $subject, $message, $headers, '-f noreply#your-email.com');
$db->commit();
echo "Thanks for joining! Check your email in a few moments to activate your account so that you may log in. See you on the site!";
$db = null;
exit();
}
catch(PDOException $e){
$db->rollBack();
echo $e->getMessage();
$db = null;
exit();
}
Here is my SMTP server used for configuration:
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = nleachman#stayontrack.net
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
When ever I try to run my log-in script I get this:
Thanks for joining! Check your email in a few moments to activate your account so that you may log in. See you on the site!
My problem is that who ever signs - up never recieves the email. At first I thought that it may just be an impossible function on localhost. Then however, I saw some codes that worked for other people but gave me an error like...
Variable $From not set
How could I configure my code so that it runs without errors?
Also, how could I configure the code to make sure who ever the email is sent to recieves it?
For using SMTP within PHP, you should have a look at phpmailer. https://github.com/Synchro/PHPMailer
easy to configure and does all the work for you.
You're using mail() to send the email - I've had problems with this before not sending to certain domains like hotmail. I'd give PEAR mail a go - http://pear.php.net/package/Mail/redirected.

PHP Mail sending to external e-mail addresses but not internal

Started off with e-mails not being sent at all, but then I used the "-f" parameter in the function, which then works to send to external addresses (Gmail and Hotmail tested so far) but it won't work for addresses that are on the domain though. Just wondering if it's in the code or is it a problem with the server setup?
if ($Valid == 1) {
$_POST = array_map('strip_tags', $_POST);
$_POST = array_map('stripslashes', $_POST);
$To = "user#domain.ca";
$Subject = "Online Driver Application";
$Body = "All the values of the form that was filled out (removed because there was a lot and it doesn't affect the problem)";
$Headers = 'MIME-Version: 1.0' . "\r\n";
$Headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$Headers .= 'From: Company <info#domain.ca>' . "\r\n";
$Headers .= 'Reply-To: no-reply#domain.ca' . "\r\n";
$Headers .= 'X-Mailer: PHP/' . phpversion();
mail($To, $Subject, $Body, $Headers, -finfo#domain.ca);
echo '<div class="success">Thank You! Your form has been successfully submitted.</div>';
} else {
if ($ErrorMsg != '') {
echo '<div class="error">'.$ErrorMsg.'</div>';
}
Again, unless I have the -finfo#domain.ca in the mail function, e-mails don't get sent out at all.
Thanks.
The additional_parameters parameter (-f or -r) can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option.
The additional_parameters(> 4.2.2) is disabled in safe_mode and the mail() function will expose a warning message and return FALSE when used.
The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a 'X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is /etc/mail/trusted-users.
(or, remove sendmail and install postfix (much easier) if still in problem.)
The code seems OK. This is probably something not setup correctly (like PHP.ini not having the SMTP server or a Firewall blocking something). Also, make sure newlines are "\n" in $Body I've seen some mail servers rejecting mails because of this. Using mail directly is always tricky. Try to use a library like Swiftmailer or PHPMailer instead. This way you don't have to worry about how to send HTML, attachments...

how to know if php mail failed

I am sending mails from php mail() : and I want to receive a failed message if sending is failed to the destinatio .
$to = 'itsdfdsf#7sisters.in';
$email_from = "info#7sisters.in";
$full_name = 'XXXX';
$from_mail = $full_name.'<'.$email_from.'>';
$subject = "testing sender name";
$message = "";
$message .= '
<p><strong>This is only a test mail. Please do not reply.</strong><br />
';
$from = $from_mail;
//$headers = "" .
// "Reply-To:" . $from . "\r\n" .
// "X-Mailer: PHP/" . phpversion();
$headers = "From:" . $from_mail . "\r\n" .
"Reply-To:" . $from_mail . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if(!mail($to,$subject,$message,$headers))
{
echo 'failed !!';
}
But although $to mail does no exist,it is not showing failed !!
The mail method is just sending the mail out. If it does not receive any errors (e.g. by not finding the server etc), it will return succesfull. You will not be able to know if the mail actually landed in the inbox of the recipient unless you create some code around bounced emails etc.
I think what you want is to check for a real email not only a valid formatted email. So I would suggest you to have a look at this blog
check the return from of mail
Returns TRUE if the mail was successfully accepted for delivery, FALSE
otherwise.
It is important to note that just because the mail was accepted for
delivery, it does NOT mean the mail will actually reach the intended
destination.
Although the fact it is returning true probably means that your mail program is accepting the message but then failing when it tries to send to no one...
You should run the $to through a validator to check its a valid address and then throw an error if its not, don't rely on mail() to filter out things which you already know are wrong, or can check against easily.
--UPDATE
Then check out #SeRPRo , but what your trying to do is hard work to test programatically - its far easier and more reliable to send an e-mail which requires the user to click a link to verify that it's real than try querying SMTP servers which all have different behaviour (read: are broken to different degrees). Also note that your intended behaviour (code wise) is hard to differentiate from a spammers so don't be surprised to find it difficult going if you avoid the verification e-mail route.
But although $to mail does no exist,it is not showing failed !!
actually the fact that mail is being delivered to SMTP server, doesn't mean it will be delivered to the end user. There's no easy way in PHP to check whether it's delivered.
You could CC yourself as a way of testing that it is leaving the outbox.
In my case it helped to set the return-path via the commandline parameter "-f", which can be passed in the $additional_parameters parameter of mail(). so i call
mail($to, $subject, $message, $headers, "-f address.where.i.want.the.bounces#xy.com");
... according to some comments on http://www.php.net/manual/en/function.mail.php hosting-inviroments react different and have different restrictions (address might need to be registered in the same hosting-account, or be on the same domain, the same as the "From:" in the heade ... and so on)
The page where I got the bounces to be received (with non of the mentioned restrictions, as it seems) is hosted at Domainfactory http://www.df.eu
Use phpmailer to send email and set $mail->AddCustomHeader('Return-path:bounce#mail.com');
This will send bounce email at bounce#mail.com if recipient mail id does not exist or recipient does not receive email by any other case.

Send mail through mail relay with PHP

I ran into a wee issue when using mail(). I wasn't able to send to addresses off the domain I was hosting the form. I understand this is for security reasons but it makes the creation of a 'send to friend' system a little tough.
Here's what I had working (albeit it only sent to my address):
<?php
$senderName = $_POST['name'];
$friendsEmail = $_POST['friendsEmail'];
if ($_POST['formName'] == 'refer') {
$to = $friendsEmail;
$subject = "$senderName has referred you";
$message = "Message goes here";
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: Me <no-reply#test.com>' . "\r\n";
mail($to,$subject,$message,$headers);
}
header("Location: referConfirm.html");
?>
I've talked to my hosting company who setup a mail relay (yay!). Trouble is, I have no idea how to get what I working above through a mail relay. I have the following details:
IP: 000.000.000.000
Domain: domain.company.com
UN: username
PW: password
(Details are dummy.)
Can anyone give me a clue?
Thanks,
#rrfive
mail() uses the smtp/sendmail settings found in php.ini. If you need to send it via another smtp, or one with authentication (like in your example) mail is simply not enough.
There are good mailer libraries out there, just to name a few:
Swift Mailer
Zend_Mail
PHPMailer
They are all capable of sending emails via an authenticated smtp server.

Categories