PHP的自动邮件地址addAddress

时间:2018-10-28 16:50:27

标签: php mysql function session phpmailer

我想通过从数据库中获取电子邮件值来向用户发送otp邮件。我正在使用php auto mailer。当我直接使用邮件地址时,它起作用了,但是当我从数据库中检索它时,我无法发送它。任何人都可以帮我发送。

update

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

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

我在$ mail-> addAddress中同时使用了$ row和$ row2,但似乎没有用。我可以做些什么改变。我提供了一个特定的地址就可以了

1 个答案:

答案 0 :(得分:0)

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

$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错误。