Variable equals variable will not work [closed] - php

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'];

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

Convert escaped characters PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I know this is a simple question, but having a hard time finding a simple solution.
I have a form on my PHP site that takes in the user message, then sends an email to my client with the message. I want the message sent to my client to be easily readable, without escaped characters \r , \n , \" , etc.
For example, the message is currently coming into the email as:
test\'s\r\nnew line \r\n\"quote\"
What I want it to come in as:
test's
new line
"quote"
Current code is:
if(isset($_POST['submit']) && !empty($_POST['email'])){
//Process form
$name = $_POST['name'];
$name = mysql_prep($name);
$email = $_POST['email'];
$email = mysql_prep($email);
$message = $_POST['message'];
$message = mysql_prep($message);
$emailto = "client_email1#gmail.com,client_email2#gmail.com";
$subject = "Message from '$name'";
// the message
$message = "<html><body>
<h3>Title</h3>
From: $name<br>
Email: $email<br><br>
Message:<br>
$message<br><br>
</body></html>";
//headers make the mail() work
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($emailto, $subject, $message, $headers);
What you need is stripslashes() and nl2br().
nl2br will make newlines html br tags.
Stripslashes does what the name indicates, it removes slashes that is escape slashes.
echo stripslashes(nl2br($str));
Example: https://3v4l.org/RPYWt
Couple issues, first was my $message = mysql_prep($message); function was creating the escaped characters, so I just put that function after the mail() function. mysql_prep() was used to prevent mysql injection.
Second, I needed the $message = nl2br($message) function to maintain the user entered new lines from the form so the client can see the message as it was typed. I put the nl2br() function just after the $message = $_POST['message']; line.

php html mail not working on cpanel server [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have a code to send html mail with php on cpanel but doesn't send without any errors. I have tested plain text mail and that is working fine, but once I add the header like below, it doesn't work.
//send html mail
ini_set('sendmail_from', 'info#toludimaokojie.com');
$headers = "From: info#toludimaokojie.com\r\n"."X-Mailer: php";
$headers .= "Content-type: text/html\r\n";
$html = '<html>
<body>
<h2>Result Analysis for Test With Reference Number:'.$reference.'</h2>
<br/>
<p><b>Personality: '.$personality_mail.'</b></p>
<br/>
<p><b>MBQ Score: '.$resultTotal.'</b></p>
<br/>
<ul>'.$analyseData.'</ul>
<pre>Mail Sent on '.date("l, F Y H:i:sa").'</pre>
</body>
</html>';
mail("olaegbesamuel#gmail.com", "MBQ TEST ANALYSIS", $html, $headers);
This doesn't work. Please help, i guess am doing everything correctly here. I have tested without html and confirmed that mail is working
You have defined $headers with all mandatory email parameters but look at your $header variable, there are two things need to be changed,
1) You have used $header and $headers both so use either of them and append them to one variable.
2) your 2nd line of $header variable is missing . to append the previous header values, so the corrected code should be:
$headers = "From: info#toludimaokojie.com\r\n"."X-Mailer: php";
$headers .= "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
As you missed . operator in 2nd line, it will remove From header and so it would not be sending email.
Try after changing these lines.

PHP Mail not working for me [closed]

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.

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