使用phpmailer和html表单发送带附件的邮件

时间:2014-10-31 12:08:31

标签: php html email phpmailer

我正在尝试使用gmail smtp将邮件从一个发送到另一个。 邮件发送成功但没有附件。我怎么能解决这个问题?

html表格:

<form id="main-contact-form" class="contact-form" name="contact-form"  method="post" action="sendresume.PHP" enctype="multipart/form-data">
                <div class="row">
                    <div class="col-sm-5">
                        <div class="form-group">
                            <input type="text" name="name" class="form-control" required="required" placeholder="Name">
                        </div>
                        <div class="form-group">
                            <input type="text" name="mobile" class="form-control" required="required" placeholder="Mobile number">
                        </div>
                        <div class="form-group">
                            <input type="email" name="email" class="form-control" required="required" placeholder="Email address">
                        </div>

                        <p class="lead" style="margin-bottom: 10px;">Resume :</p>
                         <div class="form-group">
                            <input type="file" name="file" class="form-control" required="required" >
                        </div>

                         <div class="form-group">
                            <button type="submit"  class="btn btn-primary btn-lg">Send Message</button>
                        </div>
                    </div>
                    <div class="col-sm-7">

                        <textarea name="messege" id="message" class="form-control" rows="8" placeholder="Note"></textarea>

                    </div>

                </div>
            </form>

和php代码是:

<?php
include "classes/class.phpmailer.php"; // include the class name
$email = $_POST["email"];
$messege=$_POST['messege'];
$name=$_POST['name'];
$mobile=$_POST['mobile'];
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "abc@gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom("feedback@abc.com");
$mail->Subject = "this is subject";
$mail->Body = "this is body <br/><br/><br/><br/>
name:$name <br/><br/>
email:$email<br/><br/>
mobile:$mobile<br/><br/>
messege:$messege</b>";

$mail -> Addattachment('here is problem');

$mail->AddAddress("mymail@gmail.com");
 if(!$mail->Send()){
    echo "Mailer Error: " . $mail->ErrorInfo;
 }
 else{
    echo "mail sent";
  }
 ?>

此邮件已成功发送,但我想要附件。目录中的文件存储并不重要。希望这个问题能立即解决。提前谢谢,因为我之前从未学过二手php。

1 个答案:

答案 0 :(得分:-1)