2013-02-26 129 views
0

我製作了將圖片上傳到上傳文件夾的表單。但現在我需要用附件郵寄圖像。我試過這個嘗試發送帶附件的郵件

$Body .= "http://myurl.nl/upload/" . $filename . ""; 

實際上,只要可以直接從我的服務器下載它們,圖像是否在附件中並不重要。所以,現在的IM struggeling與文件的路徑

$allowedExts = array("jpg", "jpeg", "gif", "png"); 
$extension = end(explode(".", $_FILES["file"]["name"])); 
if ((($_FILES["file"]["type"] == "image/gif") 
|| ($_FILES["file"]["type"] == "image/jpeg") 
|| ($_FILES["file"]["type"] == "image/png") 
|| ($_FILES["file"]["type"] == "image/pjpeg")) 
&& ($_FILES["file"]["size"] < 10000000) 
&& in_array($extension, $allowedExts)) 
{ 
if ($_FILES["file"]["error"] > 0) 
{ 
echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
} 
else 
{ 
echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; 

if (file_exists("upload/" . $_FILES["file"]["name"])) 
    { 
    echo $_FILES["file"]["name"] . " already exists. "; 
    } 
else 
    { 
    $filename = str_replace(' ', '', $_FILES["file"]["tmp_name"]); 
    move_uploaded_file($_FILES["file"]["tmp_name"], 
    "upload/" . $filename); 
    echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 
    } 
    } 
} 
else 
{ 
echo "Invalid file"; 
} 
+0

是你使用文字按? – 2013-02-26 12:37:05

+0

沒有它只是一個基本的HTML和PHP – user2111146 2013-02-26 13:33:10

回答

2

我會使用Swiftmailer並使用下面的方法:從dqhendricks採取 http://swiftmailer.org/docs/messages.html#attaching-files

<?php 

require_once 'lib/swift_required.php'; 

// Create the message 
$message = Swift_Message::newInstance() 

    // Give the message a subject 
    ->setSubject('Your subject') 

    // Set the From address with an associative array 
    ->setFrom(array('[email protected]' => 'John Doe')) 

    // Set the To addresses with an associative array 
    ->setTo(array('[email protected]', '[email protected]' => 'A name')) 

    // Give it a body 
    ->setBody('Here is the message itself', 'text/html') 

    // You can attach files from a URL if allow_url_fopen is on in php.ini 
    ->attach(Swift_Attachment::fromPath('my-document.pdf')); 

// If you have SMPT, use the SMTP transport(http://swiftmailer.org/docs/sending.html#using-the-smtp-transport) 
// Create the Transport 
$transport = Swift_MailTransport::newInstance(); 

// Create the Mailer using your created Transport 
$mailer = Swift_Mailer::newInstance($transport); 

// Send the message 
$numSent = $mailer->send($message); 

printf("Sent %d messages\n", $numSent); 

/* Note that often that only the boolean equivalent of the 
    return value is of concern (zero indicates FALSE) 

if ($mailer->send($message)) 
{ 
    echo "Sent\n"; 
} 
else 
{ 
    echo "Failed\n"; 
} 

*/ 
+0

有了這個解決方案,似乎我必須從頭開始我的腳本。我更新了我的問題,可能是一個更簡單的解決方案 – user2111146 2013-02-26 13:40:58

0

搶答php send email with attachment

function mail_attachment($to, $subject, $message, $from, $file) { 
     // $file should include path and filename 
     $filename = basename($file); 
     $file_size = filesize($file); 
     $content = chunk_split(base64_encode(file_get_contents($file))); 
     $uid = md5(uniqid(time())); 
     $from = str_replace(array("\r", "\n"), '', $from); // to prevent email injection 
     $header = "From: ".$from."\r\n" 
      ."MIME-Version: 1.0\r\n" 
      ."Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n" 
      ."This is a multi-part message in MIME format.\r\n" 
      ."--".$uid."\r\n" 
      ."Content-type:text/plain; charset=iso-8859-1\r\n" 
      ."Content-Transfer-Encoding: 7bit\r\n\r\n" 
      .$message."\r\n\r\n" 
      ."--".$uid."\r\n" 
      ."Content-Type: application/octet-stream; name=\"".$filename."\"\r\n" 
      ."Content-Transfer-Encoding: base64\r\n" 
      ."Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n" 
      .$content."\r\n\r\n" 
      ."--".$uid."--"; 
     return mail($to, $subject, "", $header); 
    } 
+0

這可以自動發送表單上傳的文件。 – user2111146 2013-02-26 13:45:38

0

爲了好主意,請不要嘗試使用PHP內置的mail()函數來發送附件 - 對於基本來說已經夠糟糕的了郵件,但對於附件你真的需要使用一個體面的圖書館。

有幾個庫可以使用。

我建議phpMailer。它非常易於使用,並且使添加附件變得非常簡單 - 請參閱this examples page以瞭解它是多麼容易。

是的,這意味着你必須扔掉你現有的代碼。這是一件好事。沒有違法的代碼,但它肯定不是一個體面的郵件庫。 phpMailer一直在開發,並且已經處理了當時出現的所有錯誤和怪癖。如果您準備花費相同的時間修復自己的代碼中的錯誤,那麼一定要自己做。如果沒有,你真的應該利用一個圖書館,其他人已經爲你做了所有的辛苦工作。

0

我也會使用Swiftmailer(我仍然在使用symfony 1.4進行開發時使用它)。

但是,僅僅爲了完成這個答案,Swiftmailer在附加遠程文件(特別是PDF文件)時存在一些問題。這仍然是一個公開的問題(https://github.com/swiftmailer/swiftmailer/issues/207)。但在這個問題的結束,你可以找到一個解決辦法,那就是:

$attachment = Swift_Attachment::newInstance(file_get_contents($url_file)); 

我會重命名文件名補充它或將它附加壬文件:

$attachment = Swift_Attachment::newInstance(file_get_contents($url_file))->setFilename('arquivo.pdf'); 

就是這樣:)

相關問題