使用javascript和PHP发送电子邮件的简单方法 [英] Simple way to send email with javascript and PHP

查看:167
本文介绍了使用javascript和PHP发送电子邮件的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够从用户那里获取电子邮件地址,然后使用php向他们发送一个带有链接的电子邮件。
我知道有一些使用jquery或AJAX的实现,但是我没有任何一个主题的经验。我希望一些简单的东西。谢谢

I need to be able to fetch an email address from the user and then use php to send them an email with a link. I know there are some implementations using jquery or AJAX but i have no experience in either of those topics. I was hoping for something simple. thanks

推荐答案

我需要从用户那里获取电子邮件地址

"I need to be able to fetch an email address from the user"

创建一个表单:

<form action="emailHandler.php" method="POST" id="emailForm">
    <label for="emailInput">Email: </label>
    <input type="text" name="emailInput" id="emailInput" value="" />
    <input type="submit" id="submitEmail" value="Submit Email" />
</form>

然后使用php向他们发送一封带有链接的电子邮件

"and then use php to send them an email with a link"

表单中的提交按钮将POST输入字段的值发送到PHP脚本emailHandler.php

The submit button within the form will POST the value of the input field to the PHP script emailHandler.php

我知道有一些实现使用jquery或AJAX,但我没有在任何一个主题的经验

"I know there are some implementations using jquery or AJAX but i have no experience in either of those topics"

您不需要jQuery或AJAX为此,jQuery和AJAX是JavaScript主题(在在这种情况下,AJAX是关于让您从HTML获取值,然后将其POST到PHP后端,并接收一个JSON对象,该对象将告诉javascript是否成功,这在这里是不需要的,但它可以是使用),您可以简单地使用内置的PHP邮件功能: http://php.net /manual/en/function.mail.php

You don't need jQuery or AJAX for this, jQuery and AJAX are javascript topics (in which case, AJAX is about having you fetched the value from the HTML, then POST them to a PHP backend, and receive a JSON object which will tell the javascript whether it was successful or not, this is obiously NOT REQUIRED here but it CAN be used), you can simply use the built in PHP mail function: http://php.net/manual/en/function.mail.php

在emailHandler.php。

in emailHandler.php.

我喜欢这样做:

function spamcheck($field)
{
    //filter_var() sanitizes the e-mail
    //address using FILTER_SANITIZE_EMAIL
    $field=filter_var($field, FILTER_SANITIZE_EMAIL);

    //filter_var() validates the e-mail
    //address using FILTER_VALIDATE_EMAIL
    if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
        return true;
    }
    else
    {
        return false;
    }
}

function sendMail($toEmail, $fromEmail, $subject, $message)
{
    $validFromEmail = spamcheck($fromEmail);
    if($validFromEmail)
    {
        mail($toEmail, $subject, $message, "From: $fromEmail");
    }
}

在emailHandler.php中执行类似这样的操作: / p>

And do something like this in emailHandler.php:

$email = isset($_POST['emailInput']) ? $_POST['emailInput'] : false;

if($email != false)
{
    $yourEmail = "example@example.com";.
    $subject = "Link";
    $message = "The link and some message";
    $success = sendMail($email, $yourEmail, $subject, $message);
}

在某些情况下,您必须修改PHP ini文件,您可以像这样:

In some cases you have to modify the PHP ini file, you can do it like this:

ini_set('SMTP' , 'smtp.example.com');
ini_set('smtp_port' , '25');
ini_set('username' , 'example@example.com');
ini_set('password' , 'password');
ini_set('sendmail_from' , 'example@example.com');

我希望简单一些,谢谢

如果这不简单,那我不知道是什么。如果您不想使用jQuery和Ajax,请在线阅读它们(或查看我的个人资料,我已经发出了很多完整的工作代码)。

If this wasn't simple, then I don't know what is. If you wan't to make it complex, using jQuery and Ajax, then read about them online (or take a look at my profile, I've given out a lot of full working code that works with it).

这篇关于使用javascript和PHP发送电子邮件的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