我如何添加smtp代码

时间:2016-05-16 07:28:02

标签: php

首先,我在这里和PHP

我的问题是关于Smtp的支持。我的托管需要smtp支持。我没有添加它。我读了很多帖,但我的知识基本。如何在此文件中添加smtp代码,请帮助我

<?php
  //start a session -- needed for Securimage Captcha check
  session_start();

  //add you e-mail address here
  define("MY_EMAIL", "sss@sss.com,sss@gmail.com");

  /**
   * Sets error header and json error message response.
   *
   * @param  String $messsage error message of response
   * @return void
   */
  function errorResponse ($messsage) {
    header('HTTP/1.1 500 Internal Server Error');
    die(json_encode(array('message' => $messsage)));
  }

  /**
   * Return a formatted message body of the form:
   * Name: <name of submitter>
   * Comment: <message/comment submitted by user>
   *
   * @param String $name     name of submitter
   * @param String $messsage message/comment submitted
   */
  function setMessageBody ($name, $phone, $email, $message) {
    $message_body .= "Name: " . $name."\n\n";
    $message_body .= "Phone: " . $phone."\n\n";
    $message_body .= "Email: " . $email."\n\n";
    $message_body .= "Message:" . $message."\n\n";
    return $message_body;
  }

 $name = $_POST['name'];
 $phone = $_POST['phone'];  
 $email = $_POST['email']; 
 $message = $_POST['message'];

  header('Content-type: application/json');
  //do some simple validation. this should have been validated on the client-side also
  if (empty($email) || empty($message)) {
    errorResponse('Email or message is empty.');
  }

  //do Captcha check, make sure the submitter is not a robot:)...
  include_once './vender/securimage/securimage.php';
  $securimage = new Securimage();
  if (!$securimage->check($_POST['captcha_code'])) {
    errorResponse('Invalid Security Code');
  }
//try to send the message
 if(mail(MY_EMAIL, "Feedback Form Results", setMessageBody($_POST["name"], $_POST["phone"], $_POST["email"], $_POST["message"]), "From:$email")) {
 echo json_encode(array('message' => 'Your message was successfully submitted.'));

 } else {
 header('HTTP/1.1 500 Internal Server Error');
 echo json_encode(array('message' => 'Unexpected error while attempting to send e-mail.'));
 }
 ?>

2 个答案:

答案 0 :(得分:0)

使用php mailer for smtp https://sourceforge.net/projects/phpmailer/。这个库有smtp代码默认,并给出了很多例子。

答案 1 :(得分:0)

var data = {

        "api_user":"username",
        "api_key":"ioioi",
        "to":[],
        "tonnage":[],
        "cc":[],
        "ccname":[],
        "bcc":[],
        "subject":sub,
        "from":from,
        "html":htmlBody
      }

      var headers = { "Accept":"application/json", 
                     "Content-Type":"application/json"
                    };



        data = JSON.stringify(data);

        var options = {
          "method": "POST",
          "payload": data,
          "headers": headers,
          "muteHttpExceptions": true
        };

var res = UrlFetchApp.fetch("https://api.sendgrid.com/api/mail.send.json", options);