PHPMailer – Sending PHP Emails via SMTP

Some hosting service don’t allow users to use PHP to send emails, so need to apply SMTP service like Gmail.

    private $smtp = true;
    private $local = false;

    function __construct() {
		$this->mail= new PHPMailer();
        $this->mail->From = '[email protected]';
        $this->mail->FromName = 'no-reply';
        $this->mail->IsHTML(true);

        //SMTP
        if($this->smtp == true){
            if($this->local == true){
                $this->mail->isSMTP();
                $this->mail->Host = 'smtp.gmail.com';
                $this->mail->SMTPAuth = true;
                $this->mail->Username = '[email protected]';
                $this->mail->Password = 'xxxxxxx';
                $this->mail->SMTPSecure = 'tls';
                $this->mail->Port = 587;
                $this->mail->SMTPDebug = false;
            }else{
                $this->mail->isSMTP();
                $this->mail->Host = 'mail.example.com';
                $this->mail->SMTPAuth = true;
                $this->mail->Username = '[email protected]';
                $this->mail->Password = 'xxxxxx';
                //$this->mail->SMTPSecure = 'tls';
                $this->mail->CharSet = "utf-8";
                $this->mail->Encoding = "base64";
                $this->mail->Port = 25;
                $this->mail->SMTPDebug = false;
            }
            
        }
    }

Note:

The “Password” for gmail SMTP is not real password of your account, it’s an “Application password” instead. Reference: https://support.google.com/accounts/answer/185833

Reference:

Note: ServerZoo don’t allow any outside SMTP service even Gmail. Here is the solution: http://forum.serverzoo.com/showthread.php?t=3582