How to send a message to my e-mail programmatically - php

I need a little help with sending a message to my e-mail via php. Here's the code I've got so far:
<?php
$newline = $_GET['message'];
$newline = str_replace("[N]","\n"
,"$newline");
$newline = str_replace("[n]","\n"
,"$newline");
mail($_GET['to'],$_GET[
'subject'],$newline,"From: ".$_GET['from']);
echo "<FONT FACE=\"Verdana\" SIZE=\"2\">".$_GET[
'thanks']."</FONT>";?>
I uplaoded this php code to my sever, then use the execute_shell() function to send the message to my e-mail by then defining the various variables. Here's that code:
msg_to="linera#low.com";
msg_from="celebraces#gmail.com";
msg_subject="Hey, what's up?";
msg_message="This is a test email :D";
msg_thanks="Your message has been sent :D";
execute_shell("http://yourserver.com.au/
yourgame/sendmail.php?to="+msg_to+"&from="
+msg_from+"&subject="+msg_subject+"&
message="+msg_message+"&thanks="+msg_thanks,"");
The problem is that, when the message is done, it just shows msg_thanks, which I defined as a variable. I'm new to php and dont know much. All I want to do is to remove this and rather, redirect the user to a particular thank you webpage when the message is done. How do I do this?

Try header():
header("location:../thankyou.php");

Related

The PHP Mail Function is not Working on my Website [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed last year.
I have been trying to build my own website and a central part of it is a form. I have set up an email address to recieve form results. I have been able to run the website on localhost and everything but the forms are working. When I click submit, it sucsessfully sends me to the PHP file and I am not recieving any emails from the file. I have checked my spam and it is empty. I am brand new to PHP and I tried to adapt some code I found online. I am not sure if there is anything wrong with my code or not. If anyone could help that would be great. I am also trying to be able to send an image file through the form. This is a screenshot of what the server log says when I try to submit the form. Server Log Screenshot
This is the code I have wrote:
<?php
$title = htmlspecialchars($_POST['title']);
$coordinates = htmlspecialchars($_POST['coordinates']);
$description = htmlspecialchars($_POST['description']);
$shelterorcampsite = htmlspecialchars($_POST['shelter-campsite']);
$distanceToWater = htmlspecialchars($_POST['howfarawayiswater']);
$image = $_POST['file'];
$email_from = 'contactbackpackingproject#gmail.com';
$email_subject = "New Form submission";
$email_body = "There has been a new campsite submission.
The title is: $title;
The type is: $shelterorcampsite;
The coordinates are: $coordinates;
The description is: $description;
The distance to water is: $distanceToWater;
An image is: $image";
$to = 'contactbackpackingproject#gmail.com';
mail($to,$email_subject,$email_body,$headers);
?>
This is a screenshot of the code is VS code: VS code screenshot
Often it's helpful to find the exact error message that is triggered by the mail() function. While the function doesn't provide an error directly, you can use error_get_last() when mail() returns false.
<?php
$success = mail('example#example.com', 'My Subject', $message);
if (!$success) {
$errorMessage = error_get_last()['message'];
}
?>
Please identify the errors as without them nobody can help you and the log that you provided is just a POST request which doesn't necessarily indicate that mail function executed successfully.

PHP email is sending twice when form is submitted

I set up a custom price quote feature that sends the user an email when they click submit. The issue I'm having is that even if you hit 'Submit' once, it always sends two emails. I'm using this on a a WordPress site and this code is part of the price quote plugin that I wrote.
I set up a variable called $testemail that is set to 1, and is supposed to increment by 1 when the wp_mail function is called, but both emails are still showing just the number 1 so the second email doesn't increment it at all.
Here's my code that is sending the email:
$email_array = array($get_option_array['franchise_email'],
$email_address_sanitize, "info#example.com");
$subject = "Price Quote Submission";
$to = $email_array;
if (isset($_POST['submit-clicked'])) {
if ($check_human_clean != 2) {
echo '<div>';
echo '<p class="error">Human verfication is incorrect. Please try again.</p>';
echo '</div>';
}
else {
if (wp_mail($to, $subject, $message)) {
echo '<div id="dialog" title="Price Quote Submitted">';
echo 'div id="popmake-233">';
echo '<p>Your price quote has been sent successfuly. We\'ll be in touch shortly. If you don\'t hear from us, please give us a call directly at (909) 982-9999. Thank you!</p>';
echo '</div>';
$testemail++;
}
else {
echo '<p class="error">An error occured while processing your price quote. Please contact the franchise owner at ';
echo $get_option_array['franchise_phone'];
echo '</p>';
}
}
}
Everything I've read is that the code must be run twice, but my $testemail variable isn't incrementing so I'm not sure that's the case. Also, I'm using SendGrid but I already tried turning off SendGrid and that didn't solve the issue. Other than that, it's just using the wp_mail default function, which I believe uses PHP Mailer.
I'm a junior developer so I'm sure I'm just overlooking something, but I'm stumped at this point. Any help would be great, thanks.
If $testemail is not being incremented, it's possible that something in WP is running this function twice, separately, and therefore $testemail as a local variable will not increment on the second run.
Here are a few possible workarounds:
1) You could consider making $testemail a global variable.
2) When the email is successfully sent, store that fact in a database. Prior to sending the email, check the database see if the email has been sent. If so, do not send the email again.
3) Look deeper into why WP is calling this function twice.

check_email_exists.php solution?

I am trying to check email which is actually existed or not via php script and I found a solution that can really solve it.
I came across from this article https://gist.github.com/sureshdsk/9c599d757e90b0215e55 .
Please check complete code there, and let me show only my problem pane.
//(Lower-most lines of the project)
$email="asadbksdhskhdksjfhk#gmail.com"; //email to test
$check =verifyEmail($email, 'youremail#gmail.com'); //your email is just used for smtp requests
if($check=="valid"){
echo "success";
}else{
echo "fail";
}
Everything is OK by changing target email and host email, it shows "success".
But I want this php script to be processed by sending data and don't want it to be static.
So I changed ..
$email="asadbksdhskhdksjfhk#gmail.com"; //email to test
to
$email=$_REQUEST['email'];
and try sending email as data like this .
http://www.samplesample.com/check_email_exists.php?email="test#gmail.com"
But not working as the other scripts in my hosting do. By echoing email, it shows blank(null).
Please kindly suggest for my problem. Thank you for reading and thinking about it.I am new to web programming,php .
Use FILTER_VALIDATE_EMAIL to validate email in php.
Try this code,
//http://www.samplesample.com/check_email_exists.php?email=test#gmail.com
$email=$_REQUEST['email'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
}
There's a plug-in class recommended via another answer which checks if the Email Address is valid using SMTP, that should free up all the complicated parts of developing the logic to test it.

PHP form still sending after error

