Contact form with attachment

Hello,

I finally found a free mail form with attachment, but it doesn’t work for 100%

The hardest part (I think), sending the attachment works, but the form contains few errors, so the content isn’t visible when you recive the mail.

I don’t really know where in the body of the mail, i have to echo the actuall message(form is now exactly as i found it, just few minor changements like i edited the names of the vars, but that’s not the problem)

Can someone help me please?

Thanks!!!

<?
/* Mailer with Attachments */

$action = $_REQUEST['action'];
global $action;

function showForm() {
?>

  <form id="sendproject" enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
  <input type="hidden" name="action" value="send" />
    <label>Name:</label>
    <input class="text" name="name" size="50" />

    <label>Subject:</label>
    <input class="text" name="subject" size="50" />

    <label>Email:</label>
    <input class="text" name="email" size="50" />

    <label>Project:</label>
    <input class="text" name="project" size="50" />

    <label>Attachment:</label>
    <input class="text" type="file" name="attachment" size="50" />

    <label>Message:</label>
    <textarea name="body" rows="10" cols="50"></textarea>

    <input type="submit" name="submit" value="Submit" /></p>
    </form>

<?
}

function sendMail() {
$MAX_FILE_SIZE = "10000000";
  if (!isset ($_POST['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 {
    $name = stripslashes($_POST['name']);
    $subject = stripslashes($_POST['subject']);
    $body = stripslashes($_POST['body']);
    $email = $_POST['email'];
    $attachment = $_FILES['attachment']['tmp_name'];
    $attachment_name = $_FILES['attachment']['name'];
    if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
      $fp = fopen($attachment, "rb"); //Open it
      $data = fread($fp, filesize($attachment)); //Read it
      $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
        fclose($fp);
    }
    //Let's start our headers
    $headers = "From: $from_name<" . $email . ">\
";
    $headers .= "Reply-To: <" . $email . ">\
";
    $headers .= "MIME-Version: 1.0\
";
    $headers .= "Content-Type: multipart/related; type=\\"multipart/alternative\\"; boundary=\\"----=MIME_BOUNDRY_main_message\\"\
";
    $headers .= "X-Sender: $from_name<" . $_POST['from_email'] . ">\
";
    $headers .= "X-Mailer: PHP4\
";
    $headers .= "X-Priority: 3\
"; //1 = Urgent, 3 = Normal
    $headers .= "Return-Path: <" . $_POST['from_email'] . ">\
";
    $headers .= "This is a multi-part message in MIME format.\
";
    $headers .= "------=MIME_BOUNDRY_main_message \
";
    $headers .= "Content-Type: multipart/alternative; boundary=\\"----=MIME_BOUNDRY_message_parts\\"\
";

    $message = "------=MIME_BOUNDRY_message_parts\
";
    $message .= "Content-Type: text/plain; charset=\\"iso-8859-1\\"\
";
    $message .= "Content-Transfer-Encoding: quoted-printable\
";
    $message .= "\
";
    /* Add our message, in this case it's plain text.  You could also add HTML by changing the Content-Type to text/html */
    $message .= " Het geschreven bericht: \
<br />".$body;
    $message .= "\
";
    $message .= "------=MIME_BOUNDRY_message_parts--\
";
    $message .= "\
";
    $message .= "------=MIME_BOUNDRY_main_message\
";
    $message .= "Content-Type: application/octet-stream;\
\	name=\\"" . $attachment_name . "\\"\
";
    $message .= "Content-Transfer-Encoding: base64\
";
    $message .= "Content-Disposition: attachment;\
\	filename=\\"" . $attachment_name . "\\"\
\
";
    $message .= $data; //The base64 encoded message
    $message .= "\
";
    $message .= "------=MIME_BOUNDRY_main_message--\
";

    // send the message
    mail("altin.t@gmail.com", $subject, $message, $headers);
  }
}

switch ($action) {
  case "send":
    sendMail();
    showForm();
    break;
  default:
    showForm();
}

?> 

No one that knows where i should echo the actuall message then?

Just use phpmailer class to send your messages. It makes this task as simple as possible.

Ok well I donwnloaded tha class, but I really don’t understand how to add a contact form to it.
I’d like to send the mails with the php mail function (i think this is the default option on the script) but I don’t see how to use the class.

I supose I ned to include it in the html form to start with, but then what?

Can you please help me a bit further?

Thanks!

You can create 2 files - one will contain for and another one will have the form processing script.

Here you can find a tutorial http://phpmailer.sourceforge.net/tutorial.html

Ah ok thanks.

Also can you tell me some more about the installation?

This is what i should do:

Copy class.phpmailer.php into your php.ini include_path. If you are
using the SMTP mailer then place class.smtp.php in your path as well.

Now, I’m with dreamhost, and I don’t think I have access to my php.ini file…

Thanks!

You can just copy both files to your folder or sub-folder and just include it like all other includes.

Yep that worked, thanks!

I think this is the last question;
I understand how to link this with a form etc, but not the file attachment.
This is the php code: AddAttachment($path);

But it just asks for the path.
Will I need another script to upload it first, and then get the path or does this class support the uploading too?

The html would look something like this:
<input class=“text” type=“file” name=“attachment” size=“50” />
Or do i only need to do something like $path = $_POST[‘attachment’]; ?

Thanks alot for helping me out!

First of all you should define encoding type for the form:


<form action="file-upload.php" method="post" enctype="multipart/form-data">

Than you can use the file path and name from the $_FILES variable:


$_FILES['userfile']['tmp_name'];
$_FILES['userfile']['name'];

<form action="file-upload.php" method="post" enctype="multipart/form-data">
<input class="text" type="file" name="attachment" size="50" />
</form>

$path = $_FILES['userfile']['tmp_name'];
$name = $_FILES['userfile']['name'];
AddAttachment($path,$name,$encoding,$type);

Something like this?

Thanks alot!

Yes, something like that. Just not sure that $encoding,$type are required.

Hey,
thanks again for replying, but it still doesn’t work.

This is the code I have now:

<?php
require("class.phpmailer.php");
if($_POST['submit']){
	$mail = new PHPMailer();
	$mail->IsMail(); // telling the class to use SMTP
	$mail->From = "test@testing.com";
	$mail->AddAddress("altin.t@gmail.com", "Altin Toooola");
	
	$mail->Subject = "testing phpmailer thing";
	$mail->Body = "Hi! \
\
 This is my first e-mail sent through PHPMailer.";
	$mail->WordWrap = 50;
	
	
	$path = $_FILES['userfile']['tmp_name'];
	$name = $_FILES['userfile']['name'];
	$encoding = "base64";
	$type = "image/gif";
	$mail->AddStringAttachment($string,$filename,$encoding,$type);
	
	
	
	if(!$mail->Send())
	{
	   echo 'Message was not sent.';
	   echo 'Mailer error: ' . $mail->ErrorInfo;
	}
	else
	{
	   echo 'Message has been sent.';
	}
}
else{
?>

<form action="test.php" method="post" enctype="multipart/form-data">
<input class="text" type="file" name="attachment" size="50" />
<input type="submit" name="submit" value="Submit" class="form-button" />
</form>

<?
}
?>

I tryed it with the 3 different possibilitys:
AddAttachment($path);
AddAttachment($path, $name);
AddStringAttachment($string,$filename,$encoding,$type);

With the 2 first ones, I get the message that the mail is sent, but I can’t see an attachment.

With the last one, I sort of get an attachment:
I see there’s an attachment, but I don’t see a preview, it’s called “noname” and when i try to see it, it doesn’t load neether.

Any idea?

THanks again!

Here is the code that is working fine on my hosting:


$mail = new PHPMailer();
			
$mail->From     = $frm['Email'];
$mail->FromName = "{$frm['FName']} {$frm['LName']}";
			
$mail->Subject = "Contact Form - {$frm['Subject']}";
			
$text = "\
"; $html = "<table>";
foreach($_POST as $v=>$l){
	$text .= "{$v}:  {$l}\
\
";
	$html .= "<tr><td>{$v}:</td><td>  {$l}</td></tr>";
}
$html .= "</table>";

$mail->Body    = $html;
$mail->AltBody = $text;
$mail->AddAddress($CFG['AppDef']['Mailer'], "Site Admin");
$mail->AddAttachment($_FILES['attach']['tmp_name'], $_FILES['attach']['name']);
			
if(!$mail->Send())
    echo "There has been a mail error sending to Admin<br>";
			
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
$mail->ClearAttachments();

Could you also post the html form with that please?

I’m not really good with php, and this way, I don’t see much in it.

Thanks!