php 使用 SMTP 邮件服务器发送邮件

2021-3-9 Jon php

通过给出指定值来通过php完成邮件发送任务,可以发送html内容,核心代码其实是php。index.html 只是测试所用,使用 bootstrap 来简单美化下样式,使用 wangEditor 强化能发送的邮件格式,使用 jquery 让代码逻辑更清晰

demo 演示地址

  1. index.html
<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <title>php 使用 SMTP 邮件服务器发送邮件</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <meta name="renderer" content="webkit">
  <meta name="force-rendering" content="webkit"/>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <link href="" rel="stylesheet">
    .modal-header {
      display: block;
  <form id="senMail">
    <div class="modal-header">
      <div class="form-row">
        <div class="form-group col-md-3">
          <label for="mailserver">smtp服务器</label>
          <input type="text" class="form-control" id="mailserver" name="mailserver" placeholder="请输入 smtp 服务器" value="" required>
        <div class="form-group col-md-3">
          <label for="name">smtp端口</label>
          <input type="text" class="form-control" id="port" name="port" placeholder="请输入 smtp 端口" value="25" required>
        <div class="form-group col-md-3">
          <label for="name">发件人邮箱</label>
          <input type="email" class="form-control" id="mailuser" name="mailuser" placeholder="请输入 smtp 服务器" required>
        <div class="form-group col-md-3">
          <label for="name">发信邮箱授权码</label>
          <input type="text" class="form-control" id="mailpass" name="mailpass" placeholder="请输入发信邮箱授权码" required>
      <div class="form-row">
        <div class="form-group col-md-6">
          <label for="subject">标题</label>
          <input type="text" class="form-control" id="subject" name="subject" placeholder="请输入标题" required>
        <div class="form-group col-md-6">
          <label for="mailto">收件人邮箱</label>
          <input type="email" class="form-control" id="mailto" name="mailto" placeholder="请输入收件人邮箱" required>
    <div class="modal-body" id="content" name="content"></div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
      <button type="submit" class="btn btn-primary">提交</button>
  <script src=""></script>
  <script src=""></script>
    $(document).ready(function() {
      // 富文本编辑器
      var E = window.wangEditor
      var editor = new E('#content')
      editor.customConfig.menus = [
          'head', // 标题
          'bold', // 粗体
          'fontSize', // 字号
          'fontName', // 字体
          'italic', // 斜体
          'underline', // 下划线
          'strikeThrough', // 删除线
          'foreColor', // 文字颜色
          'backColor', // 背景颜色
          'link', // 插入链接
          'list', // 列表
          'justify', // 对齐方式
          'quote', // 引用
          'emoticon', // 表情
          'image', // 插入图片
          'table', // 表格
          'video', // 插入视频
          'code', // 插入代码
          'undo', // 撤销
          'redo' // 重复
      * [点击发送邮件]
      $('#senMail').submit(function(event) {
        //Ajax POST发送
          type: 'post',
          url: '/static/demo/sendmail/send.php',
          data: {
            mailserver: $('#mailserver').val(),
            port: $('#port').val(),
            mailuser: $('#mailuser').val(),
            mailpass: $('#mailpass').val(),
            mailto: $('#mailto').val(),
            subject: $('#subject').val(),
            content: editor.txt.html()
          dataType: "json",
          success: function(res) {
            if(res.status === 'success') {
          error: function() {
  1. 接口 send.php
require_once './src/PHPMailer.php';

$mailserver = $_POST['mailserver'];
$port    = $_POST['port'];
$mailuser = $_POST['mailuser'];
$mailpass = $_POST['mailpass'];
$mailto = $_POST['mailto'];
$subject = $_POST['subject'];     // 邮件标题
$body    = $_POST['content'];     // 邮件正文
// print_r("<pre/>");
$test=new SendMaill($mailserver, $port, $mailuser, $mailpass, false, false);
if($test->sendMail($mailto, $mail, true)){
  echo json_encode(array('status' => 'success', 'message' => '邮件发送成功'), true);
}else {
  echo json_encode(array('status' => 'error', 'message' => '邮件发送成功'), true);
PHPMailer.php 文件地址


标签: php mail




Stephan Helbig
2023-03-26 07:40
Marketing is an art, and while we are no artists, we have spent years learning and perfecting the formula to use marketing to make some incredible profits.
And aren't you one lucky bird? The fact that you are reading this mail gives you the opportunity to be one of the few lucky marketers who gets our secret formula to success.
I have reason to believe this is a solution that is going to change your life.
Take a closer look here:
With MarketingBlocks, you will discover a better way to grow your business...
..and it’s as easy as 1, 2, 3...
STEP 1- Enter Product Name & Description
Enter keywords of your choice

STEP 2- Select The Type Of Online Business Asset
Decide from a wide variety of marketing assets such as landing pages, Facebook ads, graphics, email swipes, and more

STEP 3- A.I. Generates 100% Original Online Business Asset
Watch as our AI gets to work to generate high-converting marketing assets for your business.

Now while these steps are so easy to follow, you might still need some hand-holding in the beginning, so we have also included a support desk dedicated to guide and service you.
You also get access to some amazing features, guaranteed to make you those profits you have always dreamt about
Check out ALL the features here.
It’s time to say goodbye to all these expensive & complicated tools that you’ve been paying monthly for and invest in MarketingBlocks - The New All-In-One Digital Business Creation Platform that solves all your marketing problems.
Right now, there's a BIG founder’s special discount being offered along with exclusive fast-action bonuses.
Get access to UNLIMITED Access to MarketingBlocks for a ridiculous special launch price. Act before it’s too late and you end up spending more unnecessarily.
To your success,


皖ICP备15010162号-1 ©2015-2022 知向前端
qq:1614245331 邮箱 Powered by emlog sitemap