简体   繁体   中英

Sending an automated email

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.

Simplest way to do that:

  1. 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> 
  2. 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); ?> 

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).

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.

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.

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.

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.

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.

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.

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

w3schools form email

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.

This link can help with accessing a mysql database from php.

This link can help with sending php email.

This link can help with sending html based email from php.

You may want to investigate AJAX via jQuery.

http://learn.jquery.com/ajax/

AJAX allows you to open a PHP script within javascript, and then do things with whatever the php script echos back.

If you do not mind navigating away from the interface page, an HTML form would work.

Sounds like you're a little confused, JavaScript will not be any help for the sending of the email..

..perhaps it would help you to think about it like this:

  1. a HTML page with a link ( <a> ) that refers to your php script:

     `<a href="email-script.php">Send Email</a>` 
  2. 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.

  3. the php script should then return the user to the original page.. with a message to tell the user that their email was sent

There are a lot of tutorials for sending email from PHP, and those links above are to pages with example code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM