php实现国外邮箱,发送电子邮件 · 国外PHP框架Nette官网教程 · 看云
# 發送電子郵件
幾乎每個Web應用程序都需要發送電子郵件,無論是簡報還是訂單確認。 這就是為什么Nette Framework提供必要的工具。 本教程將向您介紹如何:
**創建電子郵件
發送電子郵件
電子郵件添加附件
在電子郵件中使用模板
電子郵件創建鏈接**
使用Nette \ Mail \ Message類創建電子郵件的示例:
~~~
use Nette\Mail\Message;
$mail = new Message;
$mail->setFrom('John ')
->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 ');
~~~
HTML內容可以使用setHtmlBody()方法定義:
~~~
$mail->setHtmlBody('Sample HTML ');
~~~
嵌入的圖像可以使用$ mail-> addEmbeddedFile('background.gif')插入,但是沒有必要。 為什么? 因為Nette Framework會自動找到并插入HTML代碼中引用的所有文件。 此行為可以通過添加FALSE作為setHtmlBody()方法的第二個參數來抑制。
如果HTML電子郵件沒有純文本替代項,則會自動生成。 如果沒有設置主題,它將從
元素中獲取。## 附件
向電子郵件添加附件很簡單。 為了附加一個文件,我們使用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 ')
->addTo('jack@example.com')
->setHtmlBody($latte->renderToString('email.latte', $params));
~~~
文件email.latte:
~~~
Order Confirmationbody {
background: url("background.png")
}
Hello,
Your order number {$orderId} has been accepted.
~~~
## 創建鏈接
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
// ...
}
~~~
在模板中,創建鏈接像在普通模板中。 我們使用絕對地址,所以鏈接總是指向正確的域:
~~~
Odkaz
~~~
## 自定義郵件
默認郵件程序使用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界面的類。
總結
以上是生活随笔為你收集整理的php实现国外邮箱,发送电子邮件 · 国外PHP框架Nette官网教程 · 看云的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 淘宝总知道你要什么?万字讲述智能内容生成
- 下一篇: php 随机生成邮箱,php 生成随机帐