# 發送電子郵件
幾乎每個Web應用程序都需要發送電子郵件,無論是簡報還是訂單確認。 這就是為什么Nette Framework提供必要的工具。 本教程將向您介紹如何:
**創建電子郵件
發送電子郵件
電子郵件添加附件
在電子郵件中使用模板
電子郵件創建鏈接**
使用Nette \ Mail \ Message類創建電子郵件的示例:
~~~
use Nette\Mail\Message;
$mail = new Message;
$mail->setFrom('John <john@example.com>')
->addTo('peter@example.com')
->addTo('jack@example.com')
->setSubject('Order Confirmation')
->setBody("Hello, Your order has been accepted.");
~~~
發送:
~~~
use Nette\Mail\SendmailMailer;
$mailer = new SendmailMailer;
$mailer->send($mail);
~~~
除了使用addTo()指定收件人之外,還可以使用addCc()和副本的收件人addBcc()指定副本的收件人。 在所有這些方法中,包括setFrom(),我們可以通過三種方式指定地址:
~~~
$mail->setFrom('john.doe@example.com');
$mail->setFrom('john.doe@example.com', 'John Doe');
$mail->setFrom('John Doe <john.doe@example.com>');
~~~
HTML內容可以使用setHtmlBody()方法定義:
~~~
$mail->setHtmlBody('<b>Sample HTML</b> <img src="background.gif">');
~~~
嵌入的圖像可以使用$ mail-> addEmbeddedFile('background.gif')插入,但是沒有必要。 為什么? 因為Nette Framework會自動找到并插入HTML代碼中引用的所有文件。 此行為可以通過添加FALSE作為setHtmlBody()方法的第二個參數來抑制。
如果HTML電子郵件沒有純文本替代項,則會自動生成。 如果沒有設置主題,它將從<title>元素中獲取。
## 附件
向電子郵件添加附件很簡單。 為了附加一個文件,我們使用addAttachment方法:
~~~
// attaches example.zip to the e-mail
$mail->addAttachment('path/to/example.zip');
// attaches new example.txt file with "Hello John!" in it
$mail->addAttachment('example.txt', 'Hello John!');
// attaches example.zip renamed to info.zip
$mail->addAttachment('info.zip', file_get_contents('path/to/example.zip'));
~~~
## 模板
結合了Latte模板系統:
~~~
$latte = new Latte\Engine;
$params = [
'orderId' => 123,
];
$mail = new Message;
$mail->setFrom('John <john@example.com>')
->addTo('jack@example.com')
->setHtmlBody($latte->renderToString('email.latte', $params));
~~~
文件email.latte:
~~~
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Order Confirmation</title>
<style>
body {
background: url("background.png")
}
</style>
</head>
<body>
<p>Hello,</p>
<p>Your order number {$orderId} has been accepted.</p>
</body>
</html>
~~~
## 創建鏈接
Latte Engine本身不提供{link}宏。 如果我們想使用它,我們可以通過調用$ template = $ this-> createTemplate() - > setFile('email.latte')并將它添加到setHtmlBody中來使用一個Presenter或一個組件來為我們創建模板 )。 這樣,我們可以在模板中創建鏈接。
~~~
use Nette;
class MyPresenter
{
public function actionFoo()
{
$template = $this->createTemplate()->setFile('email.latte');
$mail = new Message();
$mail->setHtmlBody($template);
}
}
~~~
另一個選擇是自己注冊宏。 因為生成鏈接需要一個控制器,我們將它作為參數傳遞給模板:
~~~
use Nette;
use Nette\Bridges\ApplicationLatte\UIMacros;
function createMessage(Nette\Application\Application $application)
/** @var Nette\Application\IPresenter */
$presenter = $application->getPresenter();
$latte = new Latte\Engine;
$params = [
'orderId' => 123,
'_presenter' => $presenter, // because of {plink}
'_control' => $presenter // because of {link}
];
UIMacros::install($latte->getCompiler()); // This registers macros link, plink and others
// ...
}
~~~
在模板中,創建鏈接像在普通模板中。 我們使用絕對地址,所以鏈接總是指向正確的域:
~~~
<a href="{link //Presenter:action}">Odkaz</a>
~~~
## 自定義郵件
默認郵件程序使用PHP功能郵件。 如果您需要通過SMTP服務器發送郵件,您可以使用SmtpMailer。
~~~
$mailer = new Nette\Mail\SmtpMailer([
'host' => 'smtp.gmail.com',
'username' => 'john@gmail.com',
'password' => '*****',
'secure' => 'ssl',
]);
$mailer->send($mail);
~~~
您還可以創建自己的郵件程序 - 它是一個實現Nette \ Mail \ IMailer界面的類。
- Nette簡介
- 快速開始
- 入門
- 主頁
- 顯示文章詳細頁
- 文章評論
- 創建和編輯帖子
- 權限驗證
- 程序員指南
- MVC應用程序和控制器
- URL路由
- Tracy - PHP調試器
- 調試器擴展
- 增強PHP語言
- HTTP請求和響應
- 數據庫
- 數據庫:ActiveRow
- 數據庫和表
- Sessions
- 用戶授權和權限
- 配置
- 依賴注入
- 獲取依賴關系
- DI容器擴展
- 組件
- 字符串處理
- 數組處理
- HTML元素
- 使用URL
- 表單
- 驗證器
- 模板
- AJAX & Snippets
- 發送電子郵件
- 圖像操作
- 緩存
- 本土化
- Nette Tester - 單元測試
- 與Travis CI的持續集成
- 分頁
- 自動加載
- 文件搜索:Finder
- 原子操作