使用 php 在邮件中发送附件-ag捕鱼王app官网

当前位置:ag捕鱼王app官网 > > 编程语言 > php >

使用 php 在邮件中发送附件

作者:迹忆客 最近更新:2023/03/29 浏览次数:

我们将介绍在 php 中通过电子邮件发送附件的不同方法。


使用 phpmailer 在电子邮件中发送附件

我们可以使用 phpmailer 类来发送电子邮件,允许我们发送附件。我们可以创建一个 phpmailer 类对象并使用其方法和属性将电子邮件发送给所需的收件人。我们将使用 gmail 发送电子邮件。因此,我们将使用 smtp 协议。该库具有 addattachment() 方法,可让我们添加附件。首先,我们需要从 下载库。

例如,创建一个文件夹 src 并将三个文件 phpmailer.phpsmtp.phpexception.php 复制到其中。然后创建一个文件 index.php 并使用 require 语句来包含这三个文件。然后使用这些文件的相应类。接下来,创建 phpmailer() 类的对象 $mail。使用 usernamepassword 属性设置发件人的电子邮件和密码。使用 subjectbody 属性设置电子邮件的主题和正文。使用 addattachment() 函数添加附件。将附件的相对路径作为方法的参数。在 addaddress() 方法中写入收件人的电子邮件。最后,调用 send() 方法发送电子邮件。接下来,调用 smtpclose() 关闭 smtp 连接。

我们需要将发件人的电子邮件更改为使用 phpmailer 中的 gmail 发送电子邮件。我们应该在 gmail 中打开不太安全的应用程序访问选项以使用 phpmailer。然后,运行以下脚本将向收件人发送电子邮件和附件。

示例代码:

php
require 'src/phpmailer.php';
require 'src/smtp.php';
require 'src/exception.php';
use phpmailer\phpmailer\phpmailer;
use phpmailer\phpmailer\smtp;
use phpmailer\phpmailer\exception;
$mail = new phpmailer();
$mail->issmtp();
$mail->host = "smtp.gmail.com";
$mail->smtpauth = "true";
$mail->smtpsecure ="tls";
$mail->port = "587";
$mail->username = "sendersemail@gmail.com";
$mail->password = "password";
$mail->setfrom('sendersemail@gmail.com');
$mail->subject = 'message subject';
$mail->body = "this is a body text";
$mail->addattachment('attachments/project.pdf');
$mail->addaddress( 'receiversmail@gmail.com' );
$mail->send();
$mail->smtpclose();
?>

使用 swiftmailer 在电子邮件中发送附件

我们也可以使用第三方库 swiftmailer 发送带有附件的电子邮件。该库提供了一个 attach() 方法来在发送电子邮件时添加附件。我们可以使用以下命令安装库。

composer require "swiftmailer/swiftmailer:^6.0"

我们需要在我们的脚本中包含 autoloader.php 文件以使用 swiftmailer。该文件位于下载文件的供应商文件夹内。我们将使用 gmail 发送电子邮件。要使用 gmail,我们需要使用 smtp 协议。因此,我们需要使用 swift_smtptransport 类创建传输以设置主机、端口号和协议。我们可以使用传输设置发件人的电子邮件和密码。swift_mailer 类允许我们设置传输,而 swift_mailer 类允许我们设置消息、收件人和附件。

例如,要求工作文件中的 autoload.php 文件为 vendor/autoload.php。创建 swift_smtptransport 类的对象 $transport,并将主机设置为 smtp.gmail.com,端口号设置为 587,安全协议设置为 tls。然后使用 setusernamesetpassword 方法设置发件人的电子邮件和密码。接下来,创建 swift_mailer 类的对象 $mail 并将 $transport 对象设置为它。然后,创建 swift_message 类的另一个对象 $content 并将主题写入作为参数。使用 setfrom()setto() 方法指定发件人的电子邮件和收件人的电子邮件。在 setbody() 方法中编写电子邮件正文。然后,使用 attach() 方法通过 swift_attachment 类的 frompath() 方法指定附件路径。最后,使用我们创建的 $mail 对象通过 send() 方法发送电子邮件。提供 $content 对象作为 send() 方法的参数。

这就是我们如何使用 php 中的 swiftmailer 库发送带有附件的电子邮件。

示例代码:

require_once 'vendor/autoload.php';
$transport = (new swift_smtptransport('smtp.gmail.com', 587, 'tls'))
->setusername('sendersemail@gmail.com')
->setpassword('password')
$mail = new swift_mailer($transport);
$content = (new swift_message('subject'))
->setfrom(['sendersemail@gmail.com' => 'senders name'])
->setto('recieversemail@gmail.com')
->setbody('this is a text')
->attach(swift_attachment::frompath('attachments/project.pdf'));
$result = $mail->send($content);

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 php 中获取时间差的分钟数

发布时间:2023/03/29 浏览次数:204 分类:php

本文介绍了如何在 php 中获取时间差的分钟数,包括 date_diff()函数和数学公式。它包括 date_diff()函数和数学公式。

发布时间:2023/03/29 浏览次数:156 分类:php

本教程演示了如何将用户从页面重定向到 php 中的其他页面

php 分页

发布时间:2023/03/29 浏览次数:95 分类:php

本教程介绍如何在 php 中对数据库行进行分页

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图