开发者

PHPMailer Problems

Okay, so I've downloaded PHP Mailer's class, required it using require_once then make a function for sending mail:

public function sendMail($to, $subject, $body) { 
            $mail = new phpmailer;

            $mail->IsSMTP(); // set mailer to use SMTP
            $mail->SMTPAuth = true;
            $mail->Username = 'USERNAME';
            $mail->Password = 'PASSWORD';
            $mail->Port = 465;
            $mail->SMTPSecure = 'SSL';
            $mail->From = "EMAIL";
            $mail->FromName = "FROM NAME";
            $mail->Host = "smtp.gmail.com;";  // specify main and backup server

            if(is_array($to)) {
                foreach($to as $x) {
                    $mail->AddAddress($x);
                }
            } else {
                $mail->AddAddress($to);
            }

            $mail->AddReplyTo("REPLY EMAIL", "REPLY NAME");
            $mail->WordWrap = 50;    // set word wrap

            $mail->IsHTML(true);    // set email format to HTML
            $mail->Subject = $sub开发者_JAVA百科ject;
            $mail->Body = $body;

            if(!$mail->Send()){
                return false;
            }
        }

And when I go to do (it's in the class $core)

if($core->sendMail('MYEMAIL@gmail.com', 'Something', 'Some body')) {
        echo 'Mail sent';
    } else {
        echo 'Fail';
    }

It returns fail. The code in the script holds the correct information, I've just used placeholders for posting it here.


$mail contains the error message, do the following:

if (!$mail->Send()) {
    throw new Exception($mail->ErrorInfo);
}

instead of just returning "false".


        if(!$mail->Send()){
            return false;
        }

should be

        if(!$mail->Send()){
            return false;
        } else {
            return true;
        }

because functions by default return false, when the return value is not specified inside the function.

You can also go with the cleaner version:

        return $mail->Send();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