Codeigniter联系表格电子邮件

时间:2015-04-19 20:39:09

标签: php forms codeigniter email

我试图在Codeigniter中设置联系表单(我使用Apache在xampp上运行,并且我已经设置了虚拟主机)。我有实际的表单工作,但当我尝试将其链接以便通过电子邮件发送结果时出现错误。 我环顾四周,尝试了一些不同的解决方案,但我无法弄清楚我做错了什么。

控制器:

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');
                }
            }
    }

查看:

 <?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'>

错误讯息:

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\htdocs\root\CI\application\controllers\home.php
Line: 80
Function: send

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

非常感谢任何建议。

修改

我已根据另一篇文章的建议更改了端口,但现在我收到以下消息:

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\htdocs\root\CI\application\controllers\home.php
Line: 68
Function: send

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

我尝试更改我的电子邮件地址,以便确保我没有输入错误的电子邮件详细信息,并得到一条不同的消息说我的主机已强行关闭该连接,所以我不会# 39;不要相信任何简单的事情。

EDIT 我已经设置了一个不同的电子邮件地址,我现在不再收到错误但是现在我总是收到&#34;消息无法发送&#34;屏幕,我不明白为什么。

修改

我现在已经按照评论中的建议使用了print_debugger。结果如下。

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--

我将此视为MIME问题,这是正确的吗?

2 个答案:

答案 0 :(得分:2)

我想也许电子邮件smpt需要ssl://如果您使用xampp或wamp来测试电子邮件,则需要配置邮件设置。

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

应该

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

如何在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');
}
}
}

答案 1 :(得分:0)

事实证明,有许多因素似乎导致了这个问题。首先,我需要配置一些PHP设置,第二个特定的电子邮件地址阻止连接(gmail和实时邮件)所以我切换到在我拥有的域上使用电子邮件地址这很好,最后,只是为了让事情变得有趣我的电子邮件帐户遇到了技术问题,现在已经解决,导致今天早上发送了大约20条测试邮件。

我做了什么;

在Xampp / PHP / php.ini中:

取消注释sendmail_path =“\”C:\ xampp \ sendmail \ sendmail.exe \“ - t”,注释掉sendmail_path的下一个实例。

取消注释extension = php_openssl.dll

进入Xampp / sendmail / sendmail.ini:

转到smtp_server并设置为你的smtp扩展名(smtp.yourdomain.com),这下面的几行将是用户名和密码的一部分。将其设置为您的电子邮件帐户的用户名和密码。

我还在控制器中使用以下程序来检查所有相关端口是否已打开:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends CI_Controller {

        public function index()
    {
    $fp = fsockopen("www.google.com", 80, $errno, $errstr, 10);
    if (!$fp)
        echo "www.google.com -  $errstr   ($errno)<br>\n";
    else
        echo "www.google.com -  ok<br>\n";


    $fp = fsockopen("smtp.yourdomain.com", 465, $errno, $errstr, 10);
    if (!$fp)
        echo "smtp.yourdomain.com 465  -  $errstr   ($errno)<br>\n";
    else
        echo "smtp.yourdomain.com 465 -  ok<br>\n";


    $fp = fsockopen("smtp.yourdomain.com", 587, $errno, $errstr, 10);
    if (!$fp)
        echo "smtp.yourdomain.com  -  $errstr   ($errno)<br>\n";
    else
        echo "smtp.yourdomain.com -  ok<br>\n";

    $fp = fsockopen("smtp.yourdomain.com", 25, $errno, $errstr, 10);
    if (!$fp)
        echo "smtp.yourdomain.com  -  $errstr   ($errno)<br>\n";
    else
        echo "smtp.yourdomain.com 25 -  ok<br>\n";
    }
}
相关问题