🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 发送电子邮件 几乎每个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界面的类。