【问题标题】:Sending mail with api data using codeigniter使用 codeigniter 发送带有 api 数据的邮件
【发布时间】:2017-09-04 11:02:02
【问题描述】:

我需要使用 APi 功能向管理员发送包含插入数据的邮件, 功能是这样的

  public function requestbookingresort_post()
    {
        $languageid = $this->input->post('languageId');
        $resort_id = $this->input->post('resortId');
        $booking_from = $this->input->post('bookingFrom');
        $booking_to = $this->input->post('bookingTo');
        $contact_name = $this->input->post('contactName');
        $contact_email = $this->input->post('contactEmail');
        $contact_phone = $this->input->post('contactPhone');
        $userid = $this->input->post('userId');

        if (empty($languageid))
        {
            $languageRecord = getDefaultlanguage();
            $languageid = $languageRecord->languageid;
        }
        $language_file = languagefilebyid($languageid);
        $this->lang->load($language_file, $language_file);

        if (empty($resort_id) || empty($booking_from) || empty($booking_to) || empty($contact_name) || empty($contact_email) || empty($contact_phone))
        {
            $arr['status'] = 'error';
            $arr['statusMessage'] = lang('error_in_booking');
            $arr['data'] = array();
        }
        else
        {
            $dataArray = array(
                "languageid" => $languageid,
                "userid" => empty($userid) ? "0" : $userid,
                "resortid" => $resort_id,
                "bookingfrom" => date("Y-m-d", strtotime($booking_from)),
                "bookingto" => date("Y-m-d", strtotime($booking_to)),
                "contactname" => $contact_name,
                "contactemail" => $contact_email,
                "contactphone" => $contact_phone,
                "requestdatetime" => date("Y-m-d H:i:s"),
            );
            $this->load->model("Resort_model");
            $booking_id = $this->Resort_model->saveBookingRequest($dataArray);

            if (empty($booking_id))
            {
                $arr['status'] = 'error';
                $arr['statusMessage'] = lang('error_occurred');
                $arr['data'] = array();
            }
            else
            {

                $arr['status'] = 'success';
                $arr['statusMessage'] = lang('booking_request_submit');
                $arr['data'] = array();
            }
        }

        $response = array(
            "response" => $arr
        );

        $this->set_response($response, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
    }

但我是 codeigniter 的新手,不知道如何从数据库中获取传递的数据以将邮件发送到管理员邮件或其他东西?

【问题讨论】:

    标签: php codeigniter api email


    【解决方案1】:

    试试这个。

    public function requestbookingresort_post()
    {
      // Your operations
      $response = array(
            "response" => $arr
      );
      $this->sendMail($response)
      $this->set_response($response, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
    }
    public function sendMail($response)
    {
        $settings=$this->Some_model->getEmailSettings();
        $mail = new PHPMailer();
        $mail->IsSMTP(); // we are going to use SMTP
        $mail->SMTPAuth   = true; // enabled SMTP authentication
        $mail->SMTPSecure = "ssl";  // prefix for secure protocol to connect to the server
        $mail->Host       = $settings->host;                    // setting GMail as our SMTP server
        $mail->Port       = $settings->port;                    // SMTP port to connect to GMail
        $mail->Username   = $settings->email;                   // user email address
        $mail->Password   = $settings->password;                // password in GMail
        $mail->SetFrom($settings->sent_email, $settings->sent_title);       //Who is sending the email
        $mail->AddReplyTo($settings->reply_email,$settings->reply_email);   //email address that receives the response
        $mail->Subject    = "Your Booking has been confirmed";
        $mail->IsHTML(true);
        $body = $this->load->view('path/email_template', $response, true);
        $mail->MsgHTML($body);
        $destination = $response['contactEmail']; // Who is addressed the email to
        $mail->AddAddress($destination);
        if(!$mail->Send()) {
            $data['code']=300;
            $data["message"] = "Error: " . $mail->ErrorInfo;
        }
    }
    

    确保您的库中有PHPMailer,并且您正在将库加载到您的构造函数中,我希望您将电子邮件设置保留在数据库中。如果没有,您可以手动提供主机、端口、用户名和密码字段

    【讨论】:

    • 你能告诉我那一行是从哪里来的吗:$body = $this->load->view('path/email_template', $response, true);
    • 哦,如果您正在使用,此行用于加载您的 HTML 电子邮件模板。加载视图比在这里创建视图要容易得多
    • 我想我需要为 "phpmailer/phpmailer": "~5.2" 上传作曲家?或者它不适合我吗?
    • 或者你可以从 git 克隆它
    • 它现在正在和我一起工作,但我无法从第一个函数获取响应数组到消息内容>>我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 2018-11-11
    • 2021-03-26
    相关资源
    最近更新 更多