Need help with sending a contact form with an e-mail

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
pazzesci
Forum Newbie
Posts: 6
Joined: Fri Nov 09, 2007 9:17 am

Need help with sending a contact form with an e-mail

Post by pazzesci »

I am creating a send form with several fields: Name, Phone, E-mail, Topic, Comments and Send File. I have no problem sending the first five fields (they got received at the e-mail account without problems) but so far I couldn't figure out what is the problem with the Send File one. Could anyone find the problem? Here is the code:

Code: Select all

<?php
if($_POST['IsPost']==1)
	{
	send_mail5($_POST);
	}
?>

function send_mail5($data)
	{

			require_once ( CLASSES_PATH.'/phpmailer/class.phpmailer.php' );
			$mail = new PHPMailer();
			$mail->IsSMTP();                                   // send via SMTP
			$mail->Host     = "localhost"; // SMTP servers
			$mail->SMTPAuth = true;     // turn on SMTP authentication
			$mail->Username = "site@mysite.com";  // SMTP username
			$mail->Password = 'mypassword'; // SMTP password


		//ini_set( 'include_path', $_SERVER['DOCUMENT_ROOT'].'/candoit/smarty/libs/' );



		$mail->From     = 'site@mysite.com';
		$mail->FromName = 'Nuevos Pedidos de Trabajo';


		$to_address="rrhh@mysite.com";
		
		$to_name="Admin";
		$mail->AddAddress($to_address, $to_name);
	$mail->WordWrap = 50;  // set word wrap
	
	$mail->AddAttachment($_SERVER['DOCUMENT_ROOT']."/mail/".$data['archivo']);
	//echo WEB_PATH."main/".$data['mail'];exit();
	//$mail->AddAttachment($_SERVER['DOCUMENT_ROOT']."/documents/quotation".$data['id_quotation'].".pdf");
	
	$mail->IsHTML(true);                               // send as HTML
	$subject=$data['subject'];
	$message = '
		<html>
		<head>
		</head>
		<body>
		  <table>
			<tr>
			  <td>Name:</td><td>'.$data['name'].'</td>
			</tr>
			<tr>
			  <td>Phone:</td><td>'.$data['phone'].'</td>
			</tr>
			<tr>
			  <td>E-mail:</td><td>'.$data['email'].'</td>
			</tr>
			<tr>
			  <td>Comments:</td><td>'.$data['coments'].'</td>
			</tr>
		  </table>
		</body>
		</html>
		';
	$mail->Subject  =  $subject;
	$mail->Body     =  $message;
	//$mail->AltBody  =  $text_message;
	//echo $mail->Subject;
	//echo $mail->Body;exit();
	if(!$mail->Send())
	{
	   echo "Message was not sent <p>";
	   echo "Mailer Error: " . $mail->ErrorInfo;
	}
}

function AddAttachment($path, $name = "", $encoding = "base64", 
                           $type = "application/octet-stream") {
        if(!@is_file($path))
        {
            $this->SetError($this->Lang("file_access") . $path);
            return false;
        }

        $filename = basename($path);
        if($name == "")
            $name = $filename;

        $cur = count($this->attachment);
        $this->attachment[$cur][0] = $path;
        $this->attachment[$cur][1] = $filename;
        $this->attachment[$cur][2] = $name;
        $this->attachment[$cur][3] = $encoding;
        $this->attachment[$cur][4] = $type;
        $this->attachment[$cur][5] = false; // isStringAttachment
        $this->attachment[$cur][6] = "attachment";
        $this->attachment[$cur][7] = 0;

        return true;
    }


<form action="" method="post" enctype="multipart/form-data" name="contact" style="padding-top:10px;">
				<input type="hidden" name="IsPost" value="1" />
					<table width="100%" cellpadding="0" cellspacing="0">
						<tr>
							<td>*Nombre y Apellidos</td><td style="padding-top:5px;"><input type="text" name="name" size="30"></td>
						</tr>
						<tr>
							<td>*Numero de Telefono</td><td style="padding-top:5px;"><input type="text" name="phone" size="30"></td>
						</tr>
						<tr>
							<td>E-mail</td><td style="padding-top:5px;"><input type="text" name="email" size="30"></td>
						</tr>
						<tr>
							<td>Asunto</td><td style="padding-top:5px;"><input type="text" name="subject" size="30"></td>
						</tr>
						<tr>
							<td>Comentarios</td><td style="padding-top:5px;"><textarea name="coments" rows="10" cols="25" style="width:200px;"></textarea></td>
						</tr>
                        <tr>
						  <td>Send File</td>
						  <td style="padding-top:5px;"><input type="file" name="archivo" id="archivo" /></td>
                      </tr>
						<tr>
							<td colspan="2" style="padding-left:30px;"><input type="button" class="go-btn" onclick="document.contact.submit();" ></td>
						</tr>
                        

					</table>
				</form>
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

The uploaded file isn't in the $_POST array. $_FILES global array contains all the uploaded file info.Check the php manual for how to handle it and then attach it.
pazzesci
Forum Newbie
Posts: 6
Joined: Fri Nov 09, 2007 9:17 am

Post by pazzesci »

i checked the php manual but I still cannot figure put how to implement the $_FILES global array into the existing code. I will really appreciate it if someone can help me out with this :)))
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Make a test case on your local server. Basically take the examples in the manual and use them to make a simple uploader. But instead of actually uploading anything, user var_dump() or print_r() to see what the server is seeing. That will help out a ton.
Post Reply