如何将文件上传到自定义文件夹并将附件发送到wordpress中的电子邮件

时间:2013-09-19 16:54:04

标签: wordpress email file-upload attachment

我在理解上传和发送附件时遇到了一些麻烦。 例如:

$zip_code = $_POST['zip_code'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$attachments = '';
if (!empty($_FILES['attachment']['tmp_name'])) {
    $path = $_FILES['attachment']['name'];
    if (copy($_FILES['attachment']['tmp_name'], $path)) $attachments = $path;
}

$subject = 'You have message from ' . get_bloginfo('name');
$message = '<table width="100%" cellspacing="0" cellpadding="5" border="0">';
$message .= '<tr><td width="150px"><b>Zip Code:</b></td><td>' . $zip_code . '</td></tr>';
$message .= '<tr><td width="150px"><b>Phone:</b></td><td>' . $phone . '</td></tr>';
$message .= '<tr><td width="150px"><b>Email:</b></td><td>' . $email . '</td></tr>';
$message .= '<tr><td width="150px"><b>Attachments:</b></td><td>' . $attachments . '</td></tr>';
$message .= '</table>';
//php mailer variables
$to = get_option('admin_email');
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Disposition: attachment; filename = \"" . $attachments . "\"\n\n";
$sent = wp_mail($to, $subject, $message, $headers, $attachments);

此代码工作不正确。电子邮件和附件发送正确,但文件上传到根目录。当我上传相同的文件时,新文件会覆盖旧文件。我不明白如何将一些哈希标记添加到文件名。

也许你可以帮助我吗?任何想法或我可以阅读如何解决此问题。

谢谢!

1 个答案:

答案 0 :(得分:1)

什么不起作用?为了帮助您找出不起作用的内容,请尝试以下方法:

$zip_code = $_POST['zip_code'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$attachments = '';
if (!empty($_FILES['attachment']['tmp_name'])) {
    $path = $_FILES['attachment']['name'];
    if (copy($_FILES['attachment']['tmp_name'], $path)) $attachments = $path;
}

$subject = 'You have message from ' . get_bloginfo('name');
$message = '<table width="100%" cellspacing="0" cellpadding="5" border="0">';
$message .= '<tr><td width="150px"><b>Zip Code:</b></td><td>' . $zip_code . '</td></tr>';
$message .= '<tr><td width="150px"><b>Phone:</b></td><td>' . $phone . '</td></tr>';
$message .= '<tr><td width="150px"><b>Email:</b></td><td>' . $email . '</td></tr>';
$message .= '<tr><td width="150px"><b>Attachments:</b></td><td>' . $attachments . '</td></tr>';
$message .= '</table>';
//php mailer variables
$to = get_option('admin_email');
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Disposition: attachment; filename = \"" . $attachments . "\"\n\n";

echo 'Headers:  . $headers . '<br>/r/n';
echo 'To: ' . $to .'<br>/r/n';
echo 'Subject: ' . $subject . '<br>/r/n';
echo 'Message: ' . $message . '<br>/r/n';
echo 'Image: <img src="' . $attachments .'" />';


// $sent = wp_mail($to, $subject, $message, $headers, $attachments);

如果缺少任何内容,您将能够看到丢失的内容,并且您将能够以这种方式纠正您的代码。