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.
Related
I'm having trouble making this properly work.
What I want to happen is for someone to be able to fill out the given information but there's an email validation which is just you put your email in Email: and is suppose to match in Verify Email: then in hopes that they did that write and if they answer the spam question right which is just "what is 5+5?" then the form will send to my email
The problem is that, although the email validation is working, the spam question even if you write the answer wrong, the form is still sent. I need it so that the spam and the email validation either or must be correct or the form won't send.
I hope made this simple for some to understand and help me out! Here's my PHP code. Let me know what I'm doing wrong.
<script>
<?php
$email1=$_POST['Email1'];
$email2=$_POST['Email2'];
$from=$_POST['Email1'];
$email="!YOUREMAIL#GOES.HERE!";
$subject="maintenance Request";
$message=$_POST['Box'];
$select=$_POST['Select'];
$name=$_POST['Name'];
$number=$_POST['Question'];
$message="Name: ".$name. "\r\n" ."\r\n" . "Email: " .$from ."\r\n" . "\r\n" . "Comment: " .$message ."\r\n" . "\r\n" . "Selected Machine: " .$select;
if($number==10) {
}
if(filter_var($email1, FILTER_VALIDATE_EMAIL)) {
}
if (filter_var($email2, FILTER_VALIDATE_EMAIL)) {
mail ($email, $subject, $message, "from:".$from);
echo 'Thanks You for the Maintenance Request! We will Contact you shortly. ';
}
else {
echo "This ($email2) email address is different from ($email1).\n";
}
?>
</script>
If the answer to the validation question is wrong, you should print a message and exit the script.
if ($number != 10) {
die("You are not a human!");
}
You're also never checking if the two emails are the same.
if(!filter_var($email1, FILTER_VALIDATE_EMAIL)) {
die("Invalid email ($email1)");
}
if ($email1 == $email2) {
mail ($email, $subject, $message, "from:".$from);
echo 'Thanks You for the Maintenance Request! We will Contact you shortly. ';
}
else {
echo "This ($email2) email address is different from ($email1).\n";
}
There's no need to call filter_var() on $email2. If it's equal to $email1 then it must be valid. If it's not equal to $email1, we don't care if it's valid.
I'm trying to send an email from a MODx template, either just using PHPmail or with MODx's ModMail class. Needless to say neither way is working.
I'm writing the code in a MODx snippet, and including that snippet in my template. When using PHPmail, and with the form action omitted (so that the form submits to the current URL), the page refreshes but no mail is sent.
When I try to use ModMail, nothing happens at all. But I'm not quite sure how to actually call the send mail code in this case, so the code is just sitting there doing nothing.
This is my PHPmail attempt:
<?php
$to = $_POST['email'];
$name = $_POST['name'];
$query = $_POST['message'];
$subject = "Query from " . $name;
$message = "You're received a query from " . $name . ", their email address is " . $to . ".\r\nThey said:\r\n" . $query;
$headers = 'From: MyPersonalEmail#gmail.com' . "\r\n" .
'Reply-To: MyPersonalEmail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
echo $to;
echo $name;
echo $query;
echo $subject;
echo $message;
echo $headers;
mail($to, $subject, $message, $headers);
?>
And this is with ModMail:
<?php
$message = $_POST['message'];
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,'MyPersonalEmail#gmail.com');
$modx->mail->set(modMail::MAIL_FROM_NAME,'Johnny Tester');
$modx->mail->set(modMail::MAIL_SUBJECT,'Check out my new email template!');
$modx->mail->address('to','MyPersonalEmail#gmail.com');
$modx->mail->address('reply-to','MyPersonalEmail#gmail.com');
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}
$modx->mail->reset();
There is a MODX extra available called QuickEmail, that could check the internal mail functionality.
I do all the email handling via the MODX extra FormIt. Look at the rtm for it, it is quite easy to get running. It can handle a lot of the stuff you want to do and prevent (like spam, multisubmit) when having a form.
https://docs.modx.com/extras/revo/formit/formit.tutorials-and-examples/formit.examples.simple-contact-page
Don't try and invent a new solution. Most stuf can be done by using or extending existant MODX extras.
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 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!
I'm using a PHP contact form and it is sending mail to non gmail addresses, however when I set it to send to a gmail address, it doesn't get delivered (it doesn't even appear in junk mail).
I've heard of issues like this before - I'm not a web developer/expert so can anybody suggest code/configuration changes to my PHP contact form below which would essentially mean messages get delivered to gmail addresses?
I'm on a linux/WHM dedicated server.
<?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(!empty($_POST['name']) && !empty($_POST['email']) && valid_email($_POST['email']) === true && !empty($_POST['comment']))
{
$to = "contactform#gmail.com";
$headers = 'From: '.$_POST['email'].''. "\r\n" .
'Reply-To: '.$_POST['email'].'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject = "Contact Form";
$message = htmlspecialchars($_POST['comment']);
if(mail($to, $subject, $message, $headers))
{
echo 1; //SUCCESS
}
else {
echo 2; //FAILURE - server failure
}
}
else {
echo 3; //FAILURE - not valid email
}
?>
Did you check to see if your PHP installation/server supports the Mail() function?
Also did you check to see if Gmail is treating your emails as spam? (they wont show up in the spam folder, they are just blocked)... Try sending it to a different address.
Set your script to send it from an email address that has an actual mailbox somewhere so you can check for bounces. If your script echoing 1, and can send to other addresses it suggests (to me at least), that it's gmail not liking you, rather than anything wrong with the script per se.
It also looks like you need to look into protecting this script from email injection. Just a suggestion though.