我正在学习使用
SwiftMailer,我无法从附件中获取附件
输入类型.我不应该只需要:
<input name="attachment" id="attachment" type="file">
然后
$message = Swift_Message::newInstance()
->attach(new Swift_Message_Attachment(new Swift_File($file), $filename,));
这是我的完整代码
<?php
if (!empty($_POST)) {
$success = $error = false;
$post = new stdClass;
$file = $_FILES["attachment"]["tmp_name"]; //"attachment" is the name of your input field, "tmp_name" gets the temporary path to the uploaded file.
$filename = $_FILES["attachment"]["name"]; //"name" gets the filename of the uploaded file.
foreach ($_POST as $key => $val)
$post->$key = trim(strip_tags($_POST[$key]));
if (empty($post->name) OR empty($post->email))
$error = true;
else {
$dir = dirname(__FILE__);
ob_start();
require_once($dir.'/pdf.php');
$pdf_html = ob_get_contents();
ob_end_clean();
require_once($dir.'/dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->load_html($pdf_html);
$dompdf->render();
$pdf_content = $dompdf->output();
ob_start();
require_once($dir.'/html.php');
$html_message = ob_get_contents();
ob_end_clean();
require_once($dir.'/swift/swift_required.php');
$mailer = new Swift_Mailer(new Swift_MailTransport());
$message = Swift_Message::newInstance()
->setSubject('Order Entry') // Message subject
->setTo(array('ehilse@paifashion.com' => 'Eric Hilse')) // Array of people to send to
->setFrom(array('no-reply@paifashion.com' => 'PAi Order Entry'))
->setBody($html_message, 'text/html') // Attach that HTML message from earlier
->attach(Swift_Attachment::newInstance($pdf_content, 'design.pdf', 'application/pdf')); // Attach the generated PDF from earlier
->attach(new Swift_Message_Attachment(new Swift_File($file), $filename));
// Send the email, and show user message
if ($mailer->send($message))
$success = true;
else
$error = true;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>PAi Cap & Tee Art Pack Order Form</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="container">
<div id="logo">
<img src="images/pailogo.png" id="logo" />
</div>
<h1><abbr title="Paramount Apparel International">PAi</abbr> Cap & Tee Art Pack Order Form</h1>
<?php if ($success) { ?>
<div class="message success">
<h4>Congratulations! It worked! Your order has been sent.</h4>
</div>
<?php } elseif ($error) { ?>
<div class="message error">
<h4>Sorry, an error occurred. Try again!</h4>
</div>
<?php } ?>
<form method="post" action="" enctype="multipart/form-data">
<fieldset>
<legend>Please fill in the following form:</legend>
<label for="name">Your Name:<span class="required">*</span></label>
<input type="text" name="name" id="name" class="input" />
<label for="email">Your Email:<span class="required">*</span></label>
<input type="text" name="email" id="email" class="input" />
<br />
<label for="artpacknumber">Art Pack Number:<span class="required">*</span></label>
<input type="text" name="artpacknumber" id="artpacknumber" class="input" />
<label for="language">New Or Revise?<span class="required">*</span></label>
<select name="language" id="language">
<option value="NEW">New</option>
<option value="REVISE">Revise</option>
</select>
<br />
<label for="duedate">Due Date:<span class="required">*</span></label>
<input type="text" name="duedate" id="duedate" class="input" />
<label for="dateentered">Date Entered:<span class="required">*</span></label>
<input type="text" name="dateentered" id="dateentered" class="input" />
<br />
<label for="designname">Design Name:<span class="required">*</span></label>
<input type="text" name="designname" id="designname" class="input" />
<br />
<label for="customer">Customer:<span class="required">*</span></label>
<input type="text" name="customer" id="customer" class="input" />
<label for="account">Account:<span class="required">*</span></label>
<input type="text" name="account" id="account" class="input" />
<br />
<label for="salesrep">Sales Rep:<span class="required">*</span></label>
<input type="text" name="salesrep" id="salesrep" class="input" />
<label for="extension">Extension:<span class="required">*</span></label>
<input type="text" name="extension" id="extension" class="input" />
<br />
<label class="commentslabel" for="comments">Comments:<span class="required">*</span></label>
<textarea name="comments" id="comments" rows="4" cols="40"></textarea>
<br />
<label for="attachment">File Upload<br />.</label>
<input name="attachment" id="attachment" type="file">
<br />
<input type="submit" class="submit" id="submit" value="Submit" />
</fieldset>
</form>
</div>
这是我的新错误
[27-Dec-2011 14:51:58] PHP已弃用:第4332行的/Applications/MAMP/htdocs/paipdf2/dompdf/lib/class.pdf.php中不推荐使用函数set_magic_quotes_runtime()
[27-Dec-2011 14:51:58] PHP不推荐使用:函数set_magic_quotes_runtime()在4348行的/Applications/MAMP/htdocs/paipdf2/dompdf/lib/class.pdf.php中已弃用
[27-Dec-2011 14:51:58] PHP警告:require_once(/Applications/MAMP/htdocs/paipdf2/dompdf/include/swift_message_attachment.cls.php)[function.require-once]:无法打开流:否第194行/Applications/MAMP/htdocs/paipdf2/dompdf/dompdf_config.inc.php中的此类文件或目录
[27-Dec-2011 14:51:58] PHP致命错误:require_once()[function.require]:无法打开所需的’/Applications/MAMP/htdocs/paipdf2/dompdf/include/swift_message_attachment.cls.php'(include_path第194行/Applications/MAMP/htdocs/paipdf2/dompdf/dompdf_config.inc.php中的=’.:/ Applications / MAMP / bin / php / php5.3.6 / lib / php’)
最佳答案 我猜$file和$filename不带文件路径和名称的实际值.
假设表单发布正确完成,要访问上传文件的路径,您必须使用$_FILES超全局数组,如下所示:
$file = $_FILES["attachment"]["tmp_name"]; //"attachment" is the name of your input field, "tmp_name" gets the temporary path to the uploaded file.
$filename = $_FILES["attachment"]["name"]; //"name" gets the filename of the uploaded file.
$message = Swift_Message::newInstance()
->attach(new Swift_Message_Attachment(new Swift_File($file), $filename));
文档:http://www.php.net/manual/en/reserved.variables.files.php