Email Script

Discussion in 'PHP' started by Noddegamra, Aug 20, 2007.

  1. #1
    I have a php e-mail script where I wish to add an additional feature. What I want is for the user to be able to also send their CV via an attachment.

    Here is the example code:

    <?php
    echo '<h1>Contact Us</h1>';
    		
    		if(isset($_POST['email_page']))
    			{
    				$sender_name = $_POST['sender_name'];
    				$sender_email = $_POST['sender_email'];
    				$sender_comments = $_POST['sender_comments'];
    		
    				$to = 'test@example.com';
    				$subject = "TEST - Email Submit Form";
    				$body = "Hi,\n\n $sender_name ($sender_email) has submitted the following comment: \n\n $sender_comments";
    				
    				$headers = "From: $sender_email\r\n";
    				$headers .= "X-Mailer: php";
    				
    				if (@mail($to, $subject, $body, $headers))
    					{
    						# TEST INPUT
    						echo '
    								<table class="email_form">
    									<tr>
    										<td colspan="2"><h2><font color="#339933">Message Sent!</font></h2></td>
    									</tr>
    									<tr>
    										<td><b>Name: </b></td>
    										<td>',$sender_name,'</td>
    									</tr>
    									<tr>
    										<td><b>Email: </b></td>
    										<td>',$sender_email,'</td>
    									</tr>
    									<tr>
    										<td valign="top"><b>Comments: </b></td>
    										<td>',nl2br($sender_comments),'</td>
    									</tr>
    								</table>
    								<br />
    								<p>Thankyou for your comment.</p>
    								<p>Please <a href="index.php">click here</a> to return to the main page.</p>
    							';
    					}
    				else
    					{
    						echo '
    								<h2><font color="#CC0000">Message delivery failed...</font></h2>
    								<p>There is an error with this feature. Please <a href="mailto:test@example.com">contact the site admin</a>.</p>
    								<hr />
    								<p>To send an email to us manually, please <a href="mailto:',$to,'">click here.</a></p>
    								<p>We are sorry for any inconvenience caused.</p>
    							';
    					}
    				
    			}
    		else
    			{	
    				?>
    					
    					<p>Send us an e-mail!</p>
    					
    					
    					<table class="email_form">
    				    <form name="emailer" method="post" action="index.php?section=home&page=contact">
    				    	<tr>
    				    		<td><b>Name:</b></td>
    				    		<td align="right"><input type="text" name="sender_name" size="35" class="input" /></td>
    				    	</tr>
    				    	<tr>
    				    		<td><b>Email:</b></td>
    				    		<td align="right"><input type="text" name="sender_email" size="35" class="input" /></td>
    				    	</tr>
    				    	<tr>
    				    		<td valign="top"><b>Comments:</b></td>
    				    		<td align="right"><textarea name="sender_comments" rows="6" cols="37" class="input"></textarea></td>
    				    	</tr>
    				    	<tr>
    				    		<td><b>Attach CV:</b></td>
    				    		<td align="right"><input name="uploaded" type="file" /></td>
    				    	</tr>
    				    	<tr>
    				    		<td colspan="2" align="right"><input type="submit" name="email_page" value="Submit" title="Submit Contact Information" class="button" /></td>
    				    	</tr>
    				    </form>
    				    </table>
    				<?php
    			}
    ?>
    
    Code (markup):
    Can anyone help me modify this so that the user can also send a CV?

    I know this isnt a proper application form yet, its from a "contact us" form script I wrote a while back. It is also lacking validation.

    What I need is a CV addition with validation of it. I can work the rest myself (unless you are bored and want to do it all.. lol)

    Anyway. All help will be greatly appreciated. Thankyou :)
     
    Noddegamra, Aug 20, 2007 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You are not validating user input in your script, so it can be used for spamming!
     
    matthewrobertbell, Aug 20, 2007 IP
  3. wrboyce

    wrboyce Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you should probably sanitize those _POST variables!
     
    wrboyce, Aug 20, 2007 IP
  4. Noddegamra

    Noddegamra Peon

    Messages:
    1,013
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I appreciate your comments/advice. But if you read my post you will see I already said..


    :)
     
    Noddegamra, Aug 20, 2007 IP
  5. Noddegamra

    Noddegamra Peon

    Messages:
    1,013
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #5
    can anyone provide help on this? I've searched a lot on google etc, but I can't really grasp how to add the attachment.

    thanks :)
     
    Noddegamra, Aug 21, 2007 IP
  6. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #6
    matthewrobertbell, Aug 22, 2007 IP
  7. Noddegamra

    Noddegamra Peon

    Messages:
    1,013
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I've got this

    <?php
    	/* Mailer with Attachments */
    
    		$action = $_REQUEST['action'];
    		global $action;
    		
    		function showForm() {
    		?>
    		
    		  <form enctype="multipart/form-data" name="send" method="post" action="index.php?section=careers&page=application_form2">
    		  <input type="hidden" name="action" value="send" />
    		  <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /><br />
    		    <p>Recipient Name: <input name="to_name" size="50" /><br />
    		    Recipient Email: <input name="to_email" size="50" /><br />
    		    From Name:  <input name="from_name" size="50" /><br />
    		    From Email:  <input name="from_email" size="50" /><br />
    		    Subject:  <input name="subject" size="50" /><br />
    		    Message: <textarea name="body" rows="10" cols="50"></textarea><br />
    		    Attachment: <input type="file" name="attachment" size="50" /><br />
    		    <input type="submit" value="Send Email" /></p>
    		  
    		<?php
    		}
    		
    		function sendMail()
    {
        if (!isset ($_POST['to_email'])) { //Oops, forgot your email addy!
            die ("<p>Oops!  You forgot to fill out the email address! Click on the back arrow to go back</p>");
        } else {
            $to_name = stripslashes($_POST['to_name']);
            $from_name = stripslashes($_POST['from_name']);
            $subject = stripslashes($_POST['subject']);
            $body_text = stripslashes($_POST['body']);
            $to_email = $_POST['to_email'];
    
            //Let's start our headers
            $headers  = "From: $from_name <" . $_POST['from_email'] . ">\n";
            $headers .= "Reply-To: <" . $_POST['from_email'] . ">\n"; 
            $headers .= "MIME-Version: 1.0\n";
            $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n"; 
            $headers .= "X-Sender: $from_name <" . $_POST['from_email'] . ">\n";
            $headers .= "X-Mailer: PHP4\n";
            //
            //if (checkbox_form.checkbox[counter].checked)
            //Return-Receipt-To: x@x.com
            //X-Confirm-Reading-To: x@x.com
            //Disposition-Notification-To: x@x.com
            //
            $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
            $headers .= "Return-Path: <" . $_POST['from_email'] . ">\n"; 
            $headers .= "This is a multi-part message in MIME format.\n";
            $headers .= "------=MIME_BOUNDRY_main_message \n"; 
            $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n"; 
            $message  = "------=MIME_BOUNDRY_message_parts\n";
            $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; 
            $message .= "Content-Transfer-Encoding: quoted-printable\n"; 
            $message .= "\n"; 
            /* Add our message, in this case it's plain text.  You could also add HTML by changing the Content-Type to text/html */
            $message .= "Hi $to_name \n\n $body_text\n";
            $message .= "\n"; 
            $message .= "------=MIME_BOUNDRY_message_parts--\n"; 
            $message .= "\n"; 
    
            foreach($_FILES as $file => $value) {
                $_tmpname = $_FILES[$file]['tmp_name'];
                $_filename = $_FILES[$file]['name'];
                if (is_uploaded_file($_tmpname)) { //Do we have a file uploaded?
                    $fp = fopen($_tmpname, "rb"); //Open it
                    $data = fread($fp, filesize($_tmpname)); //Read it
                    $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
                    $message .= "------=MIME_BOUNDRY_main_message\n"; 
                    $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $_filename . "\"\n";
                    $message .= "Content-Transfer-Encoding: base64\n";
                    $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $_filename . "\"\n\n";
                    $message .= $data; //The base64 encoded message
                    $message .= "\n\n";
                    fclose($fp);
                }
            }
        
            $message .= "------=MIME_BOUNDRY_main_message--\n"; 
        
            // send the message
        
            $ok = mail("$to_name <$to_email>", $subject, $message, $headers); 
        
            if ($ok == 1) {
                print "Mail sent to $to_name ($to_email).";
            } else {
                print "Mail did not send.  Please press the <b>back</b> button and re-submit the e-mail.";
            }
        }
    }
    		
    		?>
    		
    		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    		    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    		<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    		  <head
    		    ****** http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    		    <style="css" type="text/css">
    		      <!--
    		      body {
    		        margin: 0px;
    		        font-family: Verdana, Arial, Helvetica, sans-serif;
    		        font-size: 12px;
    		      }
    		      a {color: #0000ff}
    		      -->
    		    </style>
    		  </head>
    		  <body>
    		
    		<?php
    		switch ($action) {
    		  case "send":
    		    sendMail();
    		    showForm();
    		    break;
    		  default:
    		    showForm();
    		}
    		?>
    		
    		  </body>
    		</html>
    PHP:

    it sends an attachment, but the actual body text doesnt appear. Anyone know what might be wrong?
     
    Noddegamra, Aug 23, 2007 IP