如何使用Vuejs发送电子邮件?

06odsfpq  于 2023-01-26  发布在  Vue.js
关注(0)|答案(5)|浏览(1715)

我怎么能用Vuejs2发送电子邮件呢?我设法获得输入数据并将其转换为JSON,但我不能将其发送到邮箱。
我寻找PHPMailer的一面,但面对PHP和Vue不混合。
如何发送表单内容?

    • 模板:**
<div class="JC-contact__form">
  <b-form @submit="onSubmit" v-if="show">

    <b-form-group class="JC-contact__form--lastName">
      <b-form-input type="text" v-model="form.lastName"> </b-form-input>
    </b-form-group>

    <b-form-group class="JC-contact__form--firstName">
      <b-form-input type="text" v-model="form.firstName"> </b-form-input>
    </b-form-group>

    <b-form-group>
      <b-form-input type="text" v-model="form.topic"> </b-form-input>
    </b-form-group>
    <b-form-group>
      <b-form-input type="email" v-model="form.email"></b-form-input>
    </b-form-group>

    <b-form-textarea v-model="form.text"></b-form-textarea>

    <b-button type="submit">Envoyer</b-button>
  </b-form>

</div>
    • 脚本:**
export default {
    name: 'Contact',
    data () {
      return {
        form: {
          lastName: '',
          firstName: '',
          topic: '',
          email: '',
          text: ''
        },
        file: null,
        show: true
      }
    },
    methods: {
      onSubmit (evt) {
        evt.preventDefault();
        alert(JSON.stringify(this.form));
      },
      onReset (evt) {
        evt.preventDefault();
        /* Reset our form values */
        this.form.lastName = '';
        this.form.firstName = '';
        this.form.topic = '';
        this.form.email = '';
        this.form.text = '';
        /* Trick to reset/clear native browser form validation state */
        this.show = false;
        this.$nextTick(() => {
          this.show = true
        });
      }
    }
  }
5us2dqdw

5us2dqdw1#

从Vue直接发送邮件是不可能的,因为你需要某种服务器来处理邮件协议。这永远不可能直接从浏览器完成。我不熟悉PHP,所以我帮不了你。在节点中,你需要nodemailer包和一些smtp服务器来处理电子邮件,比如AmazonSimpleMailServer(或者你可以使用你的gmail帐户)你也可以使用axios发送一个post请求到SendGrid或者Mandrill或者类似的服务,他们会把你的请求转换成电子邮件,然后发送到你请求正文中指定的地址。
更多信息:
https://sendgrid.com/docs/API_Reference/Web_API/mail.html
https://mandrillapp.com/api/docs/

rsl1atfo

rsl1atfo2#

我是这样做的:

从服务器端发送邮件

PHP的mail()函数会很好地工作,这里没有什么花哨的。

    • mail.php**-此文件必须位于服务器上可访问的位置。
<?php

$name = "Undefined name";

if(isset($_POST['name'])){
    $name = $_POST['name'];
}

$message = "<p>Hi!</p>";
$message .= "<p>Wazaaaaa $name</p>";

$to_email = 'dest@mail.com';
$subject = 'Mail subject';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=UTF-8';
$headers[] = 'From: Biloo <noreply@ydomain.com>';

mail($to_email, $subject, $message, implode("\r\n", $headers));

?>

请注意,头值必须可信(没有未经验证的用户提交值)。

将数据发送到邮件脚本

然后,VueJS应该向邮件脚本发送适当的数据:

    • 一米二米一x**
<template>
   <div>
      <transition name="fade" mode="out-in">
          <div v-if="sent">
              <p>Thanks</p>
          </div>
      </transition>
      </div>
      <div v-if="!sent" class="formGroup">
          <b-form @submit="onSubmit">
              <b-form-input
                  id="input-name"
                  v-model="form.name"
                  type="text"
                  required
                  placeholder="Name"
              />
              <b-button type="submit">
                  Contact
              </b-button>
          </b-form>
      </div>
   </div>
