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
问题:
回答1:
"I need to be able to fetch an email address from the user"
Create a form:
<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>
"and then use php to send them an email with a link"
The submit button within the form will POST the value of the input field to the PHP script emailHandler.php
"I know there are some implementations using jquery or AJAX but i have no experience in either of those topics"
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
in emailHandler.php.
I like to do it like this:
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");
}
}
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);
}
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');
". . I was hoping for something simple. thanks"
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).
回答2:
If you are trying to send an email in a form, you can just do this to the form action and the rest are similar. Hope it helps(:
<form action="MAILTO:name@email.com" method="post" enctype="text/plain">
回答3:
PHP has a mail function. You will need to configure the php.ini for this though.