2014-01-09 47 views
0

我試圖郵寄附件(圖片)的形式。 但我無法讓它工作。 我想我已經嘗試了幾乎所有東西,免除了一件事情,它會使它工作:) 並搜索谷歌和YouTube的所有。郵件附件的形式

我得到圖片,但沒有任何用戶名或城市。

有人可以幫我。 不知道我在做什麼錯誤或失蹤?

<?php 
if(isset($_POST['terms'])){ 
    $email_to = "[email protected]"; 
    $email_subject = "Subject"; 
    $user = $_POST['user']; // required 
    $city = $_POST['city']; // required 

    /*********Creating Uniqid Session*******/ 

    $txtSid = md5(uniqid(time())); 

    $headers = ""; 
    $headers .= "From: ".$_POST["fieldFormName"]."<".$_POST["fieldFormEmail"].">\nReply-To: ".$_POST["fieldFormEmail"].""; 

    $headers .= "MIME-Version: 1.0\n"; 
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$txtSid."\"\n\n"; 
    $headers .= "This is a multi-part message in MIME format.\n"; 

    $headers .= "--".$txtSid."\n"; 
    $headers .= "Content-type: text/html; charset=utf-8\n"; 
    $headers .= "Content-Transfer-Encoding: 7bit\n\n"; 
    $headers .= $email_message."\n\n"; 

    /***********Email Attachment************/ 
    if($_FILES["attachment"]["name"] != "") 
    { 
    $txtFilesName = $_FILES["attachment"]["name"]; 
    $txtContent = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"]))); 
    $headers .= "--".$txtSid."\n"; 
    $headers .= "Content-Type: application/octet-stream; name=\"".$txtFilesName."\"\n"; 
    $headers .= "Content-Transfer-Encoding: base64\n"; 
    $headers .= "Content-Disposition: attachment; filename=\"".$txtFilesName."\"\n\n"; 
    $headers .= $txtContent."\n\n"; 
    } 


    $email_message = "Message.\n\n"; 

    function clean_string($string) { 
    $bad = array("content-type","bcc:","to:","cc:","href"); 
    return str_replace($bad,"",$string); 
    } 

    $email_message .= "User: ".clean_string($user)."\n"; 
    $email_message .= "City: ".clean_string($city)."\n"; 

    // Send 
    @mail($email_to, $email_subject, $email_message, $headers); 
}else{ 
    echo "You need to accept our terms"; 
} 
?> 

回答

0

看看PHPMailer,它比創建自己的html/headers/etc更容易使用。

+0

我正在關注github上的簡單示例。 https://github.com/PHPMailer/PHPMailer 繼續收到此錯誤: 郵件無法發送。郵件程序錯誤:SMTP連接()失敗。 但電子郵件正在發送到郵件... SMPT信息是正確的..從我​​的服務器支持得到它。 有什麼想法嗎? – user2254488

+0

用一些新代碼更新你的問題或者開始一個新問題。 SMTP連接()錯誤提出了我的問題,您的smtp服務器不正確..也許還提供您的支持團隊的詳細信息提供商。 –

+0

我已經發布了一個新的問題與我的代碼在這裏: http://stackoverflow.com/questions/21168643/phpmailer-help-getting-smtp-error-but-mail-is-send 感謝。 – user2254488