# 郵件發送
### [](https://octobercms.com/docs/services/mail#sending-mail)發送郵件
要發送消息,請使用外觀`send`上的方法,該方法`Mail`接受三個參數。第一個參數是用于查找[郵件視圖](https://octobercms.com/docs/services/mail#mail-views)或[郵件模板](https://octobercms.com/docs/services/mail#mail-templates)的唯一*郵件代碼*。第二個參數是您希望傳遞給視圖的數據數組。第三個參數是一個回調,它接收消息實例,使您可以自定義郵件的收件人,主題和其他方面:[](https://octobercms.com/docs/services/mail#mail-views)[](https://octobercms.com/docs/services/mail#mail-templates)`Closure`
~~~
// These variables are available inside the message as Twig
$vars = ['name' => 'Joe', 'user' => 'Mary'];
Mail::send('acme.blog::mail.message', $vars, function($message) {
$message->to('admin@domain.tld', 'Admin Person');
$message->subject('This is a reminder');
});
~~~
由于`name`在上面的示例中傳遞了包含鍵的數組,因此可以使用以下Twig標記在電子郵件視圖中顯示值:
~~~
{{ name }}
~~~
> **注意:**您應該避免`message`在郵件中傳遞變量,該變量始終會傳遞并允許[附件](https://octobercms.com/docs/services/mail#attachments)的[內聯嵌入](https://octobercms.com/docs/services/mail#attachments)。
#### 快速發送
十月還包括一個稱為的替代方法`sendTo`,可以簡化郵件的發送:
~~~
// Send to address using no name
Mail::sendTo('admin@domain.tld', 'acme.blog::mail.message', $params);
// Send using an object's properties
Mail::sendTo($user, 'acme.blog::mail.message', $params);
// Send to multiple addresses
Mail::sendTo(['admin@domain.tld' => 'Admin Person'], 'acme.blog::mail.message', $params);
// Alternatively send a raw message without parameters
Mail::rawTo('admin@domain.tld', 'Hello friend');
~~~
中的第一個參數`sendTo`用于收件人可以采用不同的值類型:
| 類型 | 描述 |
| --- | --- |
| 串 | 單個收件人地址,未定義名稱。 |
| 數組 | 多個收件人,其中數組鍵是地址,值是名稱。 |
| 目的 | 單個收件人對象,其中*email*屬性用于地址,*名稱*可選用于名稱。 |
| 采集 | 如上所述,收件人對象的集合。 |
的完整簽名`sendTo`如下:
~~~
Mail::sendTo($recipient, $message, $params, $callback, $options);
~~~
* `$recipient`如上定義。
* `$message`是原始發送的模板名稱或消息內容。
* `$params`模板內部提供的變量數組。
* `$callback`會使用一個參數`send`(方法描述的消息生成器)被調用(可選,默認為null)。如果不是可調用的值,則代替下一個options參數。
* `$options`作為數組傳遞的自定義發送選項(可選)
`$options`支持以下自定義發送
* **queue**指定是對消息進行排隊還是直接發送(可選,默認為false)。
* **BCC**指定是否添加收件人作為密件抄送或經常要地址(默認為false)。
#### 建立訊息
如前所述,該`send`方法的第三個參數是`Closure`允許您在電子郵件本身上指定各種選項。使用此閉包,您可以指定消息的其他屬性,例如復本,盲目復本等:
~~~
Mail::send('acme.blog::mail.welcome', $vars, function($message) {
$message->from('us@example.com', 'October');
$message->to('foo@example.com')->cc('bar@example.com');
});
~~~
這是`$message`消息生成器實例上可用方法的列表:
~~~
$message->from($address, $name = null);
$message->sender($address, $name = null);
$message->to($address, $name = null);
$message->cc($address, $name = null);
$message->bcc($address, $name = null);
$message->replyTo($address, $name = null);
$message->subject($subject);
$message->priority($level);
$message->attach($pathToFile, array $options = []);
// Attach a file from a raw $data string...
$message->attachData($data, $name, array $options = []);
// Get the underlying SwiftMailer message instance...
$message->getSwiftMessage();
~~~
> **注意:**傳遞給`Mail::send`Closure的消息實例擴展了[SwiftMailer](http://swiftmailer.org/)消息類,使您可以調用該類上的任何方法來構建電子郵件。
#### 郵寄純文本
默認情況下,`send`假定提供給該方法的視圖包含HTML。但是,通過將數組作為方法的第一個參數`send`傳遞,除了HTML視圖外,您還可以指定要發送的純文本視圖:
~~~
Mail::send(['acme.blog::mail.html', 'acme.blog::mail.text'], $data, $callback);
~~~
或者,如果只需要發送純文本電子郵件,則可以使用`text`數組中的鍵進行指定:
~~~
Mail::send(['text' => 'acme.blog::mail.text'], $data, $callback);
~~~
#### 郵寄已解析的原始字符串
`raw`如果您希望直接通過電子郵件發送原始字符串,則可以使用該方法。Markdown將解析此內容。
~~~
Mail::raw('Text to e-mail', function ($message) {
//
});
~~~
此外,Twig將解析此字符串,如果您希望將變量傳遞到此環境,請改用`send`方法,將內容作為`raw`鍵。
~~~
Mail::send(['raw' => 'Text to email'], $vars, function ($message) {
//
});
~~~
#### 郵寄原始字符串
如果傳遞包含`text`或`html`鍵的數組,這將是發送郵件的顯式請求。不使用布局或降價解析。
~~~
Mail::raw([
'text' => 'This is plain text',
'html' => '<strong>This is HTML</strong>'
], function ($message) {
//
});
~~~
### [](https://octobercms.com/docs/services/mail#attachments)附件
要將附件添加到電子郵件中,請使用傳遞給Closure`attach`的`$message`對象上的方法。該`attach`方法接受文件的完整路徑作為其第一個參數:
~~~
Mail::send('acme.blog::mail.welcome', $data, function ($message) {
//
$message->attach($pathToFile);
});
~~~
在將文件附加到消息時,您還可以通過將an`array`作為第二個參數傳遞給`attach`方法來指定顯示名稱和/或MIME類型:
~~~
$message->attach($pathToFile, ['as' => $display, 'mime' => $mime]);
~~~
### [](https://octobercms.com/docs/services/mail#inline-attachments)內聯附件
#### 將圖像嵌入郵件內容
將嵌入式圖像嵌入到電子郵件中通常很麻煩;但是,有一種簡便的方法可以將圖像附加到電子郵件并檢索適當的CID。若要嵌入嵌入式圖像,請在電子郵件視圖中`embed`的`message`變量上使用方法。請記住,該`message`變量可用于所有郵件視圖:
~~~
<body>
Here is an image:
<img src="{{ message.embed(pathToFile) }}">
</body>
~~~
如果您打算使用排隊的電子郵件,請確保文件的路徑是絕對路徑。為此,您可以簡單地使用[應用過濾器](https://octobercms.com/docs/markup/filter-app):
~~~
<body>
Here is an image:
{% set pathToFile = 'storage/app/media/path/to/file.jpg' | app %}
<img src="{{ message.embed(pathToFile) }}">
</body>
~~~
#### 將原始數據嵌入郵件內容
如果您已經有想要嵌入到電子郵件中的原始數據字符串,則可以`embedData`對`message`變量使用方法:
~~~
<body>
Here is an image from raw data:
<img src="{{ message.embedData(data, name) }}">
</body>
~~~
### [](https://octobercms.com/docs/services/mail#queueing-mail)排隊郵件
#### 排隊郵件
由于發送郵件消息會大大延長應用程序的響應時間,因此許多開發人員選擇將消息排隊等待后臺發送。使用內置的[統一隊列API](https://octobercms.com/docs/services/queues)可以輕松實現。要使郵件排隊,請使用外觀`queue`上的方法`Mail`:
~~~
Mail::queue('acme.blog::mail.welcome', $data, function ($message) {
//
});
~~~
此方法將自動處理將作業推入隊列以在后臺發送郵件的情況。當然,在使用此功能之前,您將需要[配置隊列](https://octobercms.com/docs/services/queues)。
#### 延遲的消息排隊
如果您希望延遲發送排隊的電子郵件,則可以使用該`later`方法。首先,只需將您希望延遲發送消息的秒數作為方法的第一個參數傳遞:
~~~
Mail::later(5, 'acme.blog::mail.welcome', $data, function ($message) {
//
});
~~~
#### 推送到特定隊列
如果希望指定要在其上推送消息的特定隊列,則可以使用`queueOn`和`laterOn`方法來這樣做:
~~~
Mail::queueOn('queue-name', 'acme.blog::mail.welcome', $data, function ($message) {
//
});
Mail::laterOn('queue-name', 5, 'acme.blog::mail.welcome', $data, function ($message) {
//
});
~~~
- 基本說明
- 基本操作
- October cms 安裝
- 后臺控制器路徑
- 圖標
- 獲取安裝網上的插件/主題
- 插件構造器使用
- 定時任務
- October后臺控制器
- vscode編輯器
- ajax操作
- 使用
- ajax更新組件
- ajax屬性API
- JavaScript API
- ajax綜合使用
- 主題
- 多語言主題
- 安裝市場主題
- 主題程序處理
- 主題
- 頁面
- 部件
- 布局
- 內容
- 組件
- 媒體
- 主題表單操作
- 表單使用
- 表單后端程序處理
- 插件
- 自定義插件
- 插件說明
- 插件導航條
- 插件數據庫設置
- 插件的設置管理
- 插件的配置文件config
- 組件
- app服務
- app容器
- 擴展行為
- 緩存
- Collection類
- Lazy Collections
- Collection方法
- 助手函數
- 數組助手函數
- 路徑助手函數
- 玄樂助手函數
- 其他助手函數
- 錯誤與記錄
- 事件處理
- HTML頁面
- 文件與目錄操作
- 散列和加密
- 郵件
- 郵件內容
- 郵件發送
- 分頁
- 模板解析器
- 動態解析器語法
- 隊列消息
- 請求與輸入
- 響應
- 視圖
- 路由器
- 配置
- 驗證操作
- 處理錯誤消息
- 錯誤消息與視圖
- 可用的驗證規則
- 有條件的驗證規則
- 驗證數組
- 錯誤消息
- 自定義驗證規則
- 模型操作
- 定義模型與其屬性
- 檢索模型
- 插入與更新
- 刪除模型
- 查詢范圍
- 事件操作
- 關聯操作
- 定義關系
- 關系類型
- 多肽關系
- 關系查詢
- 渴望加載
- 插入模型
- 數據庫操作
- 基本用法
- 數據表結構
- 查詢連貫操作
- 結果檢索
- select子句
- 插入更新
- where子句
- 排序,分組,限制和偏移
- 文件附件
- Collection操作
- 屬性操作
- 系列化json
- 數據庫屬性
- 數據庫行為
- 控制器
- 后臺控制器定義
- 后臺頁面
- 后臺組件
- 后臺表單
- 表單組件
- 表單視圖
- 表單行為
- 后臺列表
- 列表行為
- 列表過濾器
- 可用列類型
- 關系行為
- 關系行為類型
- 擴展關系行為
- 列表排序操作
- 導入導出操作
- 用于與權限
- corlate模板修改
- 修改頂部導航
- laravel問題
- 控制器不存在
- 控制器
- 路由組
- laravel筆記
- laravel 安裝
- 偽靜態配置
- 依賴注入 & 控制器
- 中間件
- 路由文件
- 視圖