php自动邮件地址

lymnna71  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(414)

我想通过从数据库中获取电子邮件值向用户发送otp邮件。我正在使用php自动邮件。当我直接使用邮件地址,但当我从数据库检索它时,我不能发送它。有人能帮我寄吗。

function mailit()
    {
            $rand=rand(100000,999999);
            $user=$_SESSION['user'];

            $sql=mysqli_query($db,"UPDATE `doctor_login` SET `otp`='$rand' WHERE `doctor_id`='1'");

            $mail = new PHPMailer;

            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = '';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = 'myusername ';                 // SMTP username
            $mail->Password = 'mypassword';                           // SMTP password
            $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 587 ;                                    // TCP port to connect to

            $mail->setFrom('dont_reply_back@example.xyz');
            $mail->addAddress('$row');     // Add a recipient
            $mail->addReplyTo('dont_reply_back@example.xyz');

            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = 'THis is your OTP';
            $mail->Body    = 'Your otp is '.$rand; 

            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

            if(!$mail->send()) {
                echo 'Message could not be sent.';
                echo 'Mailer Error: ' . $mail->ErrorInfo;
    }        
  }

用于检索和显示电子邮件的代码

$mailadd=mysqli_query($db,"SELECT `doctor_email` FROM `doctor_login` WHERE `doctor_id`='1'");
    $row = mysqli_fetch_array($mailadd,MYSQLI_ASSOC);
    echo $row['doctor_email'];
    $row2=$rowa['doctor_email'];
    echo $row2;

我在$mail->addaddaddress中使用了$row和$row2,但似乎不起作用。我能做些什么改变。我给你一个具体地址就行了

pkwftd7m

pkwftd7m1#

我认为这是一个范围问题。如果在代码主体中调用此代码

$mailadd=mysqli_query($db,"SELECT `doctor_email` 
                            FROM `doctor_login` 
                            WHERE `doctor_id`='1'");
$row = mysqli_fetch_array($mailadd,MYSQLI_ASSOC);

那我猜你会打电话给我 mailit() 之后,将要用作参数的数据传递给 mailit() 函数,使其在函数范围内可用。

function mailit($row)
{
    $rand=rand(100000,999999);
    $user=$_SESSION['user'];

    $sql=mysqli_query($db,"UPDATE `doctor_login` SET `otp`='$rand' WHERE `doctor_id`='1'");

    $mail = new PHPMailer;

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = '';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'myusername ';                 // SMTP username
    $mail->Password = 'mypassword';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587 ;                                    // TCP port to connect to

    $mail->setFrom('dont_reply_back@example.xyz');
    $mail->addAddress($row['doctor_email']);     // Add a recipient
    $mail->addReplyTo('dont_reply_back@example.xyz');

    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'THis is your OTP';
    $mail->Body    = 'Your otp is '.$rand; 

    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }        
}

$mailadd=mysqli_query($db,"SELECT `doctor_email` 
                            FROM `doctor_login` 
                            WHERE `doctor_id`='1'");
$row = mysqli_fetch_array($mailadd,MYSQLI_ASSOC);

mailit($row);

您应该从原始代码中得到错误,因此在将来添加以下内容:

ini_set('display_errors', 1); 
ini_set('log_errors',1); 
error_reporting(E_ALL); 
// if you are using the `mysqli` API
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

到你剧本的顶端。这将强制任何mysqli错误生成一个异常,您可以在浏览器上看到该异常以及正常的php错误。

相关问题