Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have this script that works fine only with Fullname and the attachment file. However if I add one more field for example "tel", the script will send except the attachment. Can some one help me please?

<?php
    if(isset($_POST['submit']))
    {
        //The form has been submitted, prep a nice thank you fullname
        $output = '<h1>Thanks for your file and fullname!</h1>';
        //Set the form flag to no display (cheap way!)
        $flags = 'style="display:none;"';

        //Deal with the email
        $to = 'email here';
        $subject = 'a file for you';

        $fullname = strip_tags($_POST['fullname']);
        $tel = strip_tags($_POST['tel']);

        $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
        $filename = $_FILES['file']['name'];

        $boundary =md5(date('r', time())); 

        $headers = "From: webmaster@example.com
Reply-To: webmaster@example.com";
        $headers .= "
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="_1_$boundary"";

        $fullname="This is a multi-part fullname in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary="_2_$boundary"

--_2_$boundary
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

$fullname

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name="$filename" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--_1_$boundary--";



        mail($to, $subject, $fullname, $headers);
    }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>MailFile</title>
</head>

<body>

<?php echo $output; ?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
<p><label for="fullname">Full name</label> <input name="fullname" id="fullname" /></p>
<p><label for="file">File</label> <input type="file" name="file" id="file"></p>
<p><input type="submit" name="submit" id="submit" value="send"></p>
</form>
</body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
407 views
Welcome To Ask or Share your Answers For Others

1 Answer

There is no need to re-invent the wheel. Use the phpmailer class and then you can add attachments as simple as

function emailWithAttachment($to, $subject, $message, $attachment) {

    $mail = new PHPMailer();

    $mail->AddAddress($to);

    $mail->From         = "your@email.com";
    $mail->FromName     = "Your Name";
    $mail->Subject      = $subject;
    $mail->Body         = $message;

    $mail->AddAttachment($attachment);

}

Here is your script, rewritten using phpmailer.class

<?php

    if(isset($_POST['submit']))
    {

        //The form has been submitted, prep a nice thank you fullname
        $output = '<h1>Thanks for your file and fullname!</h1>';

        //Set the form flag to no display (cheap way!)
        $flags = 'style="display:none;"';

        // include and start phpmailer
        require_once('PHPMailer_5.2.4/class.phpmailer.php');
        $mail = new PHPMailer();

        //Deal with the email
        $mail->From = "webmaster@example.com"; // from
        $mail->AddReplyTo("webmaster@example.com", "Webmaster"); // reply to address/name

        $mail->AddAddress('email@address.here'); // to address

        $mail->Subject = 'A file for you'; // subject

        $mail->Body = $fullname; // body

        $mail->AddAttachment($_FILES['file']['tmp_name']); // attach uploaded file
    }

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>MailFile</title>
</head>

<body>

<?php echo $output; ?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
<p><label for="fullname">Full name</label> <input name="fullname" id="fullname" /></p>
<p><label for="file">File</label> <input type="file" name="file" id="file"></p>
<p><input type="submit" name="submit" id="submit" value="send"></p>
</form>
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...