PHP Mail not working for me [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a mail form with PHP and I have something like this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$to = 'mymail#gmail.com';
$subject = 'Test mail';
$body = "From: $nombre\n E-Mail: $email";
if ($_POST['submit']){
mail ($to, $subject, $body);
header("Location: http://www.example.com/");
}
?>
As you can see the code is really simple, it's just sending a name and an adress but it doesn't work. Whenever I try to send it, the page just change the title to "mypage/send.php" and that's all.
It does not even redirect me to an example page.
I've tried:
Using headers (as recommended by others from here).
Checking if there's a grammatical error.
Testing it in a real page and in localhost with SMPT.
EDIT: HTML USED:
<form method="post" action="send.php">
<input type="text" name="name" placeholder="Your name">
<input type="email" name="email" placeholder="Email">
<input type="submit" value="send">
</form>

Firstly, you don't have an assigned variable called $nombre so having error reporting set would have thrown an undefined variable warning; you may have meant to use $name instead.
Check your Spam since you don't have a proper From: in your header, and/or the Email is being rejected altogether due to that reason, this is a common thing nowadays.
Check that all your form elements are indeed named, this includes your submit button, since your conditional statement depends on this, and you haven't provided your HTML form.
I.e.: name="name" and name="email" and for the submit button name="submit".
It's also better to use isset(). I.e.:
if(isset($_POST['submit'])){...}
instead of if ($_POST['submit'])
For more information on mail/headers, visit:
http://php.net/manual/en/function.mail.php
Example from PHP.net:
<?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();
mail($to, $subject, $message, $headers);
?>
Add error reporting to the top of your file(s) which will help find errors either.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
For more information on mail/headers, visit:
http://php.net/manual/en/function.mail.php
Example from PHP.net:
<?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();
mail($to, $subject, $message, $headers);
?>
A few things to note are:
Is PHP installed on your server and properly configured?
Is mail installed and properly configured?
If running from your own computer, all of the above apply.
"it's just sending a name and an adress but it doesn't work"
You will need to be more precise with this. "It doesn't work" doesn't define nor describe how it's not working, or any error messages you may have gotten.
If it isn't redirecting, then you may be outputting before header, another common mistake, which is something that error reporting will catch, do use it.
Here is an article on Stack about outputting before header (headers already sent), should you get that warning:
How to fix "Headers already sent" error in PHP

You should check the value of $_POST["submit"] with var_dump and the return value from mail(which is boolean).
Obs.: It is a good pratice to filter all input coming from $_GET and $_POST.

Related

how to send a message to email with php

I'm trying to send a message to the email a user provides in the contact form. The problem is the message never gets sent, but I always arrive at a blank page where my php code is located. Nothing warns me of any error in my code. Can anyone explain why this is happening and offer a solution to the problem?
<form action="site.php" method="POST">
<input
type="text"
class="form"
name="email"
placeholder="Your email address"
/>
<button class="submit" type="submit">Join Waitlist</button>
</form>
<?php
if (isset($_POST["submit"]))
{
$mailTo = $_POST["email"];
$mailFrom = "Dumele";
$message = "https://docs.google.com/forms/d/1lpj2XnKW4HT_qHFfGwpUxcvzPmK2USZ0MGSDP0XCqfg/edit";
$subject = "Welcome to Dumele";
$txt = "Thank you for your interest in Dumele. We're glad to have
you join our network and mission to enhance the technological
innovation of our African diaspora. Below is a link to a survey
we would like you to answer so we can better assist you.\n\n".message;
$headers = "From: ".mailFrom;
(mail($mailTo, $subject, $txt, $headers));
header("Location: index.php?mailsend");
}
?>
First of all make sure you enabled error reporting. You can check another Stackoverflow question and it's answers here about it.
As I see in your code you have syntax errors. You didn't place $ sign before variable names. For example you typed $headers = "From: ".mailFrom; instead of $headers = "From: ".$mailFrom; Let's fix it:
<?php
if (isset($_POST["submit"]))
{
$mailTo = $_POST["email"];
$mailFrom = "Dumele";
$message = "https://docs.google.com/forms/d/1lpj2XnKW4HT_qHFfGwpUxcvzPmK2USZ0MGSDP0XCqfg/edit";
$subject = "Welcome to Dumele";
$txt = "Thank you for your interest in Dumele. We're glad to have
you join our network and mission to enhance the technological
innovation of our African diaspora. Below is a link to a survey
we would like you to answer so we can better assist you.\n\n".$message;
$headers = "From: ".$mailFrom;
(mail($mailTo, $subject, $txt, $headers));
header("Location: index.php?mailsend");
}
Now with the mail() function of PHP; some servers disables mail() function for security purposes. If so; you can use SMTP to securely send your emails. To use SMTP in PHP of course you need additional processes but some free software packages and libraries like PHPMailer or SwiftMailer can help you about it.
This is looking for a form value with the name "submit":
if (isset($_POST["submit"]))
But there's no form element in the HTML with that name. So this will always be false. Give your submit button that name:
<button class="submit" type="submit" name="submit">Join Waitlist</button>
It shouldn't necessarily need a value, it would just default to an empty string. But it needs a name in order for the browser to send anything at all with that key.
As an aside, your mail server may reject the message since this is not really an email address:
$mailFrom = "Dumele";
For completeness... It looks like your PHP variables are also syntactically incorrect. Variable names need to begin with a $. For example, this:
$headers = "From: ".mailFrom;
Should be this:
$headers = "From: ".$mailFrom;
The same error would need to be corrected anywhere you're mis-using variable names.
Use value attribute in button tag. You are testing
if(isset($_post['submit']))
But what is submit? You should use value attribute and give a value submit i.e. Submit

Variable equals variable will not work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Right now im working on my signup and activation email, i have one problem it will not send the email, yes i have tested it, i have set
$to = 'myemail#example.com';
to my email before and it has worked but when i have
$to = $e;
it doesnt work
i have check and it does make the email the user puts in into a variable which is $e
so i have no idea what the problem is if someone can help that would be great
heres the mail form:
mail($to, $subject, $message, $headers);
EDIT:
here is my whole mail code
$to = $e;
$from = "auto_responder#mywebsite.com";
$subject = 'Account Activation Success';
$message = '<html>Success</html>';
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
mail($to, $subject, $message, $headers);
echo "signup_success";
exit();
EDIT x2:
I feel this will never be found out
i even tried this
$to = mysqli_real_escape_string($db_conx, $_POST['e']);
which post['e'] is the email the user put in
EDIT x3
after some test i noticed something
when i do
$to = 'myemail#gmail.com';
it doesnt send
but with
$to = 'myemail#hotmail.com';
it works so i guess its not sending to gmails?
It sounds like you want to get the 'e' query parameter from a form submission. If that's the case, try this:
$to = filter_input(INPUT_GET, 'e', FILTER_VALIDATE_EMAIL);
If your form uses POST, then swap in INPUT_POST instead of INPUT_GET. If you don't care about filtering or don't have the filter extension, you could just do this, which works with both GET and POST.
$to = $_REQUEST['e'];

How to set the from name in the mail() function [duplicate]

This question already has answers here:
problem with php mail 'From' header
(8 answers)
What is the format for e-mail headers that display a name rather than the e-mail?
(3 answers)
Closed 8 years ago.
When I send a message using the code below, it sends fine. But if I change the $headers to have 'From: Firstname Lastname' it doesn't send for some reason. How do I add my name to the header instead of the email address?
$subject = 'Test message';
$headers = 'From: mymail#mydomain.com' . "\r\n" .
'Reply-To: mymail#mydomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $themessage, $headers);
Some (most, I think) email clients check the From field to make sure the domain is the same as the domain that sent the message. This is to prevent spoofing. Try something like
"From: Firstname Lastname <name#domain.com>"

how to keep the display on the current slide [javascript slider] after submit button is pressed

Situation:
I have a webpage utilzing a javascript slider [tiny slider from scriptiny dot com].
I created a contact form on the last slide with jquery validation and php validation.
Everything works fine EXCEPT for:
After clicking on Submit, the webpage displays the first slide.
Not sure hot to make it stick to the contact "slide".
Site navigation is done using a string like:
onclick="slideshow.pos(0)
onclick="slideshow.pos(1)
onclick="slideshow.pos(2)
onclick="slideshow.pos(3)
onclick="slideshow.pos(4)
I need the contact "slide" to stay on "slideshow.pos(4)".
Code:
//If there is no error, send the email
if(!isset($hasError)){
$emailTo = 'myemail#gmail.com'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
How can I do this? Ideally, I would be able to run some javascript after the email is sent making the refresh of the page send the user to "slideshow.pos(4)"??
Any help would be appreciated,
Mark
You can "submit" your data with an Ajax call and avoid the page from refreshing in the first place. If you never used Ajax take some time to look at https://developer.mozilla.org/en/ajax.

Why won't my HTML/PHP form work?

Ok, so I made a form using HTML, and I'd like to get it to submit the information to my email, so I found and modified this PHP script:
<?php
$to = "me#myemail.com";
$subject = "R&R Form";
$firstname1 = $_REQUEST['firstname1'] ;
$lastname1 = $_REQUEST['lastname1'] ;
$firstname2 = $_REQUEST['firstname2'] ;
$lastname2 = $_REQUEST['lastname2'] ;
$department1 = $_REQUEST['department1'] ;
$department2 = $_REQUEST['department2'] ;
$reason = $_REQUEST['reason'] ;
$behaviour1 = $_REQUEST['behaviour1'] ;
$behaviour2 = $_REQUEST['behaviour2'] ;
$behaviour3 = $_REQUEST['behaviour3'] ;
$behaviour4 = $_REQUEST['behaviour4'] ;
$behaviour5 = $_REQUEST['behaviour5'] ;
$behaviour6 = $_REQUEST['behaviour6'] ;
$behaviour7 = $_REQUEST['behaviour7'] ;
$message = "Nominee: $firstname1 $lastname1 /n Department: $department1 /n /n Nominator: $firstname2 $lastname2 /n Department: $department2 /n /n Reason for nomination: $reason /n /n Additional reasons: $behaviour1 /n $behaviour2 /n $behaviour3 /n $behaviour4 /n $behaviour5 /n $behaviour6 /n $behaviour7 /n";
$headers = "Recognition and Reward Request for $firstname1 $lastname1";
$sent = mail($to, $subject, $message, $headers,) ;
if($sent)
{print "Your nomination was submitted successfully"; }
else
{print "We encountered an error submitting your nomination"; }
?>
It's not very well written, I know (I only started learning php today, and I just modified a script I copy and pasted.), but it doesn't seem to have any syntax errors or any other errors I can see. I'm not asking for someone to fix my code for me, I'm just asking for some pointers as to why the script isn't working as it should.
I uploaded it to a server with PHP installed, so that's not the problem. I've been trying to figure this out all day, and it's getting kinda frustrating. Someone please help?
Well, the script is using this for the headers, which is invalid:
$headers = "Recognition and Reward Request for $firstname1 $lastname1";
Maybe you meant for that to be the subject line?
The headers should be valid SMTP headers, like this:
$headers = 'From: webmaster#example.com' . "\r\n";
Look at the examples for the mail function for more info.
$sent = mail($to, $subject, $message, $headers,) ;
should look like this:
$sent = mail($to, $subject, $message, $headers) ;
(without the comma)
hope i helped
Since you are a starter I recommend you to use PEAR as much as possible.
Look at: pear html quickform
It will really make your life easier.
And for sending e-mails, I suggest you to use: PHPMailer
It comes with a lot of e-mail features just right out-of-the-box
The first problem I see is that $headers doesn't contain valid headers. Headers are things like From: name#example.com or CC: someoneelse#example.com, but you're treating it as part of the email.
Here's some info on email headers.
It does have syntax errors. Since you cannot see them, I suggest you enable full error reporting. There're many ways to do it; the simplest is probably adding this code on top of your script:
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
?>

Categories