通过PHPMailer发送电子邮件(带有上传的附件)

时间:2017-07-17 14:06:02

标签: php email phpmailer

我正在尝试使用PHPMailer创建联系表单。我已经完成了发送电子邮件的部分,但我的附件有问题。我的文件已上传,但无法发送电子邮件。请帮我解决,非常感谢!

这是我的HTML:

 <div id="main">
        <h1>h1 tag </h1>
        <div id="login">
            <h3>content text. </h3>
            <hr/>
            <form action="index.php" method="post" enctype="multipart/form-data">
                <input type="text" placeholder="         name" name="subject"/> </br>
                <label class="checkbox-inline"><input type="checkbox" name="checkbox" value="Option">Option 1</label>
                <label class="checkbox-inline"><input type="checkbox" name="checkbox" value="Option 1Option">Option 2</label>
                <label class="checkbox-inline"><input type="checkbox" name="checkbox" value="Option 1Option 1Option 1">Option 3</label>
                <textarea rows="4" cols="50" placeholder="text arena" name="message"></textarea></br>                   
                <input type="hidden" name="MAX_FILE_SIZE" value="100000"> Send this file: <input name="attachment" type="file">                                     
                <input type="submit" value="Send" name="send"/>
            </form>  
            <div><h5>* new info</h5></div>
        </div>
    </div>

和php代码:

 <?php

          require 'phpmailer/PHPMailerAutoload.php';
          if(isset($_POST['send']))
              {
                $email = 'email@gmail.com';                    
                $password = 'jcfm1211';
                $to_id = 'receive@gmail.com';
                $message = $_POST['message'];
                $subject = $_POST['subject'];
                $option = $_POST['checkbox'];
                $sub = '=?UTF-8?B?'.base64_encode($subject).'?=';               
                date_default_timezone_set('Asia/Ho_Chi_Minh');
                $date = date("H:i - d/m/Y", time());
                // build attachment- i think here is my problem!
                $file = "attachment/" . basename($_FILES['attachment']['name']);
                move_uploaded_file($_FILES['attachment']['tmp_name'], $file));

                // build message body
                $body = '
                <html>
                <body>                  
                Info<br>
                data: '.$date.' <br><br>
                ___________________________________________________________________<br>

                Class hours: '.$message.'<br>
                lựa chọn: '.$option.'<br>
                <br>
                Date: '.$message.'<br>
                <br>
                You will receive an invitation from client info text removed. You may also receive an update with documents and a reminder with client info text removed.  Please watch your e-mail.<br>
                Thanks,<br>
                Name<br>
                ____________________________________________________________________<br>
                client info text removed<br>
                client info text removed<br>
                client info text removed<br>
                client info text removed<br>
                client info text removed<br>
                </body>
                </html>
                ';

                $mail = new PHPMailer;
                $mail->isSMTP();
                $mail->Host = 'smtp.gmail.com';
                $mail->Port = 587;
                $mail->SMTPSecure = 'tls';
                $mail->SMTPAuth = true;
                $mail->Username = $email;
                $mail->Password = $password;
                $mail->setFrom('email@gmail.com', 'email@gmail.com');                  
                $mail->addAddress($to_id);
                $mail->Subject = $sub;
                // attachment
                $mail->addAttachment($attachment);

                $mail->msgHTML($body);
                if (!$mail->send()) {
                   $error = "Mailer Error: " . $mail->ErrorInfo;
                    ?><script>alert('<?php echo $error ?>');</script><?php
                } 
                else {
                   echo '<script>alert("Thanks!");</script>';
                }
           }
    ?>

非常感谢!

1 个答案:

答案 0 :(得分:0)

$mail->addAttachment($attachment);

应该是 $mail->addAttachment($file);

相关问题