I have a php page that includes an inquiry form that refers to itself as the form action.
Once completed the form writes to a database within a try-catch construct. I want to send an email to the administrator to say that someone has added themselves to the database.
All of the code works until:
if(mail($to, $subject, $message, $headers, '-f' . $from))
{ #And finally send them a thanks
header('Location: thanks.html.php');
exit();
} else {
echo 'Email did not send';
exit();
}
The code above this block all works because I get a write into the database, and the 'if' test passes because the redirect to the thanks page also works! What have I missed?
Seems like you are trying to make use of additional parameters.
Exerpt from PHP Manual
The additional_parameters parameter can be used to pass an additional
parameter to the program configured to use when sending mail using the
sendmail_path.
<?php
mail('nobody#example.com', 'the subject', 'the message', null, '-fwebmaster#example.com');
?>
Route : 2
Try something like this.
Your $from is contained in the $headers variable itself , so you don't have to specify.
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers))
{ #And finally send them a thanks
header('Location: thanks.html.php');
exit();
} else {
echo 'Email did not send';
exit();
}
?>
AT last...The answer. Nothing wrong with the code! I was falling over the web hosts anti spam filter in mail. Because I was testing the script with my own email address, it refused to send the mail assuming it was spam! As soon as I put in a completely new email address it sent the email!
Thanks to everyone that offered help!
Related
I am trying to send an email through php. But the mail() function keeps returning FALSE even though I am sure everything is correct.
After browsing through stackoverflow for similar problems, one answer stood out by saying that it could also be because of the server.
NOTE: The mail server part of the nginx server isn't configured yet. I have no idea if PHP needs that specific module in order to work.
# $email and $randomPassword already defined.
$subject = "New Password";
$message = "Your new password is ".$randomPassword;
$headers = 'From: contact#mail.com' . "\r\n" .
'Reply-To: contact#email.com';
$checkMail = mail($email, $subject, $message, $headers);
if ($checkMail) {
echo "Mail send";
} else {
echo "Mail not send";
}
I'm trying to use noreply to send out emails, but it doesn't work - it wont sent anything. I am using this test-file:
<?php
$to = 'myemailhere';
$subject = 'You received an email message!';
$message = 'This is a message in the email body form.';
$headers = 'From: noreply#example.com' . "\r\n" .
'Reply-To: noreply#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
What could be the problem? By the way, I am using rackspacke, if anyone should know about it.
Thanks in advance!
If you're running on a Rackspace cloud server, you will have to configure the server yourself to be able to send mail. The mail() function relies on the OS configuration to handle emailing. If you are on a Rackspace cloud site, you will likely need to contact Rackspace support for help.
Personally I dodge this bullet altogether by using PEAR's SMTP class. This is a full SMTP implementation in PHP and since it does not rely on any outside configuration or module, it is fully portable. It has saved me a LOT of trouble.
http://pear.php.net/package/Mail
http://pear.php.net/package/Net_SMTP
Note: The PEAR site is having some loading issues atm for me. Give it a minute and it should load.
what is exception? use try catch block. Else try this code
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
<?php
$to = 'something#gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: example#test.com' . "\r\n" .
'Reply-To: example#test.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
$send = mail($to, $subject, $message, $headers );
if ($send)
$mailReturns = "Mail sent successfully.";
else
$mailReturns = "Mail sent failed.";
?>
<?php echo $mailReturns; ?>
after run it shows mail sent successfully
but mail not showing up in any of the accounts. iv tried gmail, yahoo, rediff and ibibo why????
If you have qmail installed on your server, try using that. Alternatively, there is a nice PHP mailer class called swiftmail that is available here: http://swiftmailer.org/ you can try.
Your emails are probably being junked/marked as spam.
Try to see in spam folder.
I am using a PHP Mail form with AJAX and it's not sending any mail. What am I missing here?
send.php
<?php
error_reporting(E_NOTICE);
function valid_email($str)
{
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*#([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
if($_POST['first_name']!='' && $_POST['last_name']!='' && $_POST['e_mail']!='' && valid_email($_POST['e_mail'])==TRUE && strlen($_POST['message'])>30)
{
$to = 'zacharyrs#gmail.com';
$headers = 'From: '.$_POST['e_mail'].''. "\r\n" .
'Reply-To: '.$_POST['e_mail'].'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject = "Hello! I'm testing my new ajax email that I got from roscripts.com";
$message = htmlspecialchars($_POST['message']);
if(mail($to, $subject, $message, $headers))
{//we show the good guy only in one case and the bad one for the rest.
echo 'Thank you '.$_POST['first_name'].'. Your message was sent';
}
else {
echo "Message not sent. Please make sure you're not
running this on localhost and also that you
are allowed to run mail() function from your webserver";
}
}
else {
echo 'Please make sure you filled all the required fields,
that you entered a valid email and also that your message
contains more then 30 characters.';
}
?>
Is your email setup in PHP? Check that first, otherwise there doesn't seem to be any issue here that I can see. You should get some kind of output from this if/else block.
I would start by reviewing PHP error logs, to see if your mail() fn is working. That may be the cause.
I have successfully sent mail using PHP's mail() function before, and for my password reset notification e-mail, I copied the syntax I was using elsewhere, but I guess I messed it up, as it's not arriving at its destination. Here is the code I'm using:
$headers = 'To:'.$email."\r\n";
$headers .= 'From: webmaster#aromaclear.co.uk'."\r\n";
$to = $email."\r\n";
$subject = 'AromaClear Password Reset Notification'. "\r\n";
$msg = 'From: AromaClear'."\r\n";
$msg .='Subject: Your New Password'. "\r\n";
$msg .= 'Message: Your new password is '.$newpass."\r\n";
$msg.= 'If you have received this e-mail in error, please ignore it.'. "\r\n";
mail($to, $subject, $msg, $headers);
Any thoughts?
Try looking at your server's mail logs to see why it isn't getting forwarded. Ex., it may be that this server's sendmail wants the -f flag for the From header instead of specifying it in the header text.
mail($to, $subject, $msg, $headers, "-f $from");
Also, you seem to be doing a lot of extra/weird work. This is a lot easier:
$subject = "AromaClear Password Reset Notification";
$headers = "From: webmaster#aromaclear.co.uk";
$msg = "Your new password is $newpass\r\nIf you have received this e-mail in error, please ignore it.\r\n.";
if(mail($email, $subject, $msg, $headers))
{
//handle success
}
else
{
//handle failure
}
Change style to your preference.
have you checked the return value of mail(). If it's not FALSE then it's accepted for delivery and the code is fine, but something is messed up somewhere else.
That looks fine to me, perhaps do
if (mail($to_email,$subject,$message, $headers))
echo 'Success';
else
echo 'Error';
}
That might let you know if it's trying to send at all.
Just don't add "\r\n" everywhere, use it only to separate headers.
In the message you can use only \n, it will work.
And at the end of the subject and receiver there's no need for "\r\n".