整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

PHP-使用PHP发送电子邮件

PHP-使用PHP发送电子邮件

须在php.ini文件中正确配置PHP ,并详细说明系统如何发送电子邮件。打开/ etc /目录中的php.ini文件,并找到标题为[邮件功能]的部分。

Windows用户应确保提供了两个指令。第一个称为SMTP,它定义您的电子邮件服务器地址。第二个称为sendmail_from,它定义您自己的电子邮件地址。

Windows的配置应如下所示:

[mail function]
; For Win32 only.
SMTP=smtp.secureserver.net

; For win32 only
sendmail_from=webmaster@tutorialspoint.com

Linux用户只需要让PHP知道他们sendmail应用程序的位置即可。路径和任何所需的开关应指定给sendmail_path指令。

Linux的配置应该看起来像这样-

[mail function]
; For Win32 only.
SMTP=; For win32 only
sendmail_from=; For Unix only
sendmail_path=/usr/sbin/sendmail -t -i

现在您可以开始了-

发送纯文本电子邮件

PHP利用mail()函数发送电子邮件。此功能需要三个必填参数,用于指定收件人的电子邮件地址,邮件主题和实际邮件,此外还有其他两个可选参数。

mail( to, subject, message, headers, parameters );

这是每个参数的说明。


一旦调用了邮件功能,PHP将尝试发送电子邮件,如果成功,它将返回true;如果失败,则将返回false。

可以将多个收件人指定为逗号分隔列表中mail()函数的第一个参数。

发送HTML电子邮件

当您使用PHP发送文本消息时,所有内容将被视为简单文本。即使您将HTML标签包含在文本消息中,它也将显示为简单文本,并且不会根据HTML语法设置HTML标签的格式。但是PHP提供了将HTML消息作为实际HTML消息发送的选项。

发送电子邮件时,您可以指定Mime版本,内容类型和字符集来发送HTML电子邮件。

以下示例将HTML电子邮件发送到xyz@somedomain.com,并将其复制到afgh@somedomain.com。您可以使用以下方式对程序进行编码:它应接收用户的所有内容,然后发送电子邮件。

<html>
   
   <head>
      <title>Sending HTML email using PHP</title>
   </head>
   
   <body>
      
      <?php
         $to="xyz@somedomain.com";
         $subject="This is subject";
         
         $message="<b>This is HTML message.</b>";
         $message .="<h1>This is headline.</h1>";
         
         $header="From:abc@somedomain.com \r\n";
         $header .="Cc:afgh@somedomain.com \r\n";
         $header .="MIME-Version: 1.0\r\n";
         $header .="Content-type: text/html\r\n";
         
         $retval=mail ($to,$subject,$message,$header);
         
         if( $retval==true ) {
            echo "Message sent successfully...";
         }else {
            echo "Message could not be sent...";
         }
      ?>
      
   </body>
</html>

通过电子邮件发送附件

要发送包含混合内容的电子邮件,需要将Content-type标头设置为multipart / mixed。然后可以在边界内指定文本和附件节。

边界以两个连字符开头,后跟一个唯一的数字,该数字不能出现在电子邮件的消息部分。PHP函数md5()用于创建32位十六进制数字以创建唯一数字。表示电子邮件最后部分的最后边界也必须以两个连字符结尾。

者 | Mateusz Iwaniuk

译者 | 明明如月,责编 | 夕颜

出品 | CSDN(ID:CSDNnews)

文章配套代码: https://github.com/iwaniukooo11/email-sender

现在,即使是创建最基本的网站,程序员也必须使用现代的功能和技术。甚至像为你的朋友创建简单的投资组合这样的基本项目也可能涉及到一些问题,比如从联系人表单接收数据。有很多方法可以读取这些数据。你可以将表单与数据库连接起来,然后从数据库中读取传入的消息来实现功能,但这样做会给不懂技术的客户造成困难。

你为什么不通过发送电子邮件传输信息?

不使用数据库就能接收到传入的消息,绝对是最佳选择,也是最方便用户的选择。但问题来了—如何实现呢?你可能认为需要使用某种后端语言。

实际上,你不必使用任何如 php 或 python 这种后端语言,你甚至不需要用到 node.js!你需要的就是一个简单的EmailJS 库。

