【问题标题】:PHP Page is not Sending EmailPHP页面未发送电子邮件
【发布时间】:2014-04-29 05:11:30
【问题描述】:

我有一个页面,其中有一个表单供用户输入他们的电子邮件。提交表单后,电子邮件将在我的 MSSQL 表中进行检查,如果它存在于表的某一行中,它将向用户的电子邮件发送一封电子邮件。现在,我正确地输入了一封现有的电子邮件,但从未收到该电子邮件。我正在尝试这样做,以便如果用户忘记密码,它将从正确的行中检索密码并将该密码发送到用户的电子邮件。

这是我的 PHP 页面代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php 
$conn=mssql_connect('gdm','dr','Rd1!');
mssql_select_db('Gdr',$conn);
if (isset($_POST['forgotpass'])) {
$conn=mssql_connect('gdom','GBdr','d1!');
mssql_select_db('Gdr',$conn);
    if (!get_magic_quotes_gpc()) {

        $_POST['email'] = addslashes($_POST['email']);

    }

 $email = $_POST['email'];
$querye = "SELECT password FROM staffportal WHERE email = '".$_POST['email']."'";
$check = mssql_query($querye, $conn);
$check2 = mssql_num_rows($check);
echo "".$check2."";
//if the email doesn't exist it gives an error
 if ($check2 != 0) {
print"<p>Thank you, dsa we will get back to you.</p>";
print"<p>Today's date isdsa.</p>";
    ob_start();
     $tae = "".$_POST['email']."";  
     echo "".$tae."";
    $out2 = ob_get_contents();
    ob_end_clean();
var_dump($out2);
var_dump($out2); 
$to = "".$out2."";
echo "Emailing to: ".$to."";
$subject = "Financing fordsac ";
$body = "dsdasd \n\n";
$headers = "From: info@gbmtd.ca";
mail($to, $subject, $body, $headers);
} else {
    echo "Sorry, the email ".$email." is incorrect.";
} } else {
?>
<form method="POST" action="<?php $_PHP_SELF ?>">
      Email:<br />
      <input type="text" name="email" id="email"/>
 <br /><br />
      <input type="submit" id="forgotpass" value="Change Password" name="forgotpass"/>
</form>
<?php } ?>
</body>
</html>

点击提交后,这会显示在我的页面上:

1
Thank you, dsa we will get back to you.

Today's date isdsa.

string(22) "kelseynealon@gmail.com" string(22) "kelseynealon@gmail.com" Emailing to: kelseynealon@gmail.com

非常感谢所有帮助。感谢您的帮助。

【问题讨论】:

  • 使用$to = $_POST['email']; 而不是$to = "".$out2."";
  • 在数据库中以明文形式存储密码是一个非常糟糕的主意。
  • 您的$to 基本上是在尝试将邮件发送到ob_get_contents() 内的所有内容,为什么不直接进行常规磨坊运行 $to = "email@example.com"; 进行测试。
  • mail() 返回什么?失败时返回 FALSE。请参阅文档。您还需要在服务器的防火墙中打开端口 25 才能使邮件正常工作。
  • 您是否尝试过仅使用一个简单的表单来查看mail() 是否确实有效?这会马上告诉你。 @Michael 或者,您知道这是实际问题吗?

标签: php html sql forms email


【解决方案1】:

我对 PHP 邮件功能的运气好坏参半。你可以到PHP邮件功能页面http://www.php.net/manual/en/function.mail.php看看,寻找线索。我个人使用 PHP SwiftMailer (http://swiftmailer.org/) 来处理从 PHP 应用程序发送的任何电子邮件,而且效果非常好。

这是我使用它的通用函数:

/*
Starting code for sending email via this function:
list($email_logger, $email_mailer) = email_interface();
$message = Swift_Message::newInstance()
        ->setFrom(array('from@domain.ext' => 'John Doe'))
        ->setTo(array('to@domain.ext' => 'Jane Doe'))
        ->setSubject('<SUBJECT>')
        ->setBody('<BODY>');
$email_mailer->send($message);
*/

// Returns PHP SwiftMailer mailer and logger email interfaces
function email_interface()
{

        // Mail configuration
        $global_email_config = array(
                //'relay_encryption' => 'ssl',
                //'relay_host' => 'relayhost.domain.ext',
                //'relay_port' => '465',
                // 'relay_user' => '<ADDR>',
                // 'relay_pass' => '<PASS>',
                'smtp_sender' => array(
                        'sender@domain.ext' => 'Sender Name'
                )
        );

        if (isset($global_email_config['relay_host'])) {
                $transport = Swift_SmtpTransport::newInstance();
                $transport->setHost($global_email_config['relay_host']);

                if (isset($global_email_config['relay_port'])) {
                        $transport->setPort($global_email_config['relay_port']);
                }

                if (isset($global_email_config['relay_encryption'])) {
                        $transport->setEncryption($global_email_config['relay_encryption']);
                }

                if (isset($global_email_config['relay_user'])) {
                        $transport->setUsername($global_email_config['relay_user']);
                        $transport->setPassword($global_email_config['relay_pass']);
                }

        } else {
                $transport = Swift_SendmailTransport::newInstance();
        }

        $mailer = Swift_Mailer::newInstance($transport);
        $logger = new Swift_Plugins_Loggers_ArrayLogger();
        $mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));

        return array(
                $logger,
                $mailer
        );
}

【讨论】:

    猜你喜欢
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    相关资源
    最近更新 更多