2019 PHPMailer 6.07/PHP Scrip如何发送电子邮件工作示例 [英] 2019 PHPMailer 6.07 / PHP Scrip How to send an email working example

查看:71
本文介绍了2019 PHPMailer 6.07/PHP Scrip如何发送电子邮件工作示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码不使用phpMailer v6.0.7发送电子邮件-我尝试了各种更改,即tls,ssl,465和587.我在日志中没有发现错误,php.ini上有错误,报告错误.与我的托管服务一起确认连接设置.我不想使用composer,但遵循github上的文档以及从github提取所有文件并上传到我的cpanel.不想使用5.2,因为它不再受支持.实时托管服务正在使用Linux和PHP7.检查垃圾邮件和所有文件夹. Cpanel有跟踪邮件或已投递邮件,以查看是否已被拒绝,而没有任何内容表明已发送或被拒绝.使用调试2,3,4,没有错误. php.ini的错误已打开.似乎从未将其发送到服务器被拒绝或允许.

This code does not send an email with phpMailer v6.0.7 - I have tried various changes, tls, ssl, 465 and 587. I have found no errors in logs, php.ini with errors on, reporting error. Worked with my hosting service to confirm connection settings. I dont want to use composer but followed the document on github as well as pulled all files from github and uploaded to my cpanel. Dont want to use 5.2 as its not supported anymore. The live hosting services is using linux and PHP 7. Check spam and all folders. Cpanel has a trace mail or delivered mail to see if it was rejected and nothing stated that it was sent or rejected. Used Debug 2,3,4 with no errors. Php.ini had the errors turned on. Seems like its nevered sent to the server to be rejected or allowed.

    <?php 
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    require 'PHPMailer/Exception.php';
    require 'PHPMailer/PHPMailer.php';
    require 'PHPMailer/SMTP.php';


    if(isset($_POST[‘submit’]))

    $mail = new PHPMailer(true);

    $mail->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
    $mail->isSMTP(); 

    $mail->Host = 'mail.email.org'; 
    $mail->SMTPSecure = 'ssl'; <-- Recommend by the hosting service <--Hosting Service Docs verify this
    $mail->Port = 465; <-- Recommend by the hosting service <--Hosting Service Docs verify this
    $mail->SMTPAuth = true;
    $mail->Username = 'Mail@email.org';
    $mail->Password = 'Using the correct Password'; <-- Resettled the password and verified its correct.


    $to = 'Mail@email.org'; <-- sending this to myself
    $from = 'Mail@email.org'; <--sending to myself

<Note this below was recommeended my hosting service to set it up this way below>
    $first_name = ((isset($_POST['FirstName']))&&(!is_null($_POST['FirstName'])))? $_POST['FirstName']:'';
    $last_name = ((isset($_POST['LastName']))&&(!is_null($_POST['LastName'])))? $_POST['LastName']:'';
    $email = ((isset($_POST['Email']))&&(!is_null($_POST['Email'])))? $_POST['Email']:'';
    $age = ((isset($_POST['Age']))&&(!is_null($_POST['Age'])))? $_POST['Age']:'';
    $student = ((isset($_POST['Student']))&&(!is_null($_POST['Student'])))? $_POST['Student']:'';
    $agree18 = ((isset($_POST['Agree18']))&&(!is_null($_POST['Agree18'])))? $_POST['Agree18']:'';


   /* Set the mail sender. */
    $mail->setFrom( $to , 'Research');

   /* Add a recipient. */
    $mail->addAddress( $_POST['Email'] , 'Research');
   /* Set the subject. */
    $mail->Subject = 'Learn More about Research Requested';

    $mail->isHTML(TRUE);
    $mail->Body = '<html> "First Name:" . $first_name . " Last Name:" . $last_name .  " Email:". $email . " Age:" . $age . " Student:" . $student . " Agree18:" . $agree18 . ""
</html>';


    $mail->AltBody = ' "First Name:" . $first_name . " Last Name:" . $last_name .  " Email:". $email . " Age:" . $age . " Student:" . $student . " Agree18:" . $agree18 . ""
'; <-- Note I have tried /r/n and failed 

    if($mail->send()){

      $msg="Your email msg has been send";


    }else{

       $msg="mail msg has not been send"; 
       echo 'Mailer Error: ' . $mail->ErrorInfo;
     }

   ?>



    <!DOCTYPE html>
    <html lang="en">
    <head>
    > 
    </head>
    <body>

    <div class="container">

    <form  method="POST" enctype=multipart/form-data action="">  
    <php echo '$msg' ?>
    <h4>Want to Learn More? Please fill out the form below.</h4>
     <div class="form-group">
    <label for="FirstName">First Name:</label>
    <input type="text" class="form-control" name="FirstName" id="FirstName" placeholder="First Name" required="">
  </div>

    <div class="form-group">
    <label for="LastName">Last Name:</label>
    <input type="text" class="form-control" name="LastName" id="LastName" placeholder="Last Name"required="">
  </div>


    <div class="form-group">
    <label for="Age">How old are you?</label>
    <input type="text" class="form-control"  name="Age" id="Age" placeholder="Age"required="">
  </div>

    <div class="form-group">
    <label for="Email">Email:</label>
    <input type="email" class="form-control"  name="Email" id="Email" placeholder="Yes@SModels.org" required="">
  </div>

    <h4Please Select if your a Student?</h4>
    <div><small class="form-text text-muted">
        Select all that apply.

    <div class="custom-control custom-checkbox">
    <input type="checkbox" class="custom-control-input"  name="Student" id="Student" value="Yes" >
    <label class="custom-control-label" for="Student">Student</label>
