简体   繁体   中英

i am unable to send mail with help oh PHPMailer in yii 2.0

whenever I click on send button it always fired 0 value (else part).whenever I click on send button it always fired 0 value (else part).I am unable to send mail on a server with help of PHPMailer in Yii 2.0 framework..I am unable to send mail on a server with help of PHPMailer in Yii 2.0 framework. please help me and thank you in advance.!

<?php        

    //Check e-mail validation
    function check_email($email){
    if(!@eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
            return false;
        } else {
            return true;
        }
    }
     function validate_mobile($mobile)
    {
        return preg_match('/^[0-9]{10}+$/', $mobile);
    }

    //Get post data   
    if(isset($_POST['name']) and isset($_POST['email']) and isset($_POST['mobile'])){             
        $name       =  $_POST['name'];
        $email      =  $_POST['email'];
        $mobile     =  $_POST['mobile'];
        $comment    =  $_POST['comment'];

        if($name == '') {
            echo json_encode(array('info' => 'error', 'msg' => "Please enter your name."));
            exit();
        } else if($email == '' or check_email($email) == false){
            echo json_encode(array('info' => 'error', 'msg' => "Please enter valid e-mail."));
            exit();
        } else if($mobile == '' or validate_mobile($mobile) == false){
            echo json_encode(array('info' => 'error', 'msg' => "Please enter 10 digit mobile number."));
            exit();
        } else if($comment == ''){
            echo json_encode(array('info' => 'error', 'msg' => "Please enter your message."));
            exit();
        } else {

        $message = '
            <html>
            <head>
              <title>Mail from '. $name .'</title>
            </head>
            <body>
              <table border="0" cellspacing="0" cellpadding="0" class="tableContent bgBody" align="center"  style="font-family:Helvetica, sans-serif; width:100%;">
                <tr>
                    <td style="padding-bottom: 28px;">                   
                        <table style="font-size:14px;width:100%;">                        
                            <tr>
                                <td style="width:18%;">
                                    <b>Name :</b>        
                                </td>
                                <td>
                                    <span style="">
                                        '.$name.' 
                                    </span>
                                </td>
                            </tr>

                            <tr>
                                <td style="width:18%;">
                                    <b>Email :</b>        
                                </td>
                                <td>
                                    <span style="">
                                        '.$email.' 
                                    </span>
                                </td>
                            </tr>

                            <tr>
                                <td>
                                    <b>Contact Number :</b>
                                </td>
                                <td>
                                    <span style="">
                                        '.$mobile.'
                                    </span>
                                </td>
                            </tr>

                            <tr>
                                <td>
                                    <b>Message :</b>
                                </td>
                                <td>
                                    <span style="">
                                        '.$comment.'
                                    </span>
                                </td>
                            </tr>
                        </table>                     
                    </td>
                </tr>                 
            </table>
            </body>
            </html>
            ';

        require 'PHPMailer/PHPMailerAutoload.php';

         //Send Mail   
        $mail->addReplyTo = "user@gmail.com";
        $mail->subject = 'Xion-Solutions';
        $mail->setFrom = $email;
        $mail->Body = $message;


        $mail = new PHPMailer;
        $mail->isSMTP();               
        $mail->SMTPSecure = 'ssl';                          
        $mail->SMTPDebug = 1;
        $mail->Debugoutput = 'html';
        $mail->isSMTP();                                     
        $mail->Host = 'mail.xionproaudio.com';             
        $mail->SMTPAuth = true;                              
        $mail->Username = 'info@xionproaudio.com';        
        $mail->Password = 'INdia@1991$';                         
        $mail->Port = 465;
        $mail->IsHTML(true);


            if($mail->send()){
                echo 1;
            }else{
                echo 0;
            }

        }
    } else {
        echo json_encode(array('info' => 'error', 'msg' => __MESSAGE_EMPTY_FILDS__));
    }
 ?>

I'll second the call to use Yii's built-in mailer class - if you're going to use a framework, use the framework.

As always with PHPMailer: Read the PHPMailer troubleshooting guide , base your code on the examples provided with PHPMailer , use the latest version .

Many basic problems here.

  • You're not giving it an address to send to.
  • You're forging the from address.
  • You're using your own address as the reply-to.
  • You're trying to set property names that are methods.
  • You're ignoring that PHP property names are case sensitive.
  • You're not generating any error message output if sending fails.
  • You've set debug output to only show client-side messages, so you can't see what's going on.

If you'd based your code on one of the examples provided you would have avoided most of these problems. The main parts you need to fix are:

$mail->addAddress('user@gmail.com', 'Xion-Solutions');
$mail->setFrom('user@gmail.com', 'Xion-Solutions');
$mail->addReplyTo($email, $name);
$mail->Subject = 'Xion-Solutions';
$mail->SMTPDebug = 2;

Why roll your own validator? PHPMailer has a better one built in that you can call statically, but it's applied everywhere it accepts addresses automatically anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM