完成上一篇的php檔案之後我才發現,另一家公司配合的中華電信主機,因為垃圾郵件氾濫的問題,沒有提供php sendmail傳遞資料的服務,所以要使用PHPMailer來發送電子郵件。
PHPMailer是一套功能強大的的Mail套件
可到這裡下載GitHub:https://github.com/PHPMailer/PHPMailer
再用composer安裝phpmailer,但是我實在看不懂啊!
還好在網路上搜尋到可以不使用composer的方法,
只要到上述網址下載檔案,解壓縮之後將資料夾放入專案目錄,
再修改一下語法(已修改)就可以使用了喔! (注意路徑要正確就是了)
<?php
//移除Composer語法
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
//use PHPMailer\PHPMailer\PHPMailer;
//use PHPMailer\PHPMailer\SMTP;
//use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
//require 'vendor/autoload.php';
//新增
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//設定檔案路徑
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
//建立物件
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8'; // 設置郵件內容的編碼為 UTF-8
try {
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->SMTPDebug = 0; // DEBUG訊息
$mail->isSMTP(); // 使用SMTP
$mail->Host = 'smtp.hibox.biz'; // SMTP server 位址
$mail->SMTPAuth = true; // 開啟SMTP驗證
$mail->Username = 'sales@fidelity-bio.com.tw'; // email帳號
$mail->Password = 'Fb83493557'; // email密碼
//$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; PHPMailer::ENCRYPTION_SMTPS also accepted
//$mail->SMTPSecure = "ssl"; // Gmail要透過SSL連線
$mail->Port = 25; // SMTP TCP port
// 從表單中獲取值
$name = $_POST['name'];
$email = $_POST['email'];
$institution = $_POST['institution'];
$message = $_POST['message'];
//設定收件人資料
$mail->setFrom('sales@fidelity-bio.com.tw'); // 寄件人(透過Gmail發送會顯示Gmail帳號為寄件者)
$mail->addAddress('sales@fidelity-bio.com.tw'); // 收件人會顯示 Apple <apple@example.com'>User<apple@example.com>(*註2)
// $mail->addAddress('banana@example.com'); // 名字非必填
// $mail->addReplyTo('info@example.com', 'Information'); //回信的收件人非必填
// $mail->addCC('cc@example.com'); //副本
// $mail->addBCC('bcc@example.com'); //密件副本
// 信件內容
$mail->isHTML(true); // 設定為HTML格式
$mail->Subject = 'Message from Fidelity Website'; // 信件標題
$mail->Body = "Name:".$name."<br />Email:".$email."<br />Institution:".$institution."<br />Message:".$message; //郵件內容 // 信件內容
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; // 對方若不支援HTML的信件內容
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
留言列表