473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

mail() function sending twice

LacrosseB0ss
113 100+
Hey all!

I am coding a page in php to send an e-mail to 2 addresses (to and cc). The user fills out 2 text boxes on a form. The first will be the body of the message and the 2nd is return address. Everything is working however when the body text box is clicked, it sends a blank e-mail. Then when "submit" is clicked the real one is submitted.

Does anyone know how to prevent the first e-mail from sending? Or has anyone else encountered this problem? Thanks!

- LB
Oct 31 '07 #1
8 9273
Atli
5,058 Expert 4TB
Hi LB. Welcome to TSDN!

Thats a weird problem. Could you show us the HTML markup for the body text box?
Nov 1 '07 #2
brettl
41
Yeah that sounds really strange... I would love to see the code you are using.
Nov 1 '07 #3
LacrosseB0ss
113 100+
code is as follows:
HTML
Expand|Select|Wrap|Line Numbers
  1. <form action="../Prayer.php" method="post" name="frmPrayer" id="frmPrayer">
  2.       <div align="center"><strong>Enter your request in the box below and hit "send" to submit:</strong></div>
  3.     <center>
  4.         <textarea name="txtRequest" id="txtRequest" cols="100" rows="3"></textarea>
  5.         <br />
  6.       <br />
  7.       <div align="center"><strong>If you would like a reply, enter your e-mail or leave blank to remain anonymous:</strong></div>
  8.       <input name="txtReply" id="txtReply"  type="text" size="60">
  9.       <br />
  10.       <br />
  11.       <input name="cmdSendReq" type="submit" value="Submit Request" onClick="<?php echo(sendMail()) ?>">
  12.     </center>
  13. </form>
  14.  
and php:
[php]
<?php
function sendMail()
{
$to = "user@example.com";
$subject = "New Online Prayer Request";
$message = $_POST['txtRequest'];
$txtReply = $_POST['txtReply'];

$headers = "To: Bloor Central Prayer" . "\r\n";

if($txtReply == "") {
$headers .= "From: user@example.com" . "\r\n";
} else {
$headers .= "From: ";
$headers .= $_POST['txtReply'] . "\r\n";
}

$headers .= "Cc: webmaster@example.com";
/*temp test - check all e-mails make it, spam*/

$sent = mail($to, $subject, $message, $headers);

if($sent)
{
echo "Message Sent Successfully";
} else {
echo "There Was an Error Sending Your Message";
}
}
?>
[/php]

Thanks all!
Nov 2 '07 #4
LacrosseB0ss
113 100+
I'll add, as you can see by the comment, the CC e-mail is only a temporary thing while we get the site off the ground. I just want to ensure everything is going to the right places and that we're spam free and so on and so forth. I highly doubt this is where the problem is but I'm adding the comment anyway.

Also, the first e-mail is sent when EITHER text box gets focus.

-LB
Nov 2 '07 #5
Atli
5,058 Expert 4TB
Hi.

The problem is not with your textboxes. The problem is with the submit button's onClick event:
Expand|Select|Wrap|Line Numbers
  1. onClick="<?php echo(sendMail()) ?>"
  2.  
PHP functions can not be called from client-side code. PHP being a server side language, the PHP code in that line would be executed on the server before the form even reached the client.

The result of the function would be replaced in the form the client receives and a blank email sent. I'm sure, if you check out the source in your browser, that this line actually looks something like this:
Expand|Select|Wrap|Line Numbers
  1. onClick="Message Sent Successfully"
  2.  
Try removing the onClick event all together, it should solve your problem.
Nov 2 '07 #6
LacrosseB0ss
113 100+
I understand now. I have never used PHP before and your explanation makes sense. And you were bang on with the onClick prediction I just didn't know why it was doing that.

I followed your advice and it now doesn't send out multiple e-mails. In fact, it doesn't send any when the button is clicked. The sendMail() function isn't being called anymore. I'm lost again.

Is there an onSubmit() event that could work better? I'll continue to play with it. Thanks for the advice!

- LB
Nov 5 '07 #7
Atli
5,058 Expert 4TB
Hi.

Your mail is not getting sent at all now because your sendMail() function is never getting called. Sorry, should have caught this earlier.

The onClick event you removed earlier was not only responsible for sending the empty email, but the other one as well. Your form is posting its data to the same page it is on, effectively refreshing the page and sending along the form data.

Because your onClick event contained the sendMail() function, it was being sent ACCIDENTALLY in the wrong place.

Try either adding a call to the sendMail() function if the data is sent, or extracting the code from the function, calling it if the data has been submitted.

For example, try adding this after your sendMail() function:
Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['cmdSendReq'])) {
  2.   sendMail();
  3. }
  4.  
Nov 7 '07 #8
LacrosseB0ss
113 100+
For example, try adding this after your sendMail() function:
Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['cmdSendReq'])) {
  2.   sendMail();
  3. }
  4.  
THANKS! Works like a charm!! The question now is, why? What does the "isset" function?

Thanks again for the help. It has cleared up the problem.
- LB
Nov 8 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
3
by: chuckdfoster | last post by:
When using this bit of code, it sends two emails most of the time. Sometimes it doesn't send two, but most of the time it does. I put the system time (Now) in the Subject and each email has a...
3
by: HoustonComputerGuy | last post by:
I am working on getting my web applications moved to .Net 2.0 and am having some problems with System.Net.Mail. I get the following error when sending the mail: System.Net.Mail.SmtpException was...
2
by: EdWhyatt | last post by:
Hi all, I hope there is someone out there who can help me out - it has to be something obvious. I am simulating mail traffic, and want to include multiple attachments to my mail. I have created...
0
by: Adam Honek | last post by:
I'm having this odd bahaviour with the new .net.mail SMTP sending code below. It either gives me an exception error or no error but the main form doesn't show making it seem as if it's not...
4
by: Brian Lorraine | last post by:
I tried out the new System.Net.Mail.SmtpClient feature of asp.net 2.0 and I love it, but there's just one problem. When it sends an email out, it sends to BOTH the TO and the FROM address. I have...
0
by: bambi | last post by:
I am facing issues while adding attachments in an E-mail via Mapisend. The code I use is below. If I try to send , I am getting error messgae in Mailmessage.12. Mailmessage.12 is expected as numeric,...
1
by: akuva | last post by:
I have to send more than 100s of email using a php code if i use mail function whether it will be sent to spam or inbox. If it is sent to spam which is the other way to send mass email from php. i...
10
by: Jesse Burns aka jburns131 | last post by:
Here's my code: $today = getdate(); $to = someone@somewhere.com; $subject = "New Application: ".$userdata." - ".$today."/".$today."/".$today; $message_header = "New Application:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.