I am a PHP newb so please bear with me for this rather simplistic question.
I have a PHP form setup like so >>
<?php
if($_POST){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$comments = $_POST['comments'];
if($comments)
$error = "There was an error, please give us a call at ### ###-####.";
else{
if($name=="Name" || $email=="Email" || $message=="Message"){
$error = "All fields are required, please fill them out and try again.";
}else
$header = "From: $name <$email>";
$message = "Name: $name\n\nEmail: $email\n\nMessage: $message";
if(mail("email#domain.com", 'Form Submission', $message, $header))
$success = "Thanks for sending us your message, we'll get back to you shortly.";
else
$error = "There was an error, please give us a call at ### ###-####.";
}
if($error)
echo '<div class="msg error">'.$error.'</div>';
elseif($success)
echo '<div class="msg success">'.$success.'</div>';
}
?>
The basic idea is that the form has descriptive text pre-filled in each field but when you click on them they are cleared via Javascript. I want to prevent people from pressing send on the form without filling it out, hence the "if($name=="Name" || $email=="Email" || $message=="Message"){" bit. However while that message is working the form is still submitting. Why is this. Also please note that the "comments" field is in fact a honeypot. Thanks!
Because php is server-side. You need to look into javascript validation for what you want. To validate with php you HAVE to submit the form.
One tutorial but I recommend Jquery validation
"I want to prevent people from pressing send on the form without filling it out, hence the "if($name=="Name" || $email=="Email" || $message=="Message"){"
All you need to do is disable the submit button client side until the proper validation is met, then also validate server side. As #Iznogood said, that's why your gettin your error
Like lznogood said, PHP validates the form on the server, so you need to send the information to the server before PHP can process it.
If you want the form to be checked on the user side before sending it to the server you should use JavaScript to do that (jQuery is a great tool to use), and after checking it on the user side you can decide whether to send the form to the server or not.
Though this isn't an answer to your question, you might be interest in the new html5 feature placeholder. Read about it here. You can check to see which browsers it works in here (stupid internet explorer!). 5 years ago, I would put those "hints" as the value, which was a pain to validate. placeholder makes it sooooooo much easier. Your tag would look like:
<input type="text" name="email" placeholder="Enter Your Email Here" value="">
Note that value is empty. You can omit the value attribute, I left it in to show it's not needed here.
As far as an answer to your original question, everybody else is correct, javascript is the way to go.

How to send a message securely via html and php (post)

I use a php code on my server to send messages to my clients. The programming tool I use (Game Maker) allows me to send messages via php by executing a shell so that the link appears in a browser.
Example is here ...
with all the other stuff added. So in effect, the message I'm sending and all the stuff I'm sending are seen in the browser. I use the php get method. everything works perfectly now, except that it may not be secured. Someone suggested php post method, but when I replaced get in my php cod on my server to post, and pasted the same thing in the browser, my code didn't work. It's hard to explain, but here's the php code on my server:
<?php
// Some checks on $_SERVER['HTTP_X_REFERRER'] and similar headers
// might be in order
// The input form has an hidden field called email. Most spambot will
// fall for the trap and try filling it. And if ever their lord and master checks the bot logs,
// why not make him think we're morons that misspelled 'smtp'?
if (!isset($_GET['email']))
die("Missing recipient address");
if ('' != $_GET['email'])
{
// A bot, are you?
sleep(2);
die('DNS error: cannot resolve smpt.gmail.com');
// Yes, this IS security through obscurity, but it's only an added layer which comes almost for free.
}
$newline = $_GET['message'];
$newline = str_replace("[N]","\n","$newline");
$newline = str_replace("[n]","\n","$newline");
// Add some last-ditch info
$newline .= <<<DIAGNOSTIC_INFO
---
Mail sent from $_SERVER[REMOTE_ADDR]:$_SERVER[REMOTE_PORT]
DIAGNOSTIC_INFO;
mail('info#site.com','missing Password Report',$newline,"From: ".$_GET['from']);
header( 'Location: http://site.com/report.html' ) ;
?>
I then call this php code on my site. so that in the end, the whole thing ends up in the browser address bar. I hope this makes sense. How do I make things more secured by using post so that at least the sent information cannot be seen in users history and all that.
If you replace to POST in your form you need to replace the request to POST too:
<?php
// Some checks on $_SERVER['HTTP_X_REFERRER'] and similar headers
// might be in order
// The input form has an hidden field called email. Most spambot will
// fall for the trap and try filling it. And if ever their lord and master checks the bot logs,
// why not make him think we're morons that misspelled 'smtp'?
if (!isset($_POST['email']))
die("Missing recipient address");
if ('' != $_POST['email'])
{ // A bot, are you?
sleep(2);
die('DNS error: cannot resolve smpt.gmail.com');
// Yes, this IS security through obscurity, but it's only an added layer which comes almost for free.
}
$newline = $_POST['message'];
$newline = str_replace("[N]","\n","$newline");
$newline = str_replace("[n]","\n","$newline");
// Add some last-ditch info
$newline .= <<<DIAGNOSTIC_INFO
---
Mail sent from $_SERVER[REMOTE_ADDR]:$_SERVER[REMOTE_PORT]
DIAGNOSTIC_INFO;
mail('info#site.com','missing Password Report',$newline,"From: ".$_POST['from']);
header( 'Location: http://site.com/report.html' ) ;
?>
Unless you are sending it with real GET parameters like http://www.mysite.com/send.php?email=etc; in this case you do need to set it to GET to retrieve the variables.

Categories