1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > php实现国外邮箱 发送电子邮件 · 国外PHP框架Nette官网教程 · 看云

php实现国外邮箱 发送电子邮件 · 国外PHP框架Nette官网教程 · 看云

时间:2020-08-06 21:24:47

相关推荐

php实现国外邮箱 发送电子邮件 · 国外PHP框架Nette官网教程 · 看云

# 发送电子邮件

几乎每个Web应用程序都需要发送电子邮件,无论是简报还是订单确认。 这就是为什么Nette Framework提供必要的工具。 本教程将向您介绍如何:

**创建电子邮件

发送电子邮件

电子邮件添加附件

在电子邮件中使用模板

电子邮件创建链接**

使用Nette \ Mail \ Message类创建电子邮件的示例:

~~~

use Nette\Mail\Message;

$mail = new Message;

$mail->setFrom('John ')

->addTo('peter@')

->addTo('jack@')

->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@');

$mail->setFrom('john.doe@', '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@')

->setHtmlBody($latte->renderToString('email.latte', $params));

~~~

文件email.latte:

~~~

Order Confirmation

body {

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' => '',

'username' => 'john@',

'password' => '*****',

'secure' => 'ssl',

]);

$mailer->send($mail);

~~~

您还可以创建自己的邮件程序 - 它是一个实现Nette \ Mail \ IMailer界面的类。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。