使用mailtrap整合php邮件php是当今最流行的网络开发编程语言之一。公司向用户发送邮件,通知他们新产品,如促销邮件或与员工沟通。
在本教程中,我们看看如何在我们的php中集成流行的mailtrap平台来发送多封邮件。【推荐学习:php视频教程】
前提条件要跟随本教程,你需要具备以下条件。
php的基本概念,最好是php8.0。
简单邮件传输协议(smtp)的基本概念。
一个mailtrap账户。
目标在本教程结束时,你应该能够将mailtrap集成到你的php应用程序中,以测试电子邮件。
开始使用mailtrap开发广泛的应用程序有很多要求。这包括要求测试你的应用程序,以确保一切按计划进行。这些应用的关键要求之一,如edge as a service,是测试邮件功能的能力。
科的客户经常利用edge作为服务的好处,他们有一个灵活的付款计划。为了提醒这些客户的下一个到期日,我们需要向他们发送电子邮件。
发送电子邮件的一个关键挑战是,我们不确定我们的电子邮件是否被交付。为了确保电子邮件被送达,我们需要在开发和阶段性水平上测试我们的电子邮件,以确保它们在生产中运行良好。
现在,mailtrap随着应用程序开发过程的开发和暂存阶段的到来而出现。它被用来测试电子邮件,以确保它们被送到预定的收件人手中。在下一节中,我们将深入了解php的邮件发送方法,它们是如何工作的,以及它们可能面临的问题。
php内置的邮件发送方法在php中,我们有2种不同的方法来给我们的系统用户发送邮件。
这些方法是。
通过使用php包,我们将在下一节看到。
使用内置的方法。
在这一节中,我们将使用php的mail() 方法来给我们的用户发送邮件。然后,我们将继续检查这些邮件是否被送达或失败。
mail() 的一般结构如下所示。
// the mail method in php for sending emailsmail( // recipient email string $to, // the email subject string $subject, // the email body string $message, //any other additional settings array|string $additional_headers = [], string $additional_params = ""): bool
上述方法接收了多个参数,描述如下。
$to :这个参数指的是电子邮件的收件人。这可以是这样的: 。test@section.io
$subject:这指的是电子邮件的主题,你必须确保它符合rfc 2047 - mime(多用途互联网邮件扩展)。
$message:这是你的邮件正文。我们需要确保每一行都用crlf(\r\n)分隔。行数不应大于70个字符,否则邮件将不会被发送。
$additional_headers (optional)- 这是一个数组参数,确保我们可以在邮件标题中添加额外的信息。这可能包括cc ,bcc 等。
现在我们了解了php的mail() 方法的基本功能,让我们继续向一些随机的电子邮件发送一封样本邮件。
<?php// sending to$to = 'no-reply@section.io';// email subject$subject = "section's edge as a service";// additional headers$headers = array( 'from' => 'test@example.com', 'reply-to' => 'test2@example.com', 'x-mailer' => 'php/' . phpversion());//body template$message = '<html><head> <title>node.js deployment</title></head><body> <p>i have a few requests:</p> <ol> <li>how much is the cost?</li> <li>what is the whole procedure of delpoyment</li> <li>how are my appplications distributed?</li> <li>how flexible is the payment plans?</li> </ol></body></html>';mail($to, $subject, $message, $headers);
在上面的代码中,我们正在向一个随机的电子邮件发送一封询问邮件。我们已经定义了html主体,并添加了额外的参数,如标题。
注意:重要的是要记住,要使用html主体向用户发送电子邮件,我们必须设置我们的标题,如下图所示。
$headers[] = 'mime-version: 1.0';$headers[] = 'content-type: text/html; charset=iso-8859-1';
否则,我们的邮件正文就会以html的形式传递。当我们的传输协议遇到错误的内容时,可能会出现其他问题。在这一点上,我们假设当这个特定的代码被运行时,我们期望它能完美无缺地运行。
然而,我们怎样才能确保我们的电子邮件被送到预定的收件人手中呢?在下一节中,让我们用一个邮件包来发送同样的电子邮件。这些包将帮助我们克服mail() 方法的局限性,这种方法在检查我们的邮件是否被送达时相当困难。
php邮件包前面的电子邮件发送方法的一个关键缺点是,它的特点或功能非常有限。这通常是在需要发送大量邮件时面临的问题。
在本节中,我们将研究如何克服这些缺点,并随后分析我们的电子邮件是否发展到了预期的收件人。
我们将讨论以下软件包。
phpmailer
swift mailer
梨子邮件
让我们继续,先从phpmailer :phpmailer是我们上面列出的所有包中最流行的用php发送邮件的包之一。
创建一个php文件mail.php ,并添加以下代码片段。
<?php// import the mailer classuse phpmailer\phpmailer\phpmailer;require_once './vendor/autoload.php';// create a new mailing object$mail = new phpmailer();// smtp configuration$phpmailer = new phpmailer();$phpmailer->issmtp();$phpmailer->host = 'smtp.mailtrap.io';$phpmailer->smtpauth = true;$phpmailer->port = 2525;$phpmailer->username = 'cb7xx33e1856xxx5b25xx';$phpmailer->password = '87f63xx87d73e52xxx4xx';$mail->setfrom('no-reply@section.io', 'node.js deployment');$mail->addaddress('test@gmail.com', 'me');$mail->subject = 'thanks for using section.io edge as a service!';// our html setup$mail->ishtml(true);$mail->body = '<html>hello johndoe, thank you for using our node.js deployment and distribution platform. kinldy check the document in the attachment below to review your payments plan.</html>';$mail->altbody = 'success';// adding mailing attachment for payment plan$mail->addattachment('//node/paymments.pdf', 'payments.pdf');// send the thank you messangeif(!$mail->send()){ echo 'your message could not be develired, try again later'; echo 'error: ' . $mail->errorinfo;} else { echo 'your message has been sent successfully.';}
在上面的代码中,我们已经安装了phpmailer包。我们还创建了这个类的一个新实例,$mail 。接下来,我们已经创建了我们的mailtrap账户,并在这里抓取了凭证。
当你创建一个项目时,确保你将其与phpmailer 选项集成,如下面的截图所示。
你会注意到,我们的截图省略了用户名和密码。这些是自动生成的,对每个用户都是不同的。
接下来,我们设置了我们的setfrom() 方法来接收发件人的电子邮件和电子邮件标题。然后,我们继续配置收件人的电子邮件地址和电子邮件的主题。
注意:之前,我们曾表示,我们可以将正文添加为html,然后适当地设置我们的内容类型。
在上面的邮件正文中,我们将信息定义为html,以便我们能够定制邮件,满足我们的要求。然后我们添加替代标签,再最后添加一个附件。然后,我们使用phpmailer的$mail->send() 方法来发送我们的邮件。我们加入了if 语句来检查我们的邮件是否已经发送。
当我们的邮件未能送达时,我们通过打印一个警告信息来通知用户,否则就打印一个成功信息。让我们继续使用swiftmailer ,实现同样的功能,如下所示。
在你的服务器上创建一个新的文件swift.php ,并添加以下代码片段。
<?phprequire_once './vendor/autoload.php'; try { // start by creating smtp transport $transport = (new swift_smtptransport('smtp.mailtrap.io', 2525)) ->setusername('xxxxxxxxx') ->setpassword('xxxxxxxxx'); $swift_mailer = new swift_mailer($transport); // message creation $swift_message = new swift_message(); $swift_message->setsubject('hooray! you just deployed your first node'); swift_message->setfrom(['no-reply@section.io' => 'saas']); $messswift_messageage->addto('test@gmail.com','test'); // adding email attachment $email_attachment = swift_attachment::frompath('./section/payments.pdf'); $swift_message->attach($email_attachment); // set the plain-text part $swift_message->setbody('hello john doe, thank you for using the section node deployment service'); // set the html part $swift_message->addpart('we are glad to welcome you on board'); // send the message $res = swift_mailer->send($message);} catch (exception $e) { echo $e->getmessage();}
就像phpmailer一样,我们首先安装这个包,并使用./vendor/autoload.php 路径导入它。还需要注意的是,根据你的系统设置,这个路径可能与你的应用程序路径不同。
接下来,我们将传输设置为使用我们mailtrap的swift_smtptransport 。拿起你的凭证,按照上面的代码设置。按照前面的步骤来配置你的应用程序,使其使用mailtrap包来发送邮件。
现在,我们如何知道我们的邮件已经被送达?这就是我们使用mailrap的原因。与phpmail() 方法相比,该软件包允许我们配置我们的应用程序使用mailtrap,这给我们提供了一个平台来测试我们的应用程序,正如下一节所讨论的。
使用mailtrap测试电子邮件登录你的mailtrap账户,进入你的收件箱部分,如以下截图所示。
接下来,点击项目名称,展开你所发送的邮件。
注意:为了安全起见,上述截图上的一些功能已被跳过。
总结在这篇文章中,我们已经广泛地讨论了php邮件方法的基本概念。我们已经看到了php内置的方法mail() 是如何限制我们发送带有测试功能的邮件的,我们已经用php包克服了这个问题。
作者:debugusery
链接:https://juejin.cn/post/7167615841398161416
以上就是聊聊mailtrap怎么整合php邮件的详细内容。