【问题标题】:Send attachment file using PHPMailer使用 PHPMailer 发送附件文件
【发布时间】:2016-04-02 16:19:51
【问题描述】:

这是我使用 PHPMailer 使用 PHPMailer 通过邮件发送附件的代码。我在 localhost 中尝试了它,它运行良好。但是一旦我上传到我的服务器,附件就不会发送。

$email = new PHPMailer();
$email->From      = 'john@hotmail.com';
$email->FromName  = 'John';
$email->Subject   = 'Message Subject';
$email->Body      = 'test';
$email->AddAddress( 'william@hotmail.com' );

$file_to_attach = $_FILES["cv"]["tmp_name"];

$email->AddAttachment( $file_to_attach , $_FILES["cv"]["name"]);

return $email->Send();

我收不到附件文件,我上传了。

我错过了任何代码吗..?

【问题讨论】:

  • 你使用phpmailer的理由是什么?有一种纯 PHP 的带附件的电子邮件发送方法。
  • @Rynoh97 因为 PHPMailer 无需编写大量代码即可更快地工作。正确..?
  • @user3663143 确实如此......但我建议使用 PHP 方法。我目前正在尝试解决您的问题,但我建议这种方法:webcheatsheet.com/php/send_email_text_html_attachment.php 但这只是我。
  • @Rynoh97 谢谢你的回复。我尝试在本地主机中执行我的代码是向我发送带有附件的正文内容。但是当我上传到我的服务器时,附件文件不显示

标签: php phpmailer


【解决方案1】:

您的代码没有任何问题 - 它在 localhost 上对您(也对我)工作正常,所以它一定是您的服务器环境的问题。这可能是几件事:

没有安装邮件服务器的 Windows 服务器?它可能是您上传文件的权限,或者实际上是上传文件的能力,因为这也是通过 php.ini 控制的。

我建议你把阶段分开:

  • 上传文件并保存,检查它是否在服务器上。

  • 检查您的 PHP 脚本是否可以查看和读取上传的文件(以及 请read the docs如何做 这是正确的)。

  • 发送一封没有附件的简单电子邮件。如果这行得通,你知道你的 邮件服务器正常。

当所有单独的部分都起作用时,将它们连接在一起。

【讨论】:

    【解决方案2】:
    <?php 
    if(isset($_POST['sbt_resume'])){
        $ext = explode('.',$_FILES['upload_resume']['name']);
        $extension = $ext[1];
        $newname = uniqid();
        $full_local_path = 'resume/'.$newname.'.'.$extension;
        $upld = move_uploaded_file($_FILES['upload_resume']['tmp_name'], "$full_local_path");
        require_once('PHPMailer/class.phpmailer.php');
        $emailid = new PHPMailer();
        $emailid->IsSMTP(); // enable SMTP
        $emailid->SMTPAuth = true; // authentication enabled
        $emailid->Host = "smtp.gmail.com";
        $emailid->Port = 465; // or 587
        $emailid->IsHTML(true);
        $emailid->SMTPSecure = 'ssl';
        $emailid->Username = "yourgmail@gmail.com";
        $emailid->Password = "";
        $emailid->From = "yourgmail@gmail.com";
        $emailid->FromName = $name;
        $emailid->Subject = ".";
        $emailid->Body = "Atachement";
        $emailid->AddAddress("");
        $emailid->AddBCC($email);
        $emailid->AddAttachment($full_local_path);
        $emailid->Send();
        echo "<font style='color: green; margin-top: 10px;'>Thank you for upload your Resume we will get back you soon.</font>";
    }
    ?>
    
    <form class="contact-form clearfix" action="" method="post" enctype="multipart/form-data">
     <div class="row">      
            <!-- col-md-3 -->
            <div class="col-md-4 col-sm-4 col-xs-4">
                <div class="input-label">
                    <p style="padding-top: 10px;">upload <span>*</span></p>
                </div>
            </div>
            <div class="col-md-4 col-sm-4 col-xs-4">
                    <input accept="" type="file" name="upload_resume" class="valid" style="border: 1px solid #E0E0E0;padding: 12px 15px;">
            </div>
            <!-- col-md-9 -->
        </div>
        <!-- row --> 
    
        <!-- row -->
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <p class="contact-button clearfix">                    
                    <span><input type="submit" name="sbt_resume" value="Send" id="submit-contact"></span>
                </p>
            </div>
            <!-- col-md-12 -->
        </div>
        <!-- row --> 
    </form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 2014-02-14
      相关资源
      最近更新 更多