-1

我有一个表格。具有以下内容:

HTML:

<form name="feedback_form" method="post" enctype="multipart/form-data" action="" class="feedback_form">
    <input type="text" name="field-name" value="İsim" title="İsim" class="field-name form_field">
    <input type="text" name="field-email" value="Email" title="Email" class="field-email form_field">
    <input type="text" name="field-subject" value="Başlık" title="Başlık" class="field-subject form_field">
    <textarea name="field-message" cols="45" rows="5" title="Mesajınız..." class="field-message form_field">Mesajınız...</textarea>
    <label for='uploaded_file'>Fotoğrafınızı Yükleyin:</label>
    <input type="file" name="field-file" value="File">
    <br>
    <input type="reset" name="reset" id="reset2" value="Temizle" class="feedback_reset">
    <input type="button" name="submit" class="feedback_go" id="submit2" value="Gönder">                                    
</form>  

PHP:

<?php
header('Content-Type: text/html; charset=utf-8');

function sendFeedback($feedback_email, $feedback_msg, $feedback_name, $feedback_subject, $feedback_file) {


    /* EDIT THIS */
    $admin_email = "mymail@gmail.com";
    if ($feedback_subject == "Subject" || empty($feedback_subject) ) {
        $subj = "Email from your site";
    } else {
        $subj = $feedback_subject;
    }


    /* //EDIT THIS */


    $message = "
    <html>
    <head>
      <title>Websitenizin emaili</title>
    </head>
    <body>
        <p><a href='mailto:".$feedback_email."'>".$feedback_name."</a> send this message:</p>
        <p>".$feedback_msg."</p>
        <p>".$subject."</p>
    </body>
    </html>
    ";
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

    if ($feedback_name!=="Name" && $feedback_email!=="Email" && !empty($feedback_email) && !empty($feedback_msg) && !empty($feedback_name) ) {
        if ($feedback_email == "mail_error") {
            echo "<span class='ajaxok'>Geçersiz email adresi.</span>";
        } else {            
            mail($admin_email, $subj, $message, $headers);
            echo "<span class='ajaxok'>Teşekkürler. Mesajınız gönderilmiştir.</span>";  
        }
    } else {
        echo "<span class='ajaxalert'>Lütfen zorunlu alanları doldurunuz.</span>";      
    }


}

sendFeedback($_POST['email'], $_POST['message'], $_POST['name'], $_POST['subject'], $_FILES['file']);



?>

在此表单上发送消息时,发送电子邮件,工作。但只有主题、消息、姓名和电子邮件。我想在这个 php 代码中添加图片上传。但我不知道我该怎么做?请帮我。

4

2 回答 2

0

看这里: http: //pastebin.com/nAErtHgt

我写这个是因为邮件是在没有附件的情况下发送的,但似乎无法使用它。

将此函数添加到您的 PHP 文件中

function addattachment($file){
         $fname = substr(strrchr($file, "/"), 1);
         $data = file_get_contents($file);
         $i = count($this->parts);
         $content_id = "part$i." . sprintf("%09d", crc32($fname)) . strrchr($this->to_address, "@");
         $this->parts[$i] = "Content-Type: ".mime_content_type($file)."; name=\"$fname\"\r\n" .
                           "Content-Transfer-Encoding: base64\r\n" .
                           "Content-ID: <$content_id>\r\n" .
                           "Content-Disposition: inline;\n" .
                           " filename=\"$fname\"\r\n" .
                           "\n" .
                           chunk_split( base64_encode($data), 68, "\n");
         return $content_id;
     } 

在 $ message 和 $headers 之间添加

$message .= addattachment($feedback_file);

并且应该工作。

让我知道是否适合您。

于 2012-11-28T09:55:05.170 回答
0

我不推荐使用 PHP 的邮件功能,而是使用 PHPMailer 之类的东西,它是一个合理的类,用于发送带有多部分、附件等的邮件。

http://sourceforge.net/projects/phpmailer/(应该是很多替代品,但它适用于我)

然后只需将上传的 tmpfile 添加为附件。

于 2012-11-28T09:58:55.343 回答