2016-10-28 16 views
0

PHPMailerを使用して添付ファイル付きのSMTP保護メールを送信しようとしています。PhpMailerでSMTP接続に問題が発生しました

私はライン$ mail-> isSMTPを()コメントする場合、私はphpmailerのライブラリ

public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){ 
      require getcwd() .'/lib/PHPMailerAutoload.php'; 
      $mail = new PHPMailer; 

      //$mail->SMTPDebug = 3;        // Enable verbose debug output 

      $mail->isSMTP();          // Set mailer to use SMTP 

      $mail->Host = 'smtp.gmail.com'; 
      $mail->SMTPAuth = true;        
      $mail->Username = '[email protected]';     
      $mail->Password = '**************';       
      $mail->SMTPSecure = 'ssl';        
      $mail->Port = 465;        

      $mail->setFrom($from_mail, $from_name); 
      $mail->addAddress($mail_to); 
      $mail->addReplyTo($replyto, 'no reply'); 

      $mail->addAttachment($path);   
      $mail->isHTML(true);         

      $mail->Subject = $subject; 

      $mail->Body = $body; 
      //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

      if(!$mail->Send()) { 
       $error = 'Mail error: '.$mail->ErrorInfo; 
       echo $error; 
      } else { 
       $sucess = 'Mail sent!'; 
       echo $sucess; 
      } 
     } 

でこの機能を作りました。それは正常に動作しています。しかし、私はそれがSMTPで保護されていないと思う。 "メールエラー:SMTP接続()に失敗しました。"

私はこの問題を調査しましたが、私が探しているものが正しい答えを得られませんでした。私を助けてください。私はhtaccessファイルの保護とのdevのサーバーで働いています

+0

あなたは、古いバージョンを使っていますPHPMailerの[docs](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting)を読む必要があります。また、投稿前に検索すると、この質問の複製がたくさんあります。 – Synchro

+0

[SMTP connect()がphpmailerに失敗しました](http://stackoverflow.com/questions/30476024/smtp-connect-failed-phpmailer) – Synchro

答えて

0

は、まあ、私はここにhttps://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting問題を解決するための詳細を発見しました。

私はちょっと安全性の低いアプリを私のGmailアカウントからhttps://www.google.com/settings/security/lesssecureappsにしました。

アクティブにするには数分または1時間かかります。

私の現在のコード:セットアップにあなたが持っているそうで

public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){ 
      require getcwd() .'/lib/PHPMailerAutoload.php'; 
      $mail = new PHPMailer(); 

      $mail->isSMTP(); 
      $mail->Host = 'smtp.gmail.com'; 
      //$mail->SMTPDebug = 2;        
      $mail->SMTPAuth = true; 
      $mail->SMTPSecure = 'tls';        
      $mail->Port = 587; 

      $mail->Username = '[email protected]';     
      $mail->Password = 'mypass';          


      $mail->setFrom($from_mail, $from_name); 
      $mail->addAddress($mail_to); 
      $mail->addReplyTo($replyto, 'no reply'); 

      $mail->addAttachment($path);   
      $mail->isHTML(true);         

      $mail->Subject = $subject; 

      $mail->Body = $body; 
      //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

      if(!$mail->Send()) { 
       $error = 'Mail error: '.$mail->ErrorInfo; 
       echo $error; 
      } else { 
       $sucess = 'Mail sent!'; 
       echo $sucess; 
      } 
     } 

console.developers.google.com

からのアプリは、このガイドに従ってください:https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2

関連する問題