php 发送自动电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17384494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending an automated email
提问by TheQuantumBros
I would like to have an automated email sent to a person's email when they press a button. I'm having a few problems, one I am not sure what the JavaScript for this would be. Two, I don't know how to mix both PHP and JavaScript, since I need to use PHP to get their email address and password from an SQL Table, and as far as I know, I need JavaScript to send the email. Finally, I am not sure how to add the password from the PHP into the email. Help on any of these would be greatly appreciated. Thanks.
我想在某人按下按钮时自动向他们的电子邮件发送电子邮件。我遇到了一些问题,其中一个我不确定用于此的 JavaScript 是什么。二,我不知道如何混合使用 PHP 和 JavaScript,因为我需要使用 PHP 从 SQL 表中获取他们的电子邮件地址和密码,据我所知,我需要 JavaScript 来发送电子邮件。最后,我不确定如何将 PHP 中的密码添加到电子邮件中。对任何这些方面的帮助将不胜感激。谢谢。
回答by ObiVanKaPudji
Simplest way to do that:
最简单的方法:
Make HTML form on your page:
<form method="POST" action="email-script.php"> <label for="email">Email:</label> <input type="text" name="email" id="email" /> <input type="submit" value="Ok" /> </form>
Write PHP code in your email-script.php file:
<?php $email=$_POST['email']; $subject = 'Your subject for email'; $message = 'Body of your message'; mail($email, $subject, $message); ?>
在您的页面上制作 HTML 表单:
<form method="POST" action="email-script.php"> <label for="email">Email:</label> <input type="text" name="email" id="email" /> <input type="submit" value="Ok" /> </form>
在您的 email-script.php 文件中编写 PHP 代码:
<?php $email=$_POST['email']; $subject = 'Your subject for email'; $message = 'Body of your message'; mail($email, $subject, $message); ?>
When you master this simple method, you can use more advanced method with AJAX (so you don't need to reload page) or SMTP (which gives you more control over sending mails to users).
当您掌握了这个简单的方法后,您可以使用更高级的方法与 AJAX(这样您就不需要重新加载页面)或 SMTP(这使您可以更好地控制向用户发送邮件)。
回答by Filip
You can send an email through PHP, but most of the answers on SO saying "use the mail() function" are outdated I presume and, in any case, do not work.
您可以通过 PHP 发送电子邮件,但是我认为关于“使用 mail() 函数”的大多数答案已经过时,并且在任何情况下都不起作用。
I would advise you to use JavaScript or simply an HTML form collecting a user ID or user email. Then pass that on as a parameter to your post function that calls the PHP you want to run.
我建议您使用 JavaScript 或简单的 HTML 表单来收集用户 ID 或用户电子邮件。然后将其作为参数传递给调用要运行的 PHP 的 post 函数。
In the PHP file, you read the email (for example, via $_POST['email']). Then you run a SQL query to retrieve the password WHERE the email is equal to the one you just read.
在 PHP 文件中,您阅读电子邮件(例如,通过 $_POST['email'])。然后您运行 SQL 查询以检索密码,其中电子邮件与您刚刚阅读的密码相同。
Finally, since you now have the password stored in a variable, you continue in your PHP file and use PHPMailer's function, as shown in examples like the following: https://github.com/PHPMailer/PHPMailer/blob/master/examples/mail.phps. Alternatively, you could use a similar library to achieve the same functionality.
最后,由于您现在已将密码存储在变量中,因此您可以在 PHP 文件中继续使用 PHPMailer 的函数,如以下示例所示:https: //github.com/PHPMailer/PHPMailer/blob/master/examples/邮件.phps。或者,您可以使用类似的库来实现相同的功能。
Then you put the password in your email text and send it to the user.
然后您将密码放入电子邮件文本并将其发送给用户。
However, a note of caution: it would be very unwise in terms of security to send passwords in plain text format (not encrypted) to a user's email address. I would advise you to send them an email with a password reset link instead, which would direct them to a form where they could select a new password and confirm it. That would require a different PHP file, where you would execute an UPDATE query to change the password that is stored in the database.
但是,请注意:以纯文本格式(未加密)向用户的电子邮件地址发送密码在安全性方面是非常不明智的。我建议您向他们发送一封带有密码重置链接的电子邮件,这会将他们定向到一个表单,他们可以在其中选择新密码并进行确认。这将需要一个不同的 PHP 文件,您将在其中执行 UPDATE 查询以更改存储在数据库中的密码。
I assume you have already solved your problem, since this is an old question, but maybe this will help someone else who stumbles upon it.
我假设你已经解决了你的问题,因为这是一个老问题,但也许这会帮助其他人偶然发现它。
回答by Kyle
I have done something similar to this in the past and the best way to do this is to create a HTML form. Then use php to get all of the data and still use that php to send the email. No javascript required. I can recommend these links
我过去做过类似的事情,最好的方法是创建一个 HTML 表单。然后使用 php 获取所有数据,并仍然使用该 php 发送电子邮件。不需要 javascript。我可以推荐这些链接
Why do you need to access the data from the SQL table? Is this a password recovery type of form? If so, that can also be done with php. However, given that this is a Q&A help site and not a forum, I would hire a developer.
为什么需要访问SQL表中的数据?这是一种密码恢复类型的表格吗?如果是这样,那也可以用 php 来完成。但是,鉴于这是一个问答帮助网站而不是论坛,我会聘请开发人员。
This linkcan help with accessing a mysql database from php.
此链接有助于从 php 访问 mysql 数据库。
This linkcan help with sending php email.
此链接可以帮助发送 php 电子邮件。
This linkcan help with sending html based email from php.
此链接可以帮助从 php 发送基于 html 的电子邮件。
回答by Chris Bode
You may want to investigate AJAX via jQuery.
您可能想通过 jQuery 调查 AJAX。
AJAX allows you to open a PHP script within javascript, and then do things with whatever the php script echos back.
AJAX 允许您在 javascript 中打开 PHP 脚本,然后使用 php 脚本回显的任何内容执行操作。
If you do not mind navigating away from the interface page, an HTML form would work.
如果您不介意离开界面页面,HTML 表单也可以。
回答by msturdy
Sounds like you're a little confused, JavaScript will not be any help for the sending of the email..
听起来您有点困惑,JavaScript 对发送电子邮件没有任何帮助。
..perhaps it would help you to think about it like this:
..也许它会帮助你这样思考:
a HTML page with a link (
<a>
) that refers to your php script:`<a href="email-script.php">Send Email</a>`
a (separate) PHP script (called
email-script.php
in the example above) that uses one of the many mail libraries (swiftmailer, phpmailer) to send the email.the php script should then return the user to the original page.. with a message to tell the user that their email was sent
带有指向
<a>
您的 php 脚本的链接 ( )的 HTML 页面:`<a href="email-script.php">Send Email</a>`
一个(单独的)PHP 脚本(
email-script.php
在上面的示例中调用),它使用许多邮件库(swiftmailer、phpmailer)之一来发送电子邮件。php 脚本然后应该将用户返回到原始页面.. 用一条消息告诉用户他们的电子邮件已发送
There are a lot of tutorials for sending email from PHP, and those links above are to pages with example code.
有很多关于从 PHP 发送电子邮件的教程,上面的链接是带有示例代码的页面。