Jump to content

Code to email form data


Guest

Recommended Posts

The attached code works perfect for me. This code displays data from my mysql database in a browser & allows me to edit it, then re-submit it to the mysql database. Again, all of this works great.

 

I was wondering if there was a way way for me to add some code to this existing code that would send me an email with the information in the form when the submit button is clicked? Nothing fancy....I'd just like to get an email based off of the location, for example if the location is changed from "Office1" to "Office2", I'd like the email to go to office2@mail.com. If "Office3" is entered, I'd like the form info to go to office3@mail.com.

 

Is this possible?

 

[attachment deleted by admin]

Link to comment
Share on other sites

have a look at the PHP manual http://php.net/manual/en/ . You can use the mail function to send the data you need. Here is an example from the manual:

 

// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

 

this should get you started

Link to comment
Share on other sites

okay, I've added lines 128 - 138 to my code. I'm going to try & stumble through this...After adding the code, the updated data in the form still goes to my mysql database but no email or errors...I checked my php.ini file & it is set correctly to my SMTP server. I can PING the SMTP server & there are no firewalls. I think the problem is with my code... can someone help? Thanks

 

[attachment deleted by admin]

Link to comment
Share on other sites

No, here is my actual code. Am I putting it in the correct place? The code for the mail is located between lines 135 - 162.

 

I hope someone can help me. Thanks

 

[attachment deleted by admin]

Link to comment
Share on other sites

Ahh....I am not getting "An email has been sent to the supervisor for review" after I submit it. Any clues why? I also changed the sSubject but that didn't fix anything. Wonder why I'm not getting the "An email has been sent to the supervisor for review"

Link to comment
Share on other sites

you are trying to mail twice, once before you declare your headers. from line 145 change:

	mail($sEmailAddress, $sSubject, $sMessage, $sHeaders);

	$sMessage = "<p>Someone has edited the information below.</p><br> Location : {$_REQUEST['location']}<br>First Name : {$_REQUEST['fname']}<br>Last Name : {$_REQUEST['lname']}";
	$sHeaders  = "MIME-Version: 1.0\n"; 
	$sHeaders .= "Content-type: text/html; charset=iso-8859-1\n"; 
	$subject  = "Please Review";
	$sHeaders .= "To: Supervisor <$sEmailAddress>\n"; 
	$sHeaders .= "From: The Database <sender email>\n"; 

	if (mail($sEmailAddress, $sSubject, $sMessage, $sHeaders)) {

	echo "<center>An email has been sent to the supervisor for review"; 

	} else { 

	echo "This system is not working properly. Please contact a tech."; 

	} 

 

To:

	$sMessage = "<p>Someone has edited the information below.</p><br> Location : {$_REQUEST['location']}<br>First Name : {$_REQUEST['fname']}<br>Last Name : {$_REQUEST['lname']}";
	$sHeaders  = "MIME-Version: 1.0\n"; 
	$sHeaders .= "Content-type: text/html; charset=iso-8859-1\n"; 
	$subject  = "Please Review";
	$sHeaders .= "To: Supervisor <$sEmailAddress>\n"; 
	$sHeaders .= "From: The Database <sender email>\n"; 
        $sendMail = mail($sEmailAddress, $sSubject, $sMessage, $sHeaders);
	if (sendMail) {

	echo "<center>An email has been sent to the supervisor for review"; 

	} else { 

	echo "This system is not working properly. Please contact a tech."; 

	} 

 

also echo out

$sMessage

and 

$sHeaders

to make sure you havent got anything escaping the string. If you are still getting no confirmation message  then this part of the loop is not being entered on the page and you will need to do some fault finding to find where the script is stopping.

Link to comment
Share on other sites

Okay, I did as you suggested but still no luck...if I add

 

ini_set('display_errors',1);

error_reporting(E_ALL);

 

will that help me determine where it is stopping?

Link to comment
Share on other sites

It might not be throwing an error. It might be a logic problem where your code will never hit that loop. One way to test is to manually kill the script with the

die('I am here');

 

place that firstly before you call the mail part. If you see 'I am here' then if loop has been entered. Then place it after the mail function and see if it is called again. Im sure you get the picture

Link to comment
Share on other sites

I can put the code like you see in the attached & get several errors but I do see the confirmation message in the script. The errors are:

 

Undefined index: location in C:\wamp\www\flow\query\edit_dqa.php

Undefined index: fname in C:\wamp\www\flow\query\edit_dqa.php

Undefined index: lname in C:\wamp\www\flow\query\edit_dqa.php

Undefined variable: EmailAddress in C:\wamp\www\flow\query\edit_dqa.php

Warning: mail() [function.mail]: SMTP server response: 503 Issue RCPT TO: command before DATA command in C:\wamp\www\flow\query\edit_dqa.php

Notice: Use of undefined constant sendMail - assumed 'sendMail' in C:\wamp\www\flow\query\edit_dqa.php

 

[attachment deleted by admin]

Link to comment
Share on other sites

Hi, sorry, I never had time to look over all of the code before. You have the email function in the wrong part of your script, all of the email code you have written needs to go directly after your update query and before the

$URL =[\code] line. The errors you are getting is because it is sitting on a part of the script that hasn't been posted yet. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.