Jump to content

[SOLVED] Email form with attachments


petonii

Recommended Posts

Hello guys n gals,

 

Just wanted some help if possible. I have an online email form system on my website but would like people to send logos and such with there emails. I was wondering how would this be done? With my current code i want to add the user can attach up to 3 images, .jpgs and .gifs only.

 

Please help!

 


<?php

$contact_to_email="[email protected]";
$contact_subject="Contact Us";
$contact_from_email="[email protected]";
$contact_from_name="Woooooo";

$send_from_internal_address=false;
$error_color="red";
$use_security_image=false;

function previous_request_value($str)
{
if (isset($_REQUEST[$str]) )
return $_REQUEST[$str];
else
return '';
}

function cndstrips($str)
{
if (get_magic_quotes_gpc())
return stripslashes($str);
else
return $str;
}

$visitor_email=cndstrips(trim(previous_request_value('visitor_email')));
$visitor_name=cndstrips(trim(previous_request_value('visitor_name')));
$message_body=cndstrips(previous_request_value('message_body'));
$message_subject=cndstrips(previous_request_value('message_subject'));
$security_code=str_replace(' ','',cndstrips(trim(previous_request_value('security_code'))));

$errors="";
$message_sent=false;

function validate_email($email) {
return preg_match('/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]+\.[A-Za-z0-9_\-\.]+$/', $email) == 0;
}


if ($_SERVER['REQUEST_METHOD'] == 'POST')
{

if (validate_email($visitor_email) ) {
$errors.="Please enter a valid email address in the form of [email protected]<br/><br/>";
}

if ($use_security_image && $security_code != $_SESSION['contact_form_security_code'] ) {
$errors.="The verification code for the image presented was incorrect. Please enter a correct verification code.<br/><br/>";
}

if ($message_body == '')
$errors.="Please enter a message<br/><br/>";

if ($message_subject == '')
$errors.="Please enter a message subject<br/><br/>";

if ($visitor_name == '')
$errors.="Please enter your name<br/><br/>";

if ( !$errors ) {
$ip = $_SERVER["REMOTE_ADDR"];
$httpagent = $_SERVER["HTTP_USER_AGENT"];
$time = date("D, F j, Y H:i O");

if ($visitor_name)
$visitor_name_and_email="$visitor_name <$visitor_email>";
else
$visitor_name_and_email="$visitor_email";

if ($contact_from_name)
$contact_from_email="$contact_from_name <$contact_from_email>";

$message = "

$message_body

____________________________
Browser Info: $ip $httpagent
";

if ($send_from_internal_address) {
$message= "
From: $visitor_name_and_email
Date: $time
Subject: $message_subject
".$message;
}

if ($send_from_internal_address) {
mail($contact_to_email, $contact_subject." $message_subject", $message, "From: $contact_from_email\r\nReply-To: $visitor_name_and_email");
}
else {
mail($contact_to_email, $contact_subject." $message_subject", $message, "From: $visitor_name_and_email");
}

echo "Your message";
echo "<div style='border: 1px solid black; margin: 10px 10px 10px 10px; padding: 10px 10px 10px 10px;'>From: ".htmlentities($visitor_name_and_email)."<br />Re: ".htmlentities($message_subject)."<br />".htmlentities($message_body)."</div>";
echo "Has been sent. Thank you for contacting us.";
$message_sent=true;
}
}

if (!$message_sent) {
$this_file = substr(strrchr($_SERVER['PHP_SELF'], "/"), 1);

?>


Link to comment
https://forums.phpfreaks.com/topic/59480-solved-email-form-with-attachments/
Share on other sites

mail [ at ] wietse [ dot ] com

11-Jun-2007 03:34

<?php

function send_mail($MailTo = "", $SenderName = "Sender", $SenderMail = "[email protected]", $Subject = "", $Mailcontent = "no.file", $Attachment = "no.file", $Servername = "PHPMAILSERVER", $nohtml  = "[ This message should be viewed in HTML. This is a degraded version! ]"){
    if(strtoupper(substr(PHP_OS,0,3)=='WIN')){
      $eol="\r\n";
      $sol="\n";
    }elseif(strtoupper(substr(PHP_OS,0,3)=='MAC')){
      $eol="\r";
    }else{
      $eol="\n";
    }
    if(!isset($sol)){
        $sol = $eol;
    }
    $Momentn = mktime().".".md5(rand(1000,9999));
    $f_name       = $Attachment;
    $handle       = fopen($f_name, 'rb');
    $f_contents   = @fread($handle, filesize($f_name));
    $f_contents   = @base64_encode($f_contents);
    if($handle){
        $sendfile = true;
        if(ini_get('mime_magic.debug')){
            $Bestype = @mime_content_type($Attachment);   
        }else{
            $Bestype = 'application/octet-stream';
        }
        if(!$Bestype){
            $Bestype = 'application/octet-stream';
        }
        $file_realname = explode("/", $Attachment);
        $file_realname = $file_realname[count($file_realname)-1];
        $file_realname = explode("\\", $file_realname);
        $file_realname = $file_realname[count($file_realname)-1];
    }
    @fclose($handle);
    $Mailcontentstri  = explode($sol, $Mailcontent);
    $Mailcontentstrip = strip_tags($Mailcontentstri[0]);
   
    if(@file_exists($Mailcontentstrip)){
        ob_start();
        if(require($Mailcontent)){ 
            $body  = ob_get_contents();
        }
        ob_end_clean();
    }else{
        if(count($Mailcontentstri) < 2){
            $body  = "Error loading file!";
            $error = true;
        }else{
            $body  = $Mailcontent;   
        }
    }
               
    $Textmsg = eregi_replace("<br(.{0,2})>", $eol, $body);
    $Textmsg = eregi_replace("</p>", $eol, $Textmsg);
    $Textmsg = strip_tags($Textmsg);
    $Textmsg = $nohtml.$eol.$eol.$Textmsg;
    $headers      .= 'To: '.$MailTo.' <'.$MailTo.'>'.$eol;
    $headers      .= 'From: '.$SenderName.' <'.$SenderMail.'>'.$eol;
    $headers      .= "Message-ID: <".$Momentn."@".$Servername.">".$eol;
    $headers      .= 'Date: '.date("r").$eol;
    $headers      .= 'Sender-IP: '.$_SERVER["REMOTE_ADDR"].$eol;
    $headers      .= 'X-Mailser: iPublications Adv.PHP Mailer 1.6'.$eol;
    $headers      .= 'MIME-Version: 1.0'.$eol;
    $bndp          = md5(time()).rand(1000,9999);
    $headers      .= "Content-Type: multipart/mixed; $eol       boundary=\"".$bndp."\"".$eol.$eol;
    $msg           = "This is a multi-part message in MIME format.".$eol.$eol;
    $msg          .= "--".$bndp.$eol;
    $bnd           = md5(time()).rand(1000,9999);
    $msg          .= "Content-Type: multipart/alternative; $eol       boundary=\"".$bnd."\"".$eol.$eol;
    $msg          .= "--".$bnd.$eol;
    $msg          .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
    $msg          .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $msg          .= $Textmsg.$eol;
    $msg          .= "--".$bnd.$eol;
    $msg          .= "Content-Type: text/html; charset=iso-8859-1".$eol;
    $msg          .= "Content-Transfer-Encoding: 8-bit".$eol.$eol;
    $msg          .= $body.$eol;
    $msg          .= "--".$bnd."--".$eol.$eol;
    if(isset($sendfile)){
        $msg          .= "--".$bndp.$eol;
        $msg          .= "Content-Type: $Bestype; name=\"".$file_realname."\"".$eol;
        $msg          .= "Content-Transfer-Encoding: base64".$eol;
        $msg          .= "Content-Disposition: attachment;".$eol;
        $msg          .= "       filename=\"".$file_realname."\"".$eol.$eol;
        $f_contents    = chunk_split($f_contents);
        $msg          .= $f_contents.$eol;
    }
    $msg          .= "--".$bndp."--";
    if(!isset($error)){
        if(@mail($MailTo, $Subject, $msg, $headers)){
            return true;  
        }else{
            return false;
        }
    }else{
        return false;
    }
}

?>

 

http://us.php.net/manual/en/ref.mail.php#75663

TY for the link. This may sound silly, but how would i attach this is my button in HTML format?

I add in the button that lets you attach a file but don't know what to attach ID with.

 

please help

 

<?php

function send_mail($MailTo = "", $SenderName = "Sender", $SenderMail = "[email protected]", $Subject = "", $Mailcontent = "no.file", $Attachment = "no.file", $Servername = "PHPMAILSERVER", $nohtml  = "[ This message should be viewed in HTML. This is a degraded version! ]"){
    if(strtoupper(substr(PHP_OS,0,3)=='WIN')){
      $eol="\r\n";
      $sol="\n";
    }elseif(strtoupper(substr(PHP_OS,0,3)=='MAC')){
      $eol="\r";
    }else{
      $eol="\n";
    }
    if(!isset($sol)){
        $sol = $eol;
    }
    $Momentn = mktime().".".md5(rand(1000,9999));
    $f_name       = $Attachment;
    $handle       = fopen($f_name, 'rb');
    $f_contents   = @fread($handle, filesize($f_name));
    $f_contents   = @base64_encode($f_contents);
    if($handle){
        $sendfile = true;
        if(ini_get('mime_magic.debug')){
            $Bestype = @mime_content_type($Attachment);   
        }else{
            $Bestype = 'application/octet-stream';
        }
        if(!$Bestype){
            $Bestype = 'application/octet-stream';
        }
        $file_realname = explode("/", $Attachment);
        $file_realname = $file_realname[count($file_realname)-1];
        $file_realname = explode("\\", $file_realname);
        $file_realname = $file_realname[count($file_realname)-1];
    }
    @fclose($handle);
    $Mailcontentstri  = explode($sol, $Mailcontent);
    $Mailcontentstrip = strip_tags($Mailcontentstri[0]);
   
    if(@file_exists($Mailcontentstrip)){
        ob_start();
        if(require($Mailcontent)){ 
            $body  = ob_get_contents();
        }
        ob_end_clean();
    }else{
        if(count($Mailcontentstri) < 2){
            $body  = "Error loading file!";
            $error = true;
        }else{
            $body  = $Mailcontent;   
        }
    }
               
    $Textmsg = eregi_replace("<br(.{0,2})>", $eol, $body);
    $Textmsg = eregi_replace("</p>", $eol, $Textmsg);
    $Textmsg = strip_tags($Textmsg);
    $Textmsg = $nohtml.$eol.$eol.$Textmsg;
    $headers      .= 'To: '.$MailTo.' <'.$MailTo.'>'.$eol;
    $headers      .= 'From: '.$SenderName.' <'.$SenderMail.'>'.$eol;
    $headers      .= "Message-ID: <".$Momentn."@".$Servername.">".$eol;
    $headers      .= 'Date: '.date("r").$eol;
    $headers      .= 'Sender-IP: '.$_SERVER["REMOTE_ADDR"].$eol;
    $headers      .= 'X-Mailser: iPublications Adv.PHP Mailer 1.6'.$eol;
    $headers      .= 'MIME-Version: 1.0'.$eol;
    $bndp          = md5(time()).rand(1000,9999);
    $headers      .= "Content-Type: multipart/mixed; $eol       boundary=\"".$bndp."\"".$eol.$eol;
    $msg           = "This is a multi-part message in MIME format.".$eol.$eol;
    $msg          .= "--".$bndp.$eol;
    $bnd           = md5(time()).rand(1000,9999);
    $msg          .= "Content-Type: multipart/alternative; $eol       boundary=\"".$bnd."\"".$eol.$eol;
    $msg          .= "--".$bnd.$eol;
    $msg          .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
    $msg          .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $msg          .= $Textmsg.$eol;
    $msg          .= "--".$bnd.$eol;
    $msg          .= "Content-Type: text/html; charset=iso-8859-1".$eol;
    $msg          .= "Content-Transfer-Encoding: 8-bit".$eol.$eol;
    $msg          .= $body.$eol;
    $msg          .= "--".$bnd."--".$eol.$eol;
    if(isset($sendfile)){
        $msg          .= "--".$bndp.$eol;
        $msg          .= "Content-Type: $Bestype; name=\"".$file_realname."\"".$eol;
        $msg          .= "Content-Transfer-Encoding: base64".$eol;
        $msg          .= "Content-Disposition: attachment;".$eol;
        $msg          .= "       filename=\"".$file_realname."\"".$eol.$eol;
        $f_contents    = chunk_split($f_contents);
        $msg          .= $f_contents.$eol;
    }
    $msg          .= "--".$bndp."--";
    if(!isset($error)){
        if(@mail($MailTo, $Subject, $msg, $headers)){
            return true;  
        }else{
            return false;
        }
    }else{
        return false;
    }
}

?>

Ok i got a working version if anyone that doesn't know much PHP and would like a copy + paste version of send an attachment like pics from a website to an email address then here it is. save it as .php just copy and paste

 

<html>
<head>
<title>E-mail with Attachment</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
   if ($_SERVER['REQUEST_METHOD']=="POST"){

   // we'll begin by assigning the To address and message subject
   $to="[email protected]";

   $subject="E-mail with attachment";

   // get the sender's name and email address
   // we'll just plug them a variable to be used later
   $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";

   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

   // store the file information to variables for easier access
   $tmp_name = $_FILES['filename']['tmp_name'];
   $type = $_FILES['filename']['type'];
   $name = $_FILES['filename']['name'];
   $size = $_FILES['filename']['size'];

   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = "Here is your file: $name";

   // if the upload succeded, the file will exist
   if (file_exists($tmp_name)){

      // check to make sure that it is an uploaded file and not a system file
      if(is_uploaded_file($tmp_name)){

         // open the file for a binary read
         $file = fopen($tmp_name,'rb');

         // read the file content into a variable
         $data = fread($file,filesize($tmp_name));

         // close the file
         fclose($file);

         // now we encode it and split it into acceptable length lines
         $data = chunk_split(base64_encode($data));
     }

      // now we'll build the message headers
      $headers = "From: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " boundary=\"{$mime_boundary}\"";

      // next, we'll build the message body
      // note that we insert two dashes in front of the
      // MIME boundary when we use it
      $message = "This is a multi-part message in MIME format.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";

      // now we'll insert a boundary to indicate we're starting the attachment
      // we have to specify the content type, file name, and disposition as
      // an attachment, then add the file content and set another boundary to
      // indicate that the end of the file has been reached
      $message .= "--{$mime_boundary}\n" .
         "Content-Type: {$type};\n" .
         " name=\"{$name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";

      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
         echo "Message Sent";
      else
         echo "Failed to send";
   }
} else {
?>
<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
   enctype="multipart/form-data" name="form1">
   <p>From name: <input type="text" name="fromname"></p>
   <p>From e-mail: <input type="text" name="fromemail"></p>
   <p>File: <input type="file" name="filename"></p>
   <p><input type="submit" name="Submit" value="Submit"></p>
</form>
<?php } ?>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.