我正在使用PHP开发一个小型电子邮件应用程序,为了发送我使用PHPMailer的电子邮件,所有功能分开形成一个“附件”。附件只是不通过发送的电子邮件。我尝试了许多不同的方法来修复它,但我现在认为的那个方法与工作秩序相差不远......
我将发布处理附件过程的一小部分代码....
HTML代码:
<form action="php/smtp_saved.php" method="post">
<input type="file" name="attach" id="attach" />
</form>
PHP代码:
if ($_POST['action'] == 'Send') {
if (preg_match('<,>', $email['recipient'])) {
$address = explode(',', $email['recipient']);
if (sizeof($address) < 19) {
foreach ($address as $recipient) {
$save = new saveSaved();
$save->save($_SESSION['user'], $email['recipient'], gmdate('Y-m-d H:i:s', strtotime ('+1 hour')), $_SESSION['delete'],$email['HTML']);
require_once('../PHPMailer/PHPMailerAutoload.php');
require_once('../PHPMailer/class.smtp.php');
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->Port = '465';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $_SESSION['user']; // SMTP username
$mail->Password = $_SESSION['pass']; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted
$mail->From = $_SESSION['user'];
$mail->FromName = 'OneTwoTrade';
//$mail->addAddress($email['recipient']); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addAddress(trim($recipient));
if (preg_match('<,>', $email['cc'])) {
$cc = explode(',', $email['cc']);
if (sizeof($cc) < 9) {
foreach ($cc as $carbonC) {
$mail->addCC($carbonC);
}
} else {
exit ('Max 10 Recipients Per Email');
}
} else {
$mail->addCC($email['cc']);
}
//$mail->addBCC('bcc@example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
if(is_uploaded_file($_FILES['attach']['tmp_name'])) {
$file = $_FILES['attach']['name'];
$mail->AddAttachment($file);
}
/*
if(isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK){
$mail->addAttachment($_FILES['uploaded_file']['tmp_name'],
$_FILES['uploaded_file']['name']);
}
*/
// Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $email['subject'];
$mail->Body = $email['HTML'];
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//$mail->SMTPDebug =1;
if (!$mail->send()) {
header("Location: ../saved_emails.php?error2");
//echo 'Message could not be sent.';
//echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
}
}
}
有人可以帮我发现错误或直接找到合适的信息来源吗??
答案 0 :(得分:1)
通过表单将文件添加enctype="multipart/form-data"
属性添加到form
标记时。我认为问题是因为这个而发生的。
<form action="php/smtp_saved.php" method="post" enctype="multipart/form-data">
<input type="file" name="attach" id="attach" />
</form>
经过一番搜索后,我发现了这一点:temp_name
函数中使用了附加文件的name
和AddAttachment()
个键:
if(isset($_FILES['attach']) && $_FILES['attach']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['attach']['tmp_name'], $_FILES['attach']['name']);
}
答案 1 :(得分:1)
$file = $_FILES['attach']['name'];
应为$file = $_FILES['attach']['tmp_name'];
。实际文件数据位于$_FILES['attach']['tmp_name']
。 $_FILES['attach']['name']
只是客户端上传文件的名称。
答案 2 :(得分:0)
好的家伙想要帮助所有试图帮助我并推动我解决问题的人,我设法解决了这个问题,这就是我必须要做的事情......
if(is_uploaded_file($_FILES['attach']['tmp_name'])) {
$file = $_FILES['attach']['tmp_name'];
$name = $_FILES['attach']['name'];
$mail->AddAttachment($file, $name);
}
:)))))))))))))))))))))))))))))))))))))))))
像魅力一样工作,只需检查PHPMailer文档和AddAttachment方法需要两个参数,