dtnd30892 2017-10-24 03:26
浏览 67
已采纳

PHPMailer根本不发送

PHPMailer send issue:

I have been following a tutorial to try and send an email via PHPMailer and SMTP, However it is not sending and I am not sure why... maybe my logic is off or something else?

It just outputs the last message $msg = "Please try again!", and removes all form fields entered.

Here is my PHP code:

<?php

 use PHPMailer\PHPMailer\PHPMailer;
 use PHPMailer\PHPMailer\Exception;

 require 'vendor/autoload.php';

 $msg = ""; //message variable
 if (isset($_POST['submit'])) {
  $name = $_POST['name'];
  $subject = $_POST['subject'];
  $email = $_POST['email'];
  $message = $_POST['message'];

//var_dump($_FILES);

if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] !="") {
  $file = "attachments/" . basename($_FILES['attachment']['name']);
  move_uploaded_file($_FILES['attachment']['tmp_name'], $file);
} else{
  $file = " ";  
} // end of file attachment

//echo $file;
//send via SMTP
$mail = new PHPMailer();

$mail->Host = "smtp.gmail.com";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Username = "myemail@gmail.com";
$mail->Password = "mypassword";
$mail->SMTPSecure = "tls"; //ssl
$mail->Port = 465; //587

$mail->addAddress("myemail@gmail.com");
$mail->setFrom($email, $name);
$mail->Subject = $subject;
$mail->isHTML(true);
$mail->Body = $message;
$mail->addAttachment($file);

if ($mail->send())
  $msg = "Your form has been submitted, thank you. I will reply very soon";
else
  $msg = "Please try again!";

 } // end of post submit
 ?>

my HTML code:

 <div class="container" style="margin-top: 80px">
  <div class="row justify-content-center">
    <div class="col-md-6 col-md-offset-3">
      <img class="sendemail mb-4 mx-auto d-block" src="./images/email.svg">

      <h2 class="text-center mb-5">Send an email</h2>

      <?php if ($msg != "") echo $msg;?>
      <form method="post" action="sendmail-v2.php" enctype="multipart/form-data">
          <input type="text" class="form-control" name="name" placeholder="name">
          <input type="text" class="form-control" name="subject" placeholder="subject">
          <input type="email" class="form-control" name="email" placeholder="email">

          <textarea class="form-control" name="message" placeholder="enter message here" rows="3"></textarea>

          <input type="file" name="attachment" class="file-input form-control d-block mb-4" id="attachment-file">

          <!-- display pathfile selected to user -->
           <!-- <script type="text/javascript">
            document.getElementById('attachment-file').onchange = function () {
              alert('Selected file: ' + this.value);
            };
          </script> -->

          <input class="btn btn-primary" type="submit" name="submit" value="send email">
      </form>
    </div>
  </div>
</div>
  • 写回答

1条回答 默认 最新

  • dqc18251 2017-10-24 03:50
    关注

    try this to show error message

    if(!$mail->send()) {
        $msg = "Mailer Error: " . $mail->ErrorInfo;
    } else {
        $msg = "Your form has been submitted, thank you. I will reply very soon";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