Send Email After Survey Completed?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
1234567890
Forum Newbie
Posts: 6
Joined: Mon Oct 06, 2008 7:21 pm

Send Email After Survey Completed?

Post by 1234567890 »

I understand you use the mail() to send an email using php, but I need to send an email in a different way. I have a survey that people need to complete, and after they submit the survey an email needs to be sent to them, so each time there will be a new email. Any advice on how I can do this? Below you will see my code:

Code: Select all

<?php
$con = mysql_connect("server","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("my_db", $con);
 
$sql="INSERT INTO Questions (Name, Email, Age, Sex, Income, Edu, Children, Apparel)
VALUES
('$_POST[name]','$_POST[email]','$_POST[age]','$_POST[sex]','$_POST[income]','$_POST[edu]','$_POST[children]','$_POST[apparel]')";
 
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Your information has been submitted. Thank you for your time. An email will be sent to the one provided shortly.";
 
mysql_close($con)
?>
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Send Email After Survey Completed?

Post by aceconcepts »

What you'll need to do is set some variables e.g. $firstName, $email etc... to be used dynamically in a seperate php file that will be used to send the email. When i say "dynamically", this basically means that those variables will change according to each form submit.

In terms of actually sending the email, what are you thinking, plain text or html?
1234567890
Forum Newbie
Posts: 6
Joined: Mon Oct 06, 2008 7:21 pm

Re: Send Email After Survey Completed?

Post by 1234567890 »

This should work, right?

Code: Select all

<?php
$con = mysql_connect("server","user","pass");
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }
 
mysql_select_db("my_db", $con);
 
$sql="INSERT INTO Questions (Name, Email, Age, Sex, Income, Edu, Children, Apparel)
VALUES
('$_POST[name]','$_POST[email]','$_POST[age]','$_POST[sex]','$_POST[income]','$_POST[edu]','$_POST[children]','$_POST[apparel]')";
 
if (!mysql_query($sql,$con))
 {
 die('Error: ' . mysql_error());
 }
echo "Your information has been submitted. Thank you for your time. An email will be sent to the one provided shortly.";
 
$subject = 'PUT YOUR SUBJECT HERE';
$message = 'YOU CAN PUT YOUR MESSAGE HERE';
 
$header = "From: YOUREMAILADDRESSHERE\r\n";
 
mail($_POST['email'],$subject,$message,$header);
 
mysql_close($con)
?>
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Send Email After Survey Completed?

Post by aceconcepts »

It's pretty crude but maybe it'll work. The problem with automated email sent from mail() is that they don't always get through.

I'll send you a mail() procedure i have used which seems to work pretty well.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Send Email After Survey Completed?

Post by aceconcepts »

Here is a script that will send out an email:

$emailMsg will be the body of the email.

Code: Select all

 
$to=$_POST['emailTo']
$from=$_POST['emailFrom'];
$subject=$_POST['emailSubject'];
$counter=0;
$notSent="";
        
    //SET HTML
        include"emailBody.php";
            
    //SET EMAIL VARIABLES
        $message="<html>";
        $message.= $emailMsg;
        $message.="</body></html>";
            
    //SET EMAIL HEADERS
        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
        $headers .= "Content-Transfer-Encoding: 7bit\r\n";
        $headers .= "From: " . $from . "\r\n";
            
    //SEND EMAIL
        if(mail($emailAddress,$subject,$message,$headers,"-f $from"))
        {
            $counter++;
            //MAIL SENT
            //$notSent[]=array($row['intAttendeeId']=>$emailAddress);
        }
        else
        {
            $counter-=1;
            //STORE ALL EMAILS AND IDS THAT WERE NOT SENT
                $notSent[]=array($row['intAttendeeId']=>$emailAddress);
        }
 
Post Reply