Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:32:34+00:00 2026-06-02T16:32:34+00:00

i am using following code, to send a mail, with two attachments, Problems i’m

  • 0

i am using following code, to send a mail, with two attachments,

Problems i’m having is, it takes only one attachment, i want to send to attachment in mail, and i’m not getting autoresponse,

can any one help me please, here is my code

<?php session_start();

$redirect_url = '../thanks.html';
$your_email ='name@domain.com';// <<=== update to your email address

$attachment_enabled = 1;
$autoresponder_enabled = 1;


    $name = $_POST['name'];
    $visitor_email = $_POST['email'];




function get_form_data(){

    global $REQUEST_METHOD;

    global $_POST;

    global $_GET;



    $vars = ($REQUEST_METHOD == 'GET') ? $_GET : $_POST;

    //strip spaces from all fields

    foreach ($vars as $k=>$v) $vars[$k] = trim($v);

    return $vars;

}


function _build_fields($vars){

    $skip_fields = array(

        'name', 

        'email', 

         'subject',

        'submitbtn');

    // order by numeric begin, if it exists

    $is_ordered = 0;

    foreach ($vars as $k=>$v) 

        if (in_array($k, $skip_fields)) unset($vars[$k]);



    $new_vars = array();

    foreach ($vars as $k=>$v){

        // remove _num, _reqnum, _req from end of field names

        $k = preg_replace('/_(req|num|reqnum)$/', '', $k);

        // check if the fields is ordered

        //if (preg_match('/^\d+[ \:_-]/', $k)) $is_ordered++;

        //remove number from begin of fields

        $k = preg_replace('/^\d+[ \:_-]/', '', $k);

        $new_vars[$k] = $v;

    }

    $vars = $new_vars;


    $max_length = 10; // max length of key field 

    foreach ($vars as $k=>$v) {

        $klen = strlen($k);

        if (($klen > $max_length) && ($klen < 40))

            $max_length = $klen;

    }



    if ($is_ordered)

        ksort($vars);



    // make output text

    $out = "";

    foreach ($vars as $k=>$v){

        $k = str_replace('_', ' ', $k);

        $k = ucfirst($k);

        $len_diff = $max_length - strlen($k);

        if ($len_diff > 0) 

            $fill = str_repeat('.', $len_diff);

        else 

            $fill = '';

        $out .= $k."$fill...: $v\n\n";

    }

    return $out;

}


$vars=get_form_data();
$out=_build_fields($vars);
$body = "A user  $name submitted the form:\n\n".$out;
//Auto Responser Function To Send Auto Respond



$autoresponder_from = $your_email;

$subject = "Page Edit Request";

$autoresponder_subject = "%subject% (autoresponse)";

$autoresponder_message = <<<MSG

Hi %name%,



Thank you for submitting the form.

--

MSG;


function auto_respond($vars){

    global $autoresponder_from, $autoresponder_message, $autoresponder_subject;

    /// replace all vars in message

    $msg = $autoresponder_message;

    preg_match_all('/%(.+?)%/', $msg, $out);

    $s_vars = $out[1]; //field list to substitute

    foreach ($s_vars as $k)

        $msg = str_replace("%$k%", $vars[$k], $msg);

    /// replace all vars in subject

    $subj = $autoresponder_subject;

    preg_match_all('/%(.+?)%/', $subj, $out);

    $s_vars = $out[1]; //field list to substitute

    foreach ($s_vars as $k)

        $subj = str_replace("%$k%", $vars[$k], $subj);

    //

    $_send_to = "$vars[name] <".$vars[email_from].">";

    $_send_from = $autoresponder_from;

   mail($_send_to, $subj, $msg, "From: $_send_from");

}