本文将介绍下面两个重要功能:

  • 配置 emailjs 帐户

  • 使用 JS 发送电子邮件

请注意,在我的项目中,我使用了 gulp 和 webpack,我在 src 文件夹存放源码,dist 存放最终发布版本的代码。

我将分 5 个步骤向你展示如何从头开始构建电子邮件发送器。

步骤1-用 HTML 创建表单

首先需要创建一个 HTML 表单。你不必放置像 required 或 max 这种验证属性,因为稍后,preventDefault 函数将在你的提交事件上运行,它会让这些属性的处理失效。

表单中最重要的是为每个输入放置 name 属性,后面会用到。

我的非常简单的表单是这样的:

src/html/index.html

 <form class="form"> <input name='name' type="text" placeholder="Your name..." class="form__input" /> <input name='topic' type="text" placeholder="Topic..." class="form__input" /> <textarea name='message' type="text" placeholder="Your Message..." class="form__input" ></textarea>

<input type="submit" value="send" class="form__input form__input--button"> </form>

步骤2-注册成为 email 用户

要配置你的电子邮件,你必须注册电子邮件服务。别担心—使用这个网站非常方便和省时。

登入后,系统会询问你的电子邮件服务,它位于个人电子邮件服务区(personal email service)。在我的例子中,我选择了 gmail。

然后,你需要连接你的 gmail 帐户。这将用来发送电子邮件给你客户。例如,如果你关联了 xyz@gmail.com 账户,你后续发送的邮件都将从这个邮箱发出。所以不要担心“ Send email on your behalf” 这个授权信息—这正是你需要的!

连接完 gmail 账户后,点击添加服务(add service)按钮。

步骤3-创建邮件模板

如果你已经成功连接了你的 gmail 账户,你现在应该在信息中心中。现在需要创建电子邮件模板了。

切换到电子邮件模板卡,并单击创建一个新的模板(create a new template)。界面非常友好,所以创建模板不会有任何问题。

你可以选择模板的名称和 ID。我称之为“我的神奇模板(my_amazing_template)”。

接下来,你必须指定邮件的内容。

模板的变量值来自 input 中的 `name` 属性。你已将变量插入`{{{}}}`符号中。

不要忘记在“收件人”部分 (右侧) 添加电子邮件地址。你的电子邮件将被发送到该电子邮件地址上。截图中的收件人邮箱是我自己的公司邮箱。

这是我的简单模板,它使用来自 HTML 表单里的 3 个变量。我还指定了接收电子邮件的主题。

步骤4-保存 API 密钥

这部分没什么特别的。Emailjs 共享授权 API 密钥,将在发送电子邮件时使用。当然,放这些钥匙最好的地方是`.env` 配置。但是因为我使用的是简单的静态文件,我不想使用服务器配置,所以我将它们保存在 apikeys 文件中,然后再将它们导入。

你的 USER_ID 位于 Account > API Keys 菜单下。

TEMPLATE_ID 位于模板的标题下面。

这是我基于不存在的 keyssrc / js / apikeys. js 的示例配置.

src/js/apikeys.js

export default { USER_ID :'user_DPUd-rest-of-my-id', TEMPLATE_ID:'my_amazing_template'}

如果需要将源码发布到 GITHUB,不要忘记将 APIKEYS 文件添加到 .GITIGNORE文件中

步骤5-发送电子邮件

现在是该项目最后也是最重要的部分的了。现在我们必须使用 javascript 发送电子邮件。

首先,你必须下载 emailjs 包。

npm i emails-com

然后,转到 js 文件,导入库和 apikeys。

src/js/main.js

import emailjs from 'emailjs-com'import apiKeys from './apikeys'

现在是编写发送电子邮件功能的时候了

src/js/main.js

const sendEmail=e=> { e.preventDefault

emailjs .sendForm('gmail', apiKeys.TEMPLATE_ID, e.target, apiKeys.USER_ID) .then( result=> { console.log(result.text) }, error=> { console.log(error.text) } )}

sendForm 函数有4个参数:

你的电子邮件的 ID,在这里:

TEMPLATE_ID 来自 apikey 文件,

事件对象来自你的表单提交

