PHPMailer 根本不发送 [英] PHPMailer not sending at all

查看:48
本文介绍了PHPMailer 根本不发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHPMailer 发送问题:

我一直在按照教程尝试通过 PHPMailer 和 SMTP 发送电子邮件,但是它没有发送,我不确定为什么......也许我的逻辑关闭或其他什么?

它只是输出最后一条消息 $msg = "请再试一次!",并删除所有输入的表单字段.

这是我的 PHP 代码:

Host = "smtp.gmail.com";$mail->isSMTP();$mail->SMTPAuth = true;$mail->用户名 = "myemail@gmail.com";$mail->Password = "我的密码";$mail->SMTPSecure = "tls";//ssl$mail->端口 = 465;//587$mail->addAddress("myemail@gmail.com");$mail->setFrom($email, $name);$mail->Subject = $subject;$mail->isHTML(true);$mail->Body = $message;$mail->addAttachment($file);如果 ($mail->send())$msg = "您的表单已提交,谢谢,我会尽快回复";别的$msg = "请再试一次!";}//帖子结束提交?>

我的 HTML 代码:

 

<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">发送电子邮件</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"><!-- 显示用户选择的路径文件--><!-- <script type="text/javascript">document.getElementById('attachment-file').onchange = function () {alert('所选文件:' + this.value);};--><input class="btn btn-primary" type="submit" name="submit" value="send email"></表单>

解决方案

try this to show error message

if(!$mail->send()) {$msg = "邮件程序错误:".$mail->ErrorInfo;} 别的 {$msg = "您的表单已提交,谢谢,我会尽快回复";}

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>

解决方案

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";
}

这篇关于PHPMailer 根本不发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