SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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!!!

    PHP Code:
    <?
    /* 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($fpfilesize($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 ">\n";
        
    $headers .= "Reply-To: <" $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";
        
    $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 .= " Het geschreven bericht: \n<br />".$body;
        
    $message .= "\n"
        
    $message .= "------=MIME_BOUNDRY_message_parts--\n"
        
    $message .= "\n"
        
    $message .= "------=MIME_BOUNDRY_main_message\n"
        
    $message .= "Content-Type: application/octet-stream;\n\tname=\"" $attachment_name "\"\n";
        
    $message .= "Content-Transfer-Encoding: base64\n";
        
    $message .= "Content-Disposition: attachment;\n\tfilename=\"" $attachment_name "\"\n\n";
        
    $message .= $data//The base64 encoded message
        
    $message .= "\n"
        
    $message .= "------=MIME_BOUNDRY_main_message--\n";

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

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

    ?>

  2. #2
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No one that knows where i should echo the actuall message then?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just use phpmailer class to send your messages. It makes this task as simple as possible.

  4. #4
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dzysyak View Post
    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!

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  6. #6
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dzysyak View Post
    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:
    Code:
    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!

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can just copy both files to your folder or sub-folder and just include it like all other includes.

  8. #8
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dzysyak View Post
    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!

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all you should define encoding type for the form:
    HTML Code:
    <form action="file-upload.php" method="post" enctype="multipart/form-data">
    Than you can use the file path and name from the $_FILES variable:
    PHP Code:
    $_FILES['userfile']['tmp_name'];
    $_FILES['userfile']['name']; 

  10. #10
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <form action="file-upload.php" method="post" enctype="multipart/form-data">
    <input class="text" type="file" name="attachment" size="50" />
    </form>
    PHP Code:
    $path $_FILES['userfile']['tmp_name'];
    $name $_FILES['userfile']['name'];
    AddAttachment($path,$name,$encoding,$type); 
    Something like this?

    Thanks alot!

  11. #11
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, something like that. Just not sure that $encoding,$type are required.

  12. #12
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,
    thanks again for replying, but it still doesn't work.

    This is the code I have now:
    PHP Code:
    <?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! \n\n 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!

  13. #13
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the code that is working fine on my hosting:

    PHP Code:
    $mail = new PHPMailer();
                    
    $mail->From     $frm['Email'];
    $mail->FromName "{$frm['FName']} {$frm['LName']}";
                    
    $mail->Subject "Contact Form - {$frm['Subject']}";
                    
    $text "\n"$html "<table>";
    foreach(
    $_POST as $v=>$l){
        
    $text .= "{$v}:  {$l}\n\n";
        
    $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(); 

  14. #14
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •