PHP MAILER - 电子邮件发送

时间:2014-04-25 03:56:48

标签: php

我想向正在尝试注册的用户发送确认邮件。我使用PHP Mailer发送邮件。这是我的代码:

require_once("PHPMailer/class.phpmailer.php");

        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->Host = "localhost";
        $mail->SMTPAuth = true;// turn on SMTP authentication
        $mail->Username = "root";// SMTP username
        $mail->Password = "";// SMTP password
        $mail->SetLanguage("en","PHPMailer/language");

        //compose mail
        $mail->From = "admin@localhost.com";
        $mail->FromName = "Cinema Admin";
        $mail->AddAddress($_POST['email']);
        $mail->AddReplyTo("admin@localhost.com", "Cinema Admin");
        $mail->Subject = 'Your confirmation link here';
        $mail->Body = "Your confirmation link\r\n";
        $mail->Body .= "Click on this link to activate your sccount\r\n";
        $mail->Body .= "http://localhost/user.login_plugin/confirm.php?passkey=$confirm_code"; 

        //send mail
        if (!$mail->Send())
        {
            echo "Message was not sent <p>";
            echo "Mailer Error: " . $mail->ErrorInfo;
            exit;
        }

        echo "Message has been sent";

当我执行文件时,它说

Message was not sent

Mailer Error: SMTP connect() failed.

有人建议我将邮件从localhost发送到注册用户的emailid。 我搜索了几个关于&#34;结束电子邮件的标题&#34;堆栈溢出,但无法从任何帖子中找到任何解决方案。

提前谢谢

2 个答案:

答案 0 :(得分:0)

您可以尝试删除smtp部分吗?从您的评论中,您没有邮件服务器设置。

    $mail = new PHPMailer();

    $mail->SetLanguage("en","PHPMailer/language"); // remove smtp things

答案 1 :(得分:0)

您需要SMTP详细信息才能使用SMTP发送电子邮件,例如,如果您要使用Gmail设置SMTP,

    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPAuth   = true;         // enable SMTP authentication
    $mail->SMTPSecure = "tls";        // sets the prefix to the servier
    $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
    $mail->Port       = 587;                   // set the SMTP port for the GMAIL server
    $mail->Username   = "yourusername@gmail.com";  // GMAIL username
    $mail->Password   = "yourpassword";            // GMAIL password

如果您不想使用SMTP,则可以调用默认邮件功能

$mail->IsMail(); //Sets Mailer to send message using PHP mail() function.