</div>

    <h4>Please agree to the following:</h4>

    <div class="custom-control custom-checkbox">
    <input type="checkbox" class="custom-control-input"  name="Agree18" id="Agree18" value="Yes" required="" > 
    <label class="custom-control-label" for="Agree18">I can prove I am 18 years or older. I am legal age to participate according to my country. Proof may be required by law.</label>
</div>
<div><small>
      All information will be held strictly confidential in accordance with our disclosure policy which can be requested.    
    </small></div> <br>
<center>

    <div class="submit">
    <button type="submit" name="submit" class="btn btn-primary">Send <i class="fa fa-lock"></i></button>
</div>
    </center> <br><br><br>
    </div>

    </form>  
    </div>
~~~~~~
I hope to have a working example code for the latest phpmailer 6.0.7 and I have read a lot of documents.  I want a good working example for 2019 since there is a lot of old phpmailer.

推荐答案

*首先,我要感谢Synchro 的帮助.以下是使用PHPMailer 6.0.7和实时托管为我工作的示例代码.我会记下我学到的东西并修复.***

*First, I want to thank Synchro for his Help. Below is the working example code for me with PHPMailer 6.0.7 and live hosting. I will note what I learn and fixed.***

  <?php 
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    use PHPMailer\PHPMailer\SMTP; <- This fixed most of my issues but not always required per Sychro
    require 'PHPMailer/Exception.php';
    require 'PHPMailer/PHPMailer.php';
    require 'PHPMailer/SMTP.php';



    $mail = new PHPMailer(true);

    if(isset($_POST[‘submit’]))
     { <--- Added Opening

  $first_name = ((isset($_POST['FirstName']))&&(!is_null($_POST['FirstName'])))? $_POST['FirstName']:'';
    $last_name = ((isset($_POST['LastName']))&&(!is_null($_POST['LastName'])))? $_POST['LastName']:'';
    $email = ((isset($_POST['Email']))&&(!is_null($_POST['Email'])))? $_POST['Email']:'';
    $age = ((isset($_POST['Age']))&&(!is_null($_POST['Age'])))? $_POST['Age']:'';
    $student = ((isset($_POST['Student']))&&(!is_null($_POST['Student'])))? $_POST['Student']:'';
    $agree18 = ((isset($_POST['Agree18']))&&(!is_null($_POST['Agree18'])))? $_POST['Agree18']:'';

    Note: Hosting Service should provide this infor to you.
    ----------    
    $mail->isSMTP(); 
    $mail->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
    $mail->Host = 'mail.email.org'; 
    $mail->SMTPSecure = 'ssl'; <-Could be TLS 
    $mail->Port = 465; <- Could be 587 or 25
    $mail->SMTPAuth = true;
    $mail->Username = 'Mail@email.org'; <-To access your Hosting email
    $mail->Password = 'Password'; 

     /* Set the mail sender. */
    $mail->setFrom('Mail@email.org'); <- From Myself
    $mail->addAddress('Mail@email.org'); <-To Myself

    $mail->Subject = 'Research Requested';

    $mail->isHTML(TRUE);
    $mail->Body = '<html> First Name: '.$first_name.' Last Name:  '.$last_name.' Email: '.$email.' Age: '.$age.' Student: '.$student.' Agree18: '.$agree18.'
</html>';

    $mail->send()
} <--Added Closing

   ?>



    <!DOCTYPE html>
    <html lang="en">
    <head>
    > 
    </head>
    <body>

    <div class="container">

    <form  method="POST" enctype=multipart/form-data action="#"> <--added #  

    <h4>Want to Learn More? Please fill out the form below.</h4>
     <div class="form-group">
    <label for="FirstName">First Name:</label>
    <input type="text" class="form-control" name="FirstName" id="FirstName" placeholder="First Name" required="">
  </div>

    <div class="form-group">
    <label for="LastName">Last Name:</label>
    <input type="text" class="form-control" name="LastName" id="LastName" placeholder="Last Name"required="">
  </div>


    <div class="form-group">
    <label for="Age">How old are you?</label>
    <input type="text" class="form-control"  name="Age" id="Age" placeholder="Age"required="">
  </div>

    <div class="form-group">
    <label for="Email">Email:</label>
    <input type="email" class="form-control"  name="Email" id="Email" placeholder="Yes@SModels.org" required="">
  </div>

    <h4Please Select if your a Student?</h4>
    <div><small class="form-text text-muted">
        Select all that apply.

    <div class="custom-control custom-checkbox">
    <input type="checkbox" class="custom-control-input"  name="Student" id="Student" value="Yes" >
    <label class="custom-control-label" for="Student">Student</label>
</div>

    <h4>Please agree to the following:</h4>

    <div class="custom-control custom-checkbox">
    <input type="checkbox" class="custom-control-input"  name="Agree18" id="Agree18" value="Yes" required="" > 
    <label class="custom-control-label" for="Agree18">I can prove I am 18 years or older. I am legal age to participate according to my country. Proof may be required by law.</label>
</div>
<div><small>
      All information will be held strictly confidential in accordance with our disclosure policy which can be requested.    
    </small></div> <br>
<center>

    <div class="submit">
    <button type="submit" name="submit" class="btn btn-primary">Send <i class="fa fa-lock"></i></button>
</div>
    </center> <br><br><br>
    </div>

    </form>  
    </div>

这篇关于2019 PHPMailer 6.07/PHP Scrip如何发送电子邮件工作示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