0

我在上传和发送附件时遇到了一些问题。例如:

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

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

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

谢谢!

4

1 回答 1

2

什么不工作?为了帮助您找出什么不起作用,试试这个:

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

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

于 2013-09-19T17:50:00.197 回答