How do I add an attachment script to email?

Discussion in 'PHP' started by kutibah, Apr 13, 2006.

  1. #1
    Hello, I have set up a form where people can visit my site and type the "to, from, subject, and message" in a form to send an email. I have also setup a form where they can attach a file to their email.

    The current php that process this is:

    <?
    
       $headers = "MIME-Version: 1.0\r\n";
       $headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
       $headers.= "From: $from\r\n";
       if (mail($to, $subject, $message, $headers))
          echo "Message Sent!";
       else
          echo "Failed to send message.";
    
    ?>
    Code (markup):
    This works perfectly. But my question is, what do I need to add to this script to make it send the attached file as well? Thanks!
     
    kutibah, Apr 13, 2006 IP
  2. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
  3. kutibah

    kutibah Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am new to scripting and I am having trouble getting this working. Is there a simple script that I can just add to my script above? Because these new script is WAY different than what my form is created to do. Thanks.
     
    kutibah, Apr 13, 2006 IP
  4. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #4
    sorry, maybe you need to learn how to program better, or at least look on a site dedicated to programming, rather than giving me a negative reputation point simply because you don't understand
     
    frankcow, Apr 14, 2006 IP
  5. kutibah

    kutibah Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok well thanks for your help. And don't worry, I'm not going to give you a negative rep point.
     
    kutibah, Apr 14, 2006 IP
  6. kutibah

    kutibah Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Can anyone else explain this to a noob like me? I'm not that great with PHP...
     
    kutibah, Apr 15, 2006 IP
  7. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Here is the function I used

    First have the form to upload file, (I will leave this for now)
    
    
    function attach($AttmFile,$FileName)
    	{
    	global $ms,$head;
     	$Text=$ms;
    	$Html=$ms; 
    	if ($Text=="") {$Text=" ";$Html=" ";}
    
    	$OB="----=_OuterBoundary_000";
    	$IB="----=_InnerBoundery_001";
    	$Html=$Html?$Html:preg_replace("/\n/","{br}",$Text) 
    	 or die("neither text nor html part present.");
    	$Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail.";
    	$headers ="MIME-Version: 1.0\r\n";
    	$headers.=$head;
    	$headers.="X-Priority: 0\n";
    	$headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n";
    	$Msg ="This is a multi-part message in MIME format.\n";
    	$Msg.="\n--".$OB."\n";
    	$Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n";
    	$Msg.="\n--".$IB."\n";
    	$Msg.="Content-Type: text/plain;\n\tcharset=\"iso-8859-1\"\n";
    	$Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
    	$Msg.=$Text."\n";
    	$Msg.="\n--".$IB."--\n";
    	$patharray = explode ("/", $AttmFile);
    	$Msg.= "\n--".$OB."\n";
    	$Msg.="Content-Type: application/zip;\n\tname=\"".$FileName."\"\n";
    	$Msg.="Content-Transfer-Encoding: base64\n";
    	$Msg.="Content-Disposition: attachment;\n\tfilename=\"".$FileName."\"\n\n";
    		
    	$fd=fopen ($AttmFile, "r");
    	$FileContent=fread($fd,filesize($AttmFile));
    	fclose ($fd);
    	$FileContent=chunk_split(base64_encode($FileContent));
    	$Msg.=$FileContent;
    	$Msg.="\n\n";
    	$Msg.="\n--".$OB."--\n";
    	
    	$ms=$Msg;
    	$head=$headers;
    }
    
    PHP:
     
    onlyican.com, Apr 15, 2006 IP
  8. kutibah

    kutibah Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Here's my PHP Script that is supposed to Upload the file, attach it, and then send an email:

    <?
    $target_path = "uploads/";
    
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
    
     function attach($name)
        {
        global $ms,$head;
        $Text=$ms;
        $Html=$ms; 
        if ($Text=="") {$Text=" ";$Html=" ";}
     
        $OB="----=_OuterBoundary_000";
        $IB="----=_InnerBoundery_001";
        $Html=$Html?$Html:preg_replace("/\n/","{br}",$Text) 
         or die("neither text nor html part present.");
        $Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail.";
        $headers ="MIME-Version: 1.0\r\n";
        $headers.=$head;
        $headers.="X-Priority: 0\n";
        $headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n";
    	$headers.= "From: $from\r\n";
        $headers.= "Reply-To: $replyto\r\n";
    	
        $Msg ="This is a multi-part message in MIME format.\n";
        $Msg.="\n--".$OB."\n";
        $Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n";
        $Msg.="\n--".$IB."\n";
        $Msg.="Content-Type: text/plain;\n\tcharset=\"iso-8859-1\"\n";
        $Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
        $Msg.=$Text."\n";
        $Msg.="\n--".$IB."--\n";
        $patharray = explode ("/", $AttmFile);
        $Msg.= "\n--".$OB."\n";
        $Msg.="Content-Type: application/zip;\n\tname=\"".$FileName."\"\n";
        $Msg.="Content-Transfer-Encoding: base64\n";
        $Msg.="Content-Disposition: attachment;\n\tfilename=\"".$FileName."\"\n\n";
            
        $fd=fopen ($name, "r");
        $FileContent=fread($fd,filesize($name));
        fclose ($fd);
        $FileContent=chunk_split(base64_encode($FileContent));
        $Msg.=$FileContent;
        $Msg.="\n\n";
        $Msg.="\n--".$OB."--\n";
        
        $ms=$Msg;
        $head=$headers;
    	
    } 
       
        $headers ="MIME-Version: 1.0\r\n";
        $headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n";
    	$headers.= "From: $from\r\n";
        $headers.= "Reply-To: $replyto\r\n";
     
       if (mail($to, $subject, $message, $headers))
          echo "<h1>Message Sent!</h1>";
       else
          echo "<h1>Failed to send message.</h1>";
    ?>
    PHP:
    The problem is, it uploads the file fine, the email is sent fine, but the file is NOT being attached to the email. How do I configure it to attach the file?
     
    kutibah, Apr 15, 2006 IP