if(empty($errors))
{
        //send the email
        $to = $your_email;
        $subject="Page Edit Request";
        $from = $visitor_email;
        $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

        //Attach File if Attachment is done
        $files = array(); //files (field names) to attach in mail
        if (count($_FILES) && $attachment_enabled)
        {
            $files = array_keys($_FILES);
        }
        /*print_r($files);
        exit;*/
        if (count($files)){

        foreach ($files as $file){

            echo "hello";
            $file_name     = $_FILES[$file]['name'];

            $file_type     = $_FILES[$file]['type'];

            $file_tmp_name = $_FILES[$file]['tmp_name'];

            $file_cnt = "";

    if($file_name!='')
    {   
            $f=@fopen($file_tmp_name, "rb");

            if (!$f) 

                continue;

            while($f && !feof($f))

                $file_cnt .= fread($f, 4096);

            fclose($f);

            if (!strlen($file_type)) $file_type="applicaton/octet-stream";

            if ($file_type == 'application/x-msdownload')

                $file_type = "applicaton/octet-stream";






            $date_time = date('Y-m-d H:i:s');
            $mime_delimiter = md5(time());
            $mail = <<<EOF

This is a MIME-encapsulated message



--$mime_delimiter

$body

--------------------

REMOTE IP : $REMOTE_ADDR

DATE/TIME : $date_time

EOF;
            $data= chunk_split(base64_encode($file_cnt));
            $mail .= "\n--$mime_delimiter\n";

            $mail.="Content-Type: {\"application/octet-stream\"};\n" . " name=\"$file_name\"\n"."Content-Disposition: attachment;\n" . " filename=\"$file_name\"\n"."Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
          //  $mail .= "Content-type: $file_type\n";

           // $mail .= "Content-Disposition: attachment; filename=\"$file_name\"\n";

           // $mail .= "Content-Transfer-Encoding: base64\n\n";

           // $mail .= chunk_split(base64_encode($file_cnt));

        }
        else
        {
            $mail=$body;
        }

    }

    $mail .= "\n--$mime_delimiter--";

    }
    else
    {
        $mail=$body;
    }
        $headers = "Mime-Version: 1.0\r\nFrom: $from \r\nContent-Type: multipart/mixed;\n boundary=\"$mime_delimiter\"\r\nContent-Disposition: inline";
        $headers .= "Reply-To: $visitor_email \r\n";
        mail($to,'Page Edit Request', $mail,$headers);

        if ($autoresponder_enabled) 

        auto_respond($vars);

        header("Location: $redirect_url");
}




// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:32:35+00:00Added an answer on June 2, 2026 at 4:32 pm

    Your mail composition is both extremely ugly and extremely wrong. The best suggestion I can give you is to stop reinventing the wheel and just use Swift mailer: http://swiftmailer.org/

    I’ve used their tool a few times and it just works!

    Edit not sure why this was downvoted, but fwiw this is the code for Swift:

    $message = Swift_Message::newInstance()
        ->setSubject('Page Edit Request')
        ->setFrom(array($visitor_email))
        ->setTo(array($your_email))
        ->setBody($body);
    
    if ($_FILES) {
        foreach (array_keys($_FILES) as $file) {
            if (UPLOAD_ERR_OK != $_FILES[$file]['error'] || !is_readable($_FILES[$file]['tmp_name'])) {
                continue;
            }
            $message->attach(Swift_Attachment::fromPath($_FILES[$file]['tmp_name']));
        }
    }
    
    $transport = Swift_MailTransport::newInstance();
    $mailer = Swift_Mailer::newInstance($transport);
    
    $numSent = $mailer->send($message);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following piece of code in Android to send a mail: Intent
I'm using Zend_Mail and the following code to send my email messages. $mail =
I'm trying to send mail from my application server using the following code FILE
I'm using the following code to send an email with attachments: $mime_boundary = <<<--==+X[.md5(time()).];
I'm using the following code in two view controllers; one, where it is triggered
I'm trying to send a mail using Zend_Mail using the following code: function sendMail()
I am using the following code to send an email with attachments. I can
I'm using following code to send sms throuh gsm modem for (int i =
i am using the following code to send request to the server var url='getDNormalForm.action';
I generate a vcard that I send to the client using the following code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.