0309(一)-網頁前端設計與開發運用培訓班
===
###### tags: `德鍵`
---
一、寄信機制
1. PHP可以透過mail()來寄信,前提是主機本身要設定好寄信機制,不管是用SMTP或sendmail其語法如下:
```php
mail($收信人的Email, $主旨, $信件內容, $headers);
```
2. 一般而言,寄信內容是純文字的,若要使用網頁語法,那麼必須設定`$headers`
```php
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
```
3. 若要帶附檔,則需要先將檔案編碼,或者直接改用PHPMailer來處理。
二、xampp設定寄信
1. 修改 \xampp\php\php.ini
搜尋 [mail function]
```php
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = 您的gmail信箱
sendmail_path = "\"D:\ugm\xampp\sendmail\sendmail.exe\" -t"
```
2. 修改 \xampp\sendmail\sendmail.ini
```
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=您的gmail信箱
auth_password=gmail密碼
force_sender=您的gmail信箱
```
3. 管理低安全性應用程式的存取權
https://myaccount.google.com/lesssecureapps
4. mail.php
```php
<?php
$to_email = "ugm158@gmail.com";
$subject = "寄信範本" . date("Y-m-d H:i:s",strtotime("now"));
$body = "信件內容";
// $headers = "From: sender\'s email";
$headers = 'From: service@dj1.ugm.one' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
if(mail($to_email, $subject, $body, $headers)){
echo "Email successfully sent to {$to_email}..." . "<br/>";
echo $subject;
}else{
echo "Email sending failed...";
}
```
5. sendToLine
lineNotify:https://notify-bot.line.me/zh_TW/
```php
send_notify_curl($message, $token);
function send_notify_curl($message, $token) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://notify-api.line.me/api/notify",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => http_build_query(array("message" => $message),'','&'),
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $token",
"Content-Type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
return $response;
}
}
```
6. 去除bom:removeBOM.php
```php
<?php
//remove the utf-8 boms
//by magicbug at gmail dot com
if (isset($_GET['dir'])){ //config the basedir
$basedir=$_GET['dir'];
}else{
$basedir = '.';
}
$auto = 1;
checkdir($basedir);
/**
* [checkdir 檢查目錄]
* @param [type] $basedir [description]
* @return [type]
* @date 2017-10-29T19:01:11+0800
*/
function checkdir($basedir)
{
if ($dh = opendir($basedir)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..'){
if (!is_dir($basedir."/".$file)) {
echo "filename $basedir/$file ".checkBOM("$basedir/$file")." <br>";
}else{
$dirname = $basedir."/".$file;
checkdir($dirname);
}
}
}
closedir($dh);
}
}
/**
* [checkBOM 檢查檔案BOM]
* @param [type] $filename [description]
* @return [type]
* @date 2017-10-29T19:01:19+0800
*/
function checkBOM ($filename)
{
global $auto;
$contents = file_get_contents($filename);
$charset[1] = substr($contents, 0, 1);
$charset[2] = substr($contents, 1, 1);
$charset[3] = substr($contents, 2, 1);
if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
if ($auto == 1) {
$rest = substr($contents, 3);
rewrite ($filename, $rest);
return ("<font color=red>BOM found, automatically removed.</font>");
} else {
return ("<font color=red>BOM found.</font>");
}
}
else return ("BOM Not Found.");
}
/**
* [rewrite 移除檔頭BOM]
* @param [type] $filename [description]
* @param [type] $data [description]
* @return [type]
* @date 2017-10-29T19:01:40+0800
*/
function rewrite ($filename, $data)
{
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
```
7.