PHP email is sending twice when form is submitted - php

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.

Related

membership database - I need a clickable email link put into a table's cell

$result = mysql_query("SELECT * FROM pm where idno ='$idno'", $db);
$datas = mysql_fetch_array($result);
<table><tr><td>
<? if ($userLevel > 9){
echo "<p><strong>$datas[email]</strong></p>";
echo "<p> </p>";
echo "<p><strong>Dear $datas[name]</strong></p>";
echo "<p>Here is the information you requested.</p>";
echo "<p><strong>Username: </strong> $datas[username]</p>";
echo "<p><strong>Password: </strong> $datas[password]</p>";
echo "<p> </p>";
echo "<p>These are case sensitive.</p>";
echo "<p> </p>";
echo "<p>Thank you</p>";
echo "Email: admin#domain.com</p>";
}else{echo " ";}?>
</td></tr>
<tr>ADMIN, COPY AND PASTE THE ABOVE AS A NEW EMAIL TO THIS MEMBER. CLICKING ON THE MEMBER EMAIL LINK ABOVE WILL OPEN YOUR EMAIL PROGRAM.</tr></table>
If you paste the above into a blank website page, you'll see that the first echo line is not clickable. How do I edit it to be clickable?
p.s. This is strictly to be text in a table cell with a clickable email link at the beginning, so I'm not looking for an emailing form. One more thing, I will not know what you're talking about unless you actually give me the code to copy and paste. I've had this old account since 2013 when someone left who did this, but I don't know a thing about php coding nor the lingo, just enough to get by and keep it alive.
Thanks so much
Tina
p.s. This has been edited due to some answers seem to be working with the admin#domain.com echos in the last paragraph of the cell, which works fine, instead of the very first sentence in the table cell. So I must not have been real clear and delivered too much information prior to this edit. I also added the "array"(???), I guess it's called, from part of what is above the doctype in case it helps. I don't want that edited at all because the page is 1,330 lines long that feed off of that. DATAS are the PM (private members that has a leadership section within it, AND because we are having smtp problems with being on an old php4 version on an old insecure plesk server, I want to add this copy and paste paragraph for an email since our host is only sending all of our automated forms to our domain related email addresses and no members are getting their emails until our host fixes the problem.)
I figured it out to be the following. Thank you very much to getting my mind focused on the closing email address and trying to do something with that for the beginning email address. This is what worked for me:
echo "$datas[email]</p>";
You didn't mention any specific errors you're seeing, but the main problem you seem to have is the lack of quote marks around the email index value. It should be a string, I assume:
<?php echo $datas["email"]; ?>
And also putting quote marks around $datas makes no sense, because that isn't a string, it's a variable.
Demo: http://sandbox.onlinephpfunctions.com/code/4b3ecac7f441a1fa74451da6c795d918bad5f58e
Try this it'll work
echo "Email: <a href='mailto:admin#domain.com'>admin#domain.com</a></p>"; OR echo "Email: <a href='".$data['email']."'>admin#domain.com</a></p>";

PHPMailer sends email to different address and different body at the click of submit button

$autoemail = new PHPMailer\PHPMailer\PHPMailer();
$autoemail->IsSMTP(); // send via SMTP
$autoemail->CharSet="utf-8";
$autoemail->WordWrap = 50; // set word wrap
$autoemail->IsHTML(true); // send as HTML
$autoemail->AddAddress("example#gmail.com");
$autoemail->AddReplyTo("example#gmail","EXAMPLE");
$autoemail->WordWrap = 50;
$autoemail->Subject="Hello";
$autoemail->Body="Testing only";
if(!$autoemail->Send())
{
echo "Message was not sent";
echo "Mailer Error: " . $autoemail->ErrorInfo;
exit;
}
$autoemail->ClearAllRecipients();
$autoemail->AddAddress("pleasehelp#gmail.com", "EXAMPLE");
$autoemail->Body="Testing Only";
if(!$autoemail->Send())
{
echo "Message was not sent";
echo "Mailer Error: " . $autoemail->ErrorInfo;
exit;
}
I want to send email to two different email addresses, but this coding gives me error that it sends the email twice to both addresses and after sending the email, my web page turns to white page saying it has error ERR_INCOMPLETE_CHUNKED_ENCODING. Please help me on this! Thank you!
Your code looks ok, but you probably have a browser plugin or something causing multiple submissions. If you stick a random number on the end of your subject line, I expect you will find they will be different for each set of messages, indicating the script is running more than once, like this:
$autoemail->Subject="Hello".rand();
Looking at your access log may also reveal multiple requests.
The chunked encoding error doesn't appear to be anything to do with this script.

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.

How to send a message to my e-mail programmatically

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");

Email with PHP after conditional statement

So, I'm trying to do a very simple thing - send an email using PHP. I've looked through other queries on stack and none of them involve a conditional statement, so I wanted to check and see if I could get some quick advice. See the conditional below that then send a confirmation / thank you email to someone who donated to my organization.
Could it be that I first have the code echoing / printing a statement and then running the mail() function?
if ((isset($_POST['submitted'])) && ($ack!="SUCCESS")) {
$_SESSION['reshash']=$resArray;
$location = "https://globalcitizenyear.org/wp-content/themes/deMar/APIError.php";
header("Location: $location");
} elseif ($ack =="SUCCESS") {
echo ("<h2>Thank You</h2><p>Thank you for your generous donation of $$amount. You will receive an email confirmation with an attached tax receipt.</p>");
$body = "Dear $firstName,
/n/nThank you ...
/n/nAs I travel the country, ...
/n/nPlease accept my deepest gratitude for your contribution.
/n/nSincerely,
/n/nAbigail Falik
/n/nFounder and CEO
/nGlobal Citizen Year";
**$body = wordwrap($body,70);
mail("$email",'Thank you for your donation to Global Citizen Year (Important tax receipt)', $body,"From:donations#globalcitizenyear.org");**
}
else {
// Display Form ?>
Your code looks ok. It's best to send the mail first and check whether the function was successful. Then you can echo out "You will receive an email confirmation..." if successful, and some other message if the mail() call failed. With the mail() function, though, problems are usually later in the mail process. Getting a 'true' return from that function doesn't mean that Everything has worked in the email world.
With other functions, like DB writes, you will get a solid success or failure returned from the function and should act on it appropriately. That means you want to run the function before printing out a message saying everything went fine.

Categories