PHP字串長度找子字串與取代字串的常用函數

PHP在字串的操作上,有非常多的相關函數可以使用,我們在這裡只介紹經常會使用到的字串操作函數。像是如何取得字串的長度、在某一個字裏面尋找子字串首次出現的位置、以及字串的取代函數...等等。這些都是在編寫程式的時候,頻繁被程式設計師使用到的基礎函數。 取得字串長度 <?...

顯示具有 Swiftmailer 標籤的文章。 顯示所有文章
顯示具有 Swiftmailer 標籤的文章。 顯示所有文章

2015年4月30日 星期四

實用Swift Mailer類庫用來發送php電子郵件

        Swift Mailer是一款功能特別強大的PHP物件導向的發送電子郵件的類別函式庫。它可以讓你透過內建的mail()函數、SMTP、postfix、甚至是你自己客製化的通訊協定來達到寄送email的目的。還可以有效的阻擋email的表頭遭到注入式攻擊,真的確是一個簡單容易使用的PHP郵件發送類庫。下載連結:http://swiftmailer.org/download

一個簡單發送SMTP Gmail電子郵件的範例

<?php
require_once 'swiftmailer-5.x/lib/swift_required.php';
 
// 採用預設的mail()數函發email
// $transport = Swift_MailTransport::newInstance();

// 使用SMTP的方式來發送gmail
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername('Your username')
  ->setPassword('Your password')
  ;
 
// 填寫email的主旨、內容、寄件者、收件者 
$message = Swift_Message::newInstance();  

$message->setTo(array(
  "jason@gmail.com" => "Jason.Terry"
  //,"abc@yahoo.com" => "Mike.Porter"
));
$message->setSubject("Swift Mail測試中...");
$message->setBody("恭喜您! Swift測試郵件發送成功。");
$message->setFrom("swiftmailer@gamil.com", "Swift-mailer");
 
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);

echo 'Send Swift mail successfully!!';