【问题标题】:Trying to send a mail with attachment尝试发送带有附件的邮件
【发布时间】:2013-02-26 12:35:16
【问题描述】:

我已经制作了一个将图像上传到上传文件夹的表单。但现在我需要将图像邮寄到附件中。我试过这个

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

其实附件里有没有图片没关系,只要他们能直接从我的服务器上下载就行了。所以现在我正在努力解决文件的路径

$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";
 }

【问题讨论】:

  • 你在用word press吗?
  • 不,它只是一个基本的html和php

标签: php image email attachment


【解决方案1】:

我会使用 Swiftmailer 并使用以下方法: 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('john@doe.com' => 'John Doe'))

  // Set the To addresses with an associative array
  ->setTo(array('receiver@domain.org', 'other@domain.org' => '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";
}

*/

【讨论】:

  • 有了这个解决方案,我似乎必须从我的脚本重新开始。我已经更新了我的问题,也许是一个更简单的解决方案
【解决方案2】:

答案来自php send email with attachment 中的 dqhendricks

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);
     }

【讨论】:

  • 这可以自动从表单发送上传的文件吗?
【解决方案3】:

我也会使用 Swiftmailer(我在使用 symfony 1.4 开发时仍然使用它)。

但为了完成这个答案,Swiftmailer 在附加远程文件时遇到了一些问题,特别是 PDF 文件。这仍然是一个悬而未决的问题 (https://github.com/swiftmailer/swiftmailer/issues/207)。但在本期结束时,您可以找到一个解决方法,即:

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

我会通过重命名文件名来补充它,否则它会附加一个 nonane 文件:

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

就是这样:)

【讨论】:

    【解决方案4】:

    看在上帝的份上,请不要尝试使用 PHP 的内置 mail() 函数来发送附件——这对于基本邮件来说已经够糟糕了,但是对于你真的需要使用的附件一个不错的图书馆。

    您可以使用几个库。

    我建议phpMailer。它非常易于使用,并且使添加附件变得非常简单——请参阅this examples page 了解它是多么容易。

    是的,这意味着您必须丢弃现有代码。这是一件好事。没有冒犯您的代码,但它肯定不是一个像样的邮件库。 phpMailer 已经开发了年,并且已经处理了当时出现的所有错误和怪癖。如果您准备花同样多的时间修复自己代码中的错误,那么当然可以自己动手。如果没有,您真的应该使用其他人已经为您完成所有艰苦工作的库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 2017-07-31
      • 2016-05-03
      • 2015-09-09
      相关资源
      最近更新 更多