douke3335 2015-04-19 20:39
浏览 58

Codeigniter联系表格电子邮件

I'm trying to set up a contact form in Codeigniter (I'm running on xampp using Apache and I have a virtual host set up). I have the actual form working but when I try to link it up so that it emails the results I get an error. I've looked around and tried a few different solutions but I can't figure out what it is that I'm doing wrong.

Controller:

public function contact()
    {
            $this->load->helper('form');
            $this->load->library('form_validation');

            $this->form_validation->set_rules('name', 'your name', 'required', array('required'=>"<p class='required'>Please provide %s</p><br>"));
            $this->form_validation->set_rules('email', 'your email address', 'required', array('required'=>"<p class='required'>Please provide %s</p><br>"));


            if($this->form_validation->run() == FALSE)
            {
                $this->load->view('templates/headder');
                $this->load->view('contact');
                $this->load->view('templates/footer');
            }
            else
            {
                $config = array(
                    'protocol' => 'smtp',
                    'smtp_host' => 'smtp.mydomain.co.uk',
                    'smtp_port' => 465,
                    'smtp_user' => 'example@mydomain.co.uk',
                    'smtp_pass' => 'mypassword',
                    'charset' => 'iso-8859-1',
                    'wordwrap' => TRUE,
                );

                $message = 'This is a test message... do I work?';

                $this->load->library('email');
                $this->email->initialize($config);
                $this->email->from('ecample@example.co.uk', 'Tester');
                $this->email->to('example@mydomain.co.uk');
                $this->email->subject('New Query');
                $this->email->message($message);
                $this->email->send();

                if($this->email->send()){
                $this->load->view('templates/headder');
                $this->load->view('sent');
                $this->load->view('templates/footer');
                }
                else
                {
                $this->load->view('templates/headder');
                $this->load->view('contact');
                $this->load->view('templates/footer');
                }
            }
    }

View:

 <?php echo form_open('home/contact'); ?>
                <h3><span class='required'>*</span>Name:</h3>
                    <label><input type='text' name='name' value="<?php echo set_value('name'); ?>" maxlength="50" ></label>
                    <?php echo form_error('name'); ?>
                <h3>Company:</h3>
                    <label><input type='text' name='company' value="<?php echo set_value('company'); ?>" maxlength="100"></label>
                <h3><span class='required'>*</span>eMail:</h3>
                    <label><input type='email' name='email' value="<?php echo set_value('email'); ?>" maxlength="100"></label>
                    <?php echo form_error('email'); ?>
                <input id='submit_button' type='submit' value='Submit'>

Error Message:

A PHP Error was encountered

Severity: Notice

Message: fwrite(): send of 11 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host.

Filename: libraries/Email.php

Line Number: 2131

Backtrace:

File: C:\xampp\htdocsoot\CI\application\controllers\home.php
Line: 80
Function: send

File: C:\xampp\htdocsoot\public_html\index.php
Line: 292
Function: require_once

Any advice would be much appreciated.

EDIT

I have changed the port as suggested on another post however now I am getting the following message:

A PHP Error was encountered

Severity: Warning

Message: fsockopen(): unable to connect to mail.myemail.co.uk:25 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. )

Filename: libraries/Email.php

Line Number: 1949

Backtrace:

File: C:\xampp\htdocsoot\CI\application\controllers\home.php
Line: 68
Function: send

File: C:\xampp\htdocsoot\public_html\index.php
Line: 292
Function: require_once

I tried changing my email address so that it was incorrect just to make sure I hadn't entered my email details wrong and got a different message to say that the connection had been forcibly closed by my host so I don't believe it's anything that simple either.

EDIT I have set a different email address up and I'm now no longer getting an error however now I'm always getting the "message failed to send" screen and I can't understand why.

EDIT

I have now used print_debugger as suggested in the comments. The result was as followed.

220 BLU436-SMTP37.smtp.hotmail.com Microsoft ESMTP MAIL Service, Version: 8.0.9200.16384 ready at Mon, 20 Apr 2015 11:25:59 -0700

hello: 250-BLU436-SMTP37.smtp.hotmail.com Hello [90.209.233.150]
250-TURN
250-SIZE 41943040
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-TLS
250-STARTTLS
250 OK

Failed to send AUTH LOGIN command. Error: 530 5.7.0 Must issue a STARTTLS command first
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

User-Agent: CodeIgniter
Date: Mon, 20 Apr 2015 20:25:58 +0200
From: "Tester" <myemail@mydomain.co.uk>
Return-Path: <myemail@mydomain.co.uk>
To: myemail@mydomain.co.uk
Subject: =?SIO-8859-1?Q?=4E=65=77=20=51=75=65=72=79?=
Reply-To: "myemail@mydomain.co.uk" <myemail@mydomain.co.uk>
X-Sender: myemail@mydomain.co.uk
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <553544b61c447@mydomain.co.uk>
Mime-Version: 1.0


Content-Type: multipart/alternative; boundary="B_ALT_553544b61c451"

This is a multi-part message in MIME format.
Your email application may not support this format.

--B_ALT_553544b61c451
Content-Type: text/plain; charset=SIO-8859-1
Content-Transfer-Encoding: 8bit

this is a test


--B_ALT_553544b61c451
Content-Type: text/html; charset=SIO-8859-1
Content-Transfer-Encoding: quoted-printable

this is a test

--B_ALT_553544b61c451--

I'm reading this as a MIME problem, is this correct?

  • 写回答

2条回答 默认 最新

  • dongyun9120 2015-04-19 23:31
    关注

    I think perhaps email smpt needs ssl:// and if you are using xampp or wamp for testing email you need to configure the mail settings.

    'smtp_host' => 'smtp.mydomain.co.uk',
    

    And Should Be

    'smtp_host' => 'ssl://smtp.mydomain.co.uk';
    

    How to setup email on xampp

    https://www.youtube.com/watch?v=TO7MfDcM-Ho

    public function contact() {
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->library('email');
    $this->load->library('url');
    
    $this->form_validation->set_rules('name', 'Your Name', 'required');
    $this->form_validation->set_rules('email', 'your email address', 'required');
    
    // Removed Array From Form Validation
    
    
    if($this->form_validation->run() == FALSE) {
    
    $this->load->view('templates/headder');
    $this->load->view('contact');
    $this->load->view('templates/footer');
    
    } else {
    
    $config = array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.mydomain.co.uk',
    'smtp_port' => 465,
    'smtp_user' => 'example@mydomain.co.uk',
    'smtp_pass' => 'mypassword',
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE,
    );
    
    $this->email->initialize($config);
    $this->email->from('ecample@example.co.uk', 'Tester');
    $this->email->to('example@mydomain.co.uk');
    $this->email->subject('New Query');
    $message = 'This is a test message... do I work?';
    $this->email->message($message);
    
    if($this->email->send()){
    $this->load->view('templates/header'); // Fix spelling Mistake hedder
    $this->load->view('sent');
    $this->load->view('templates/footer');
    } else {
    $this->load->view('templates/header'); // Fix spelling Mistake hedder
    $this->load->view('contact');
    $this->load->view('templates/footer');
    }
    }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?