Home  >  Article  >  Web Front-end  >  How to use javascript to send emails via email

How to use javascript to send emails via email

PHPz
PHPzOriginal
2023-04-25 10:31:033592browse

With the popularity and application of email becoming more and more widespread, JavaScript has gradually become a common tool for sending emails. Using JavaScript, you can quickly and efficiently add email functionality to a website or application, making it easy for users to send emails through the website or application. Below we will introduce how to use JavaScript to implement the function of sending emails.

The implementation steps are as follows:

  1. Add a form to the HTML page.
<form action="" method="post">
  <label for="email">邮件地址:</label>
  <input type="email" id="email" name="email"><br><br>
  <label for="subject">主题:</label>
  <input type="text" id="subject" name="subject"><br><br>
  <label for="message">正文:</label>
  <textarea id="message" name="message"></textarea><br><br>
  <button type="submit" onclick="sendEmail()">发送邮件</button>
</form>
  1. Define mail server configuration information in JavaScript.
let emailjsConfig = {
  USER_ID: 'your_user_id',
  TEMPLATE_ID: 'your_template_id',
  SERVICE_ID: 'your_service_id'
};

The USER_ID here is the user ID you obtained after registering in EmailJS, TEMPLATE_ID is the ID of the email template you created, and SERVICE_ID is the ID of the email sending service you created. This information is needed when calling the email sending API.

  1. Use EmailJS to send emails.
function sendEmail() {
  let email = document.getElementById('email').value;
  let subject = document.getElementById('subject').value;
  let message = document.getElementById('message').value;

  emailjs.send(emailjsConfig.SERVICE_ID, emailjsConfig.TEMPLATE_ID, {
      to_email: email,
      from_name: '你的名字',
      subject: subject,
      message_html: message
    })
    .then(function(response) {
      console.log('邮件发送成功!', response.status, response.text);
      alert('邮件发送成功!');
    }, function(error) {
      console.log('邮件发送失败...', error);
      alert('邮件发送失败...', error);
    });
}

Here, we use EmailJS to send emails, connect to the mail server and send emails through the defined EMAILJS_CONFIG variable. The sendEmail function will obtain the email address, subject, content and other information in the form, and pass this information to the mail server. When the email is sent successfully or fails, corresponding console output and prompt box information will be generated.

The above are the steps to use JavaScript to send emails by mailbox. If you want to enable your website or application to send emails, you can refer to this example and customize it according to the actual situation.

The above is the detailed content of How to use javascript to send emails via email. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn