1

我正在尝试在使用 php 从 gmail 发送邮件时添加附件。但它显示错误......它说Could not access file: hai.jpg

以下是我使用的代码

gmail.php

 <!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>PHPMailer - GMail SMTP test</title>
</head>
<body>
<?php
 access to that
date_default_timezone_set('Etc/UTC');

require '../Mail/class.phpmailer.php';

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug  = 2;
$mail->Debugoutput = 'html';
$mail->Host       = 'smtp.gmail.com';
$mail->Port       = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth   = true;
$mail->Username   = "name@gmail.com";
//Password to use for SMTP authentication
$mail->Password   = "passwrd";
$mail->SetFrom($_POST['from'], $_POST['from_name']);
$mail->AddReplyTo($_POST['from'],$_POST['from_name']);
$mail->AddAddress($_POST['to'],$_POST['to_name']);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->MsgHTML($_POST['message']);
$mail->AltBody = 'This is a plain-text message body';
$mail->AddAttachment($_POST['attachment'],'application/octet-stream');
if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
?>
</body>
</html>

索引.html

<form action="gmail.php" method="POST">
    <div>
        From Name:<input type="text" name="from_name" />
        From:<input type="text" name="from" />

    </div>
    <div>
        To Name:<input type="text" name="to_name" />
        To:<input type="text" name="to" />
    </div>
    <div>
        Message:<textarea  name="message"></textarea>
    </div>
    <div>
        Attachment:<input type="file" name="attachment" />
    </div>
    <input type="submit" value ="Submit"/>
</form>

我不知道如果我在这里做正确的方式会怎样。有人可以指导我吗?

4

2 回答 2

1

我注意到您的代码中没有两个项目:

  1. php $_FILES变量的使用,该变量存储有关上传文件的信息
  2. 在表单元素中使用enctype属性,这是与表单一起上传文件所必需的。

因此,您的代码必须处理这两个项目。

您的表单将如下所示:

<form action="gmail.php" method="POST" enctype="multipart/form-data">
    <div>
        From Name:<input type="text" name="from_name" />
        From:<input type="text" name="from" />

    </div>
    <div>
        To Name:<input type="text" name="to_name" />
        To:<input type="text" name="to" />
    </div>
    <div>
        Message:<textarea  name="message"></textarea>
    </div>
    <div>
        Attachment:<input type="file" name="attachment" />
    </div>
    <input type="submit" value ="Submit"/>
</form>

您的代码可能会处理该$_FILES变量:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>PHPMailer - GMail SMTP test</title>
</head>
<body>
<?php
 //access to that
date_default_timezone_set('Etc/UTC');

require '../Mail/class.phpmailer.php';

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug  = 2;
$mail->Debugoutput = 'html';
$mail->Host       = 'smtp.gmail.com';
$mail->Port       = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth   = true;
$mail->Username   = "name@gmail.com";
//Password to use for SMTP authentication
$mail->Password   = "passwrd";
$mail->SetFrom($_POST['from'], $_POST['from_name']);
$mail->AddReplyTo($_POST['from'],$_POST['from_name']);
$mail->AddAddress($_POST['to'],$_POST['to_name']);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->MsgHTML($_POST['message']);
$mail->AltBody = 'This is a plain-text message body';
//Attachs the file only if it was uploaded by http post
if (is_uploaded_file ($_FILES['attachment']['tmp_name'])) {
  $mail->AddAttachment($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name'], 'base64',$_FILES['attachment']['type']);
}
if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
?>
</body>
</html>
于 2013-07-31T19:35:09.293 回答
0

将文件从客户端浏览器上传到服务器并不是那么直接。

这是一个简单的教程

于 2013-07-31T19:20:07.797 回答