[CentOS7] install PHPMailer & prob

環境 CentOS 7



PHPMailer 載點 https://github.com/Synchro/PHPMailer 



我下指令 git clone https://github.com/Synchro/PHPMailer.git 取得



Mailer 範例:




  • PHPMailer範例

    <?php

    include(“PHPMailerAutoload.php"); //匯入PHPMailer類別 注意 此php要和class等檔案放同層目錄

    $mail= new PHPMailer(); //建立新物件

    $mail->IsSMTP(); //設定使用SMTP方式寄信

    $mail->SMTPAuth = true; //設定SMTP需要驗證

    $mail->SMTPSecure = “ssl"; // Gmail的SMTP主機需要使用SSL連線

    $mail->Host = “smtp.gmail.com"; //Gamil的SMTP主機

    $mail->Port = 465; //Gamil的SMTP主機的SMTP埠位為465埠。

    $mail->CharSet = “utf-8″; //設定郵件編碼

    $mail->Username = “xxx@gmail.com"; //設定google eamil帳號

    $mail->Password = “*********"; //設定google eamil密碼

    $mail->From = “xxx@gmail.com"; //設定寄件者信箱

    $mail->FromName = “測試人員"; //設定寄件者姓名

    $mail->Subject = “PHPMailer 測試信件"; //設定郵件標題

    $mail->Body = “這是一封測試信件! “; //設定郵件內容

    $mail->IsHTML(true); //設定郵件內容為HTML

    $mail->AddAddress(“xxx@xxx.xxx.xxx", “"); //設定收件者郵件及名稱

    if(!$mail->Send()) {

    echo “Mailer Error: " . $mail->ErrorInfo;

    } else {

    echo “Message sent!";

    }

    ?>

  • from http://info.stes.tc.edu.tw/?p=267



SMTP connect() failed   &   Failed to connect to server: Permission denied (13)



可能原因有二(當然不只,此僅列出本人解決方案):



登入 Gmail 帳號後連結https://www.google.com/settings/security/lesssecureapps 需要開啟



https://gistpages.com/posts/phpmailer_smtp_error_failed_to_connect_to_server_permission_denied_13_fix



-> 一些系統上的布林值沒設好



# code below is from the web above



First, try to out put the settings you currently have:(確定你目前的設定是否為以下狀況)



$ getsebool httpd_can_sendmail
httpd_can_sendmail --> off
$ getsebool httpd_can_network_connect
httpd_can_network_connect --> off


If you get something similar, you should set these settings on.(如果是的話,你應該將設定開啟)



$ setsebool -P httpd_can_sendmail 1
$ setsebool -P httpd_can_network_connect 1


If you get error messages like:(如果遇到了些錯誤訊息)



Cannot set persistent booleans without managed policy.
Could not change policy booleans


You may have the need to run these commands as root and you may need to sudo.(你應該需要以root的身分下一些指令,用用sudo吧)



$ sudo setsebool -P httpd_can_sendmail 1
$ sudo setsebool -P httpd_can_network_connect 1


Now, try sending emails using your script!(開始用你的script寄信吧!)



留言

這個網誌中的熱門文章

[Antergos] disable touchpad