</template>

<script>
const querystring = require("querystring");

export default {
    data() {
        return {
          sent: false,
          form: {
              name: ""
          }
        };
    },
    methods: {
      onSubmit(e) {
          e.preventDefault();
          this.$axios
             .post(
                 "https://theServer.com/mail.php",
                  querystring.stringify(this.form)
             )
             .then(res => {
                 this.sent = true;
             });
      }
    }
};
</script>

这里,需要注意的是Axios的默认行为是发布JSON对象,但是这样做的话,PHP会收到一个空的$_POST值,因此在发布之前必须使用querystring依赖项格式化数据。
可以使用npm安装Querystring:
npm i querystring

ymzxtsji

ymzxtsji3#

<template>    
<div class="request-a-callback-form">
 <transition name="fade" mode="out-in">
  <div v-if="sent">
   <p>Thanks for contacting us, we will get back to you shortly...</p>
  </div>
 </transition>
 <form v-on:submit="sendForm">
  <input type="text" v-model="ContactForm.name" placeholder="Your Name">
  <input type="text" v-model="ContactForm.email" placeholder="Email Address">
  <input type="text" v-model="ContactForm.phone" placeholder="Phone Number">
  <input type="text" v-model="ContactForm.subject" placeholder="Subject">
  <textarea v-model="ContactForm.message" rows="8" cols="80" class="form-control"> 
  </textarea>
  <br>
  <button data-text="submit" type="submit" class="btn btn-primary">Submit</button>
 </form>
</div>
</template>
        <script>
        export default {
          data() {
            return {
              sent: false,
              ContactForm: {
                name : '',
                email: '',
                phone: '',
                subject: '',
                message: ''
              }
            }
          },
          methods: {
            sendForm(e) {
              e.preventDefault()
              console.log(this.ContactForm)
              this.$axios.post('api/mailserver.php',
              querystring.stringify(this.ContactForm)).then(res => {
                this.sent = true
              })
            }
          }
        }
        </script>

    Your php Server

    <?php
    // Allow from any origin
     if(isset($_SERVER["HTTP_ORIGIN"]))
     {
        // You can decide if the origin in $_SERVER['HTTP_ORIGIN'] is something you want to 
      allow, or as we do here, just allow all
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
     }
     else
     {
        //No HTTP_ORIGIN set, so we allow any. You can disallow if needed here
        header("Access-Control-Allow-Origin: *");
     }

     header("Access-Control-Allow-Credentials: true");
     header("Access-Control-Max-Age: 600");    // cache for 10 minutes

     if($_SERVER["REQUEST_METHOD"] == "OPTIONS")
     {
        if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_METHOD"]))
            header("Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT"); 
  //Make 
        sure you remove those you do not want to support

        if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"]))
            header("Access-Control-Allow-Headers: 
           {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

           //Just exit with 200 OK with the above headers for OPTIONS method
        exit(0);
      }
    //From here, handle the request as it is ok

     if(!empty($_POST['name'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];
     }

     $message = "$message";

     $to_email = '<email to be sent to>';
     $subject = "$subject";
     $headers[] = 'MIME-Version: 1.0';
     $headers[] = 'Content-type: text/html; charset=UTF-8';
     $headers[] = "From: <$email>";

     mail($to_email, $subject, $message, implode("\r\n", $headers));

    ?>
ha5z0ras

ha5z0ras4#

你也可以使用EmailJS发送客户端电子邮件。它可以帮助你使用vuejs或任何其他客户端技术发送电子邮件,一旦你创建了一个帐户,创建了一个电子邮件模板,并使用VueJS触发电子邮件,就可以将EmailJS连接到支持的电子邮件服务之一。这里有一个链接到他们的文档https://www.emailjs.com/docs/examples/vuejs/

fivyi3re

fivyi3re5#

您可以试试mailjs软件包。mailjs

相关问题