USER_ID 来自 apikey 文件,

最后,查找表单并添加提交事件监听器:

src/js/main.js

const form=document.querySelector('.form')form.addEventListener('submit',sendEmail)

正如我前面提到的,由于 `preventDefault` 函数,属性验证将无法工作。你必须使用 JS 自己进行验证和清除输入。

以上就是全部内容,接下来让我们测试一下。

填写页面上的表单并发送。

我收到电子邮件,内容正是根据我们的模板和表单数据渲染出来的。

通过上图可以看出,所有的变量的值都填充到了正确的位置上。

总结

通过本文的介绍你会发现用 JS 发送邮件并非难事。

使用 emailjs,你可以简单的方式发送电子邮件。

我相信你未来的用户会很高兴收到来自他们网页上表单填写数据的t邮件,相信本文对你有帮助。

这篇文章的配套代码在这里: https://github.com/iwaniukooo11/email-sender

原文链接:

https://dev.to/iwaniukooo11/send-e-mails-directly-from-front-end-with-js-5d7d

本文为CSDN翻译文章,转载请注明出处。

?我们想研发一个机器学习框架,6 个月后失败了

?生产型机器学习已经没那么困难了?

?视频 | 你不知道的"开源"60年秘史

?GitHub标星10,000+,Apache项目ShardingSphere的开源之路

?阿里技术专家告诉你,如何画出优秀的架构图?

?加拿大API平台如何做到30%为中国明星项目?创业老兵这样说……

wish I could remember the very first email message I ever sent. Unfortunately, the truth is that I’ve come to take email for granted; for instance, I easily send more email messages than I make phone calls. Because the novelty has long since worn off, the memory of that first email is as lost to me as my first phone call; but I doubt my sense of wonder was any less complete at the time. To someone who has never seen email in action before, that first message can be magical.

我希望我能记得我发送的第一封电子邮件。不幸的是,事实是我已经把电子邮件视为理所当然;例如,我很容易发送比打电话更多的电子邮件。因为新奇感早已消失,我对第一封电子邮件的记忆就像第一次打电话一样消失了;但我怀疑当时我的惊奇感是否不够完整。对于一个以前从未见过电子邮件的人来说,第一条消息可能很神奇。

In this article, I’ll try to recapture some of that magic for those of you who have never created a Web site that sends email messages. We’ll see how the PHP server-side scripting language may be set up to send email, and explore how to send complex message types such as HTML email or emails with file attachments.

在这篇文章中,我将尝试为那些从未创建过发送电子邮件的网站的人重温其中的一些魔力。我们将了解如何设置PHP服务器端脚本语言来发送电子邮件,并探索如何发送复杂的消息类型,如HTML电子邮件或带有文件附件的电子邮件。

PHP Email Setup

PHP电子邮件步骤

Before we can send email with PHP, we need to set it up to do so, just as you need to set up your email program before it can send messages. Configuration for sending email in PHP is done with the php.ini file, so open up your Web server’s php.ini in whichever editor you normally use.

在我们用PHP发送电子邮件之前,我们需要对它进行设置,就像您需要在它发送消息之前设置电子邮件程序一样。用PHP发送电子邮件的配置是通过PHP.ini文件完成的,所以在您通常使用的编辑器中打开Web服务器的PHP.ini。

If you don’t run your own server, but instead have a PHP-equipped Web host, you can safely assume that everything in this section has been done for you, and skip ahead.

如果您没有运行自己的服务器,而是有一个配备了PHP的Web主机,那么您可以放心地假设本节中的所有内容都已为您完成,然后跳过。

In the section entitled [mail function] in the php.ini file, you’ll find three settings: SMTP, sendmail_from, and sendmail_path. If your server runs on a Windows machine, you’ll want to set the SMTP option to point to your SMTP server (or your ISP’s SMTP server, if you’re setting up PHP on your home machine). If instead you’re setting up PHP on a Linux (or other Unix-based OS) server, you’ll want to set the sendmail_path option to point to the sendmail program on your server, passing it the -t option. You can use the SMTP option in Linux instead if you don’t have sendmail set up.

在php.ini文件中标题为[mail function]的部分中,您将找到三个设置:SMTP、sendmail_from和sendmail_path。如果您的服务器在Windows计算机上运行,则需要将SMTP选项设置为指向您的SMTP服务器(或ISP的SMTP服务器,如果您在家庭计算机上设置PHP)。如果要在Linux(或其他基于Unix的操作系统)服务器上设置PHP,则需要将sendmail_path选项设置为指向服务器上的sendmail程序,并将-t选项传递给它。如果没有设置sendmail,则可以在Linux中使用SMTP选项。

In either case, you’ll want to set the sendmail_from option to your email address, or whichever address you’d like to appear as the default ‘from’ address for emails sent from PHP scripts.

在任何一种情况下,您都需要将sendmail_from选项设置为您的电子邮件地址,或者您希望显示为从PHP脚本发送的电子邮件的默认“from”地址的任何地址。

Here’s how the section might look on a typical Windows server, or on a Linux server without sendmail:

以下是该部分在典型Windows服务器上的外观,或在没有sendmail的Linux服务器上的展示:

[mail function]
; Setup for Windows systems
SMTP=smtp.my.isp.net
sendmail_from=me@myserver.com
And here’s how it might look on a Linux server with sendmail:

[mail function]
; Setup for Linux systems
sendmail_path=/usr/sbin/sendmail -t
sendmail_from=me@myserver.com
With those settings in place, restart your Web server and you’re ready to go!

Sending Plain Email

发送普通电子邮件

It doesn’t come much easier than the procedure to send plain text email in PHP. In fact, you can do it in just one line in a PHP script:

用PHP发送纯文本电子邮件的过程并不容易。事实上,您可以在PHP脚本的一行中完成:

<?php
mail('recipient@some.net', 'Subject', 'Your message here.');
?>

The above line would send an email message to recipient@some.net with ‘Subject‘ as the subject line and ‘Your message here.‘ as the message body. As you can see, PHP’s mail function makes sending email exceedingly simple, but there are a few advanced tricks we can use to get more out of this simple function.

上述行将向发送电子邮件recipient@some.net以“Subject”作为标题,以“Your message here.”作为邮件正文。正如您所看到的,PHP的邮件函数使发送电子邮件变得非常简单,但是我们可以使用一些高级技巧来从这个简单的函数中获得更多信息。

First of all, if the mail system you configured in your php.ini file rejects a message you try to send (for example, if the ‘to’ address is not a valid email address), this function will display an error message in the user’s browser. Like most PHP functions, however, error messages may be suppressed by preceding the function name with an @ symbol. Combine this with the fact that the mail function returns either true or false depending on whether the email was accepted by the mail sending system, and you have a formula to send email with appropriate error checking:

首先,如果您在php.ini文件中配置的邮件系统拒绝您尝试发送的邮件(例如,如果“to”地址不是有效的电子邮件地址),此函数将在用户浏览器中显示错误消息。但是,与大多数PHP函数一样,可以通过在函数名前面加上@符号来抑制错误消息。再加上邮件函数根据邮件发送系统是否接受邮件而返回true或false,并且您有一个发送电子邮件并进行适当错误检查的公式:

<?php
if (@mail($to, $subject, $message)) {
echo('<p>Mail sent successfully.</p>');
} else {
echo('<p>Mail could not be sent.</p>');
}
?>

Note that just because an email message could be sent doesn’t guarantee it will arrive at its destination. An email address can be valid (i.e. correctly formed) but not actually exist. For instance, you can successfully send a message to nonexistant.user@hotmail.com — that is, the mail function will return true — but the message will bounce because no such user exists. PHP provides no built-in means of detecting when this happens.

请注意,仅仅因为可以发送电子邮件并不保证它会到达目的地。电子邮件地址可能有效(即格式正确),但实际上不存在。例如,您可以成功地向不存在的用户发送消息。user@hotmail.com-也就是说,邮件函数将返回true-但邮件将反弹,因为不存在这样的用户。PHP没有提供检测何时发生这种情况的内置方法。

When you want to send a message to multiple recipients, you can just list their email addresses one after another, separated by commas, in the first parameter. For example:

当您想向多个收件人发送邮件时,您可以在第一个参数中逐个列出他们的电子邮件地址,并用逗号分隔。例如: