【问题标题】:adding attachment from form while sending mail from gmail via php通过 php 从 gmail 发送邮件时从表单添加附件
【发布时间】:2013-08-01 11:24:27
【问题描述】:

我正在尝试在使用 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>

index.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>

我不知道如果我在这里做正确的方式会怎样。谁能指导我完成这个?

【问题讨论】:

  • 最好的方法是上传文件并在邮件中链接。
  • 您错过了表单元素中的 enctype 属性。例如,
  • $_POST['attachment'] 是您正在上传的文件吗?如果是这样,那么这不是 php 处理文件上传的方式。
  • 请告诉我该怎么做...!!!

标签: php gmail bulk-mail


【解决方案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>

【讨论】:

  • 这个代码附件正在运行,但不是我选择的那个。它是application/octect-stream.dat
  • 对。您介意传递一个指向 phpmailer 文档的链接吗?
【解决方案2】:

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

Here's a simple tutorial

【讨论】:

    猜你喜欢
    • 2021-05-08
    • 2014-08-27
    • 2019-01-19
    • 2016-07-19
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    相关资源
    最近更新 更多