Frequently Asked Question

How do I send out emails in PHP script? 我如何使用PHP script發送電子郵件?
Last Updated 7 years ago

There are several methods for use PHP to send email which are mail() function of PHP, sendmail, SMTP. However, in order to send email with better email headers and format, we highly recommmend you to use a open source library - PHPMailer

Here are the procedures to use PHPMailer:
1. Download PHPMailer from http://sourceforge.net/projects/phpmailer
2. Decompress the PHPMailer package
3. Rename the folder extracted to "phpmailer"
4. Upload the whole folder to your website via FTP
5. Below PHP script is an example to send email using PHPMailer, you could modify and embed in your PHP scripts according to your need.

使用PHP可以有多種方法發出電話, 包括PHP 內建的 mail() 功能, sendmail, SMTP等. 然而, 我們建議使用開源碼程式庫 - PHPMailer

這是使用PHPMailer的有關步驟:
1. 從 http://sourceforge.net/projects/phpmailer 下載 PHPMailer
2. 把 PHPMailer 壓縮包解壓
3. 把解壓縮後的資料夾改名為 "phpmailer"
4. 透過 FTP 把整個資料夾上載到你的網站內
5. 以下是利用 PHPMailer 發送電郵的 PHP 程式碼例子, 你可以根據你的需要修改並嵌入到你的 PHP 程式內.



include_once('phpmailer/_lib/class.phpmailer.php');
$mail = new PHPMailer();

$mail->CharSet = "UTF-8"; // Charset of email
$mail->From = "name@yourdomain.com"; // Sender email
$mail->FromName = "First Last"; // Sender name
$mail->Sender = $mail->From;
$mail->AddAddress("whoto@otherdomain.com", "John Doe"); // Recipient email & name
$mail->AddCC("whotocc@otherdomain.com"); // CC Recipient email & name
$mail->AddBCC("whotobcc@otherdomain.com"); // BCC Recipient email & name
$mail->Subject = "PHPMailer Test Subject"; // Subject
$mail->Body = "Hi,
This is the HTML BODY
"; // HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; // Text Body
$mail->AddAttachment("/path/to/image.jpg"); // attachment
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML

if(!$mail->Send()) {
echo 'Failed to send mail, mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Mail sent';
}

?>

Please Wait!

Please wait... it will take a second!