在codeigniter中以不同形式发送电子邮件

时间:2016-04-04 09:34:21

标签: php codeigniter email

我需要一些帮助。 我使用CI 3构建这个网站,它有3种不同的形式,有3个不同的页面(职业,报价,联系),用户可以填写它们,我将收到一封电子邮件,但只有联系页面正在工作,剩下的两个页面提示和错误(无法使用PHP邮件发送电子邮件()。您的服务器可能没有配置为使用此方法发送邮件),而它们都属于同一个控制器。谁能帮我解决这个问题? 下面是我的代码的链接

http://pastebin.com/REFjPsUf

由于

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

class Site extends CI_Controller {
public function index(){
    $this->home();
}
public function home(){
    $this->load->view("home_header");
    $this->load->view("site_nav");
    $this->load->view("slider");
    $this->load->view("home_content");
    $this->load->view("footer");

}
public function aboutUs(){
    $this->load->view("home_header");
    $this->load->view("site_nav");
    $this->load->view("slider");
    $this->load->view("content_about");
    $this->load->view("footer");
}
public function projects(){
    $this->load->view("home_header");
    $this->load->view("site_nav");
    $this->load->view("slider");
    $this->load->view("content_project");
    $this->load->view("footer");

}
public function services(){
    $this->load->view("home_header");
    $this->load->view("site_nav");
    $this->load->view("slider");
    $this->load->view("content_service");
    $this->load->view("footer");

}
public function careers(){
    $this->load->view("home_header");
    $this->load->view("site_nav");
    $this->load->view("slider");
    $this->load->view("content_career");
    $this->load->view("footer");

}
public function gallery(){
    $this->load->view("home_header");
    $this->load->view("site_nav");
    $this->load->view("slider");
    $this->load->view("content_gallery");
    $this->load->view("footer");

}
public function register(){
    $this->load->view("home_header");
    $this->load->view("site_nav");
    $this->load->view("slider");
    $this->load->view("quote");
    $this->load->view("footer");

}


public function contact(){
    $this->load->view("home_header");
    $this->load->view("site_nav");
    $this->load->view("contact_header");
    $this->load->view("content_contact");
    $this->load->view("footer");

}


public function sendmail(){




    $this->load->library('email');



    $this->email->from($this->input->post("email"), $this->input->post("name"));
    $this->email->to("me@example.com");


    $this->email->subject('from website contact form');

    $this->email->message($this->input->post("message"));


    if ( ! $this->email->send())
    {
            echo $this->email->print_debugger(); // Generate error
    }
    else{
        $this->load->view("home_header");
        $this->load->view("site_nav");
        $this->load->view("contact_header");
        $this->load->view("contact_success");
        $this->load->view("footer");
    }


}


public function quotation(){




    $this->load->library('email');



    $this->email->from($this->input->post("email"));
    $this->email->to("me@example.com");


    $this->email->subject($this->input->post("subject"));

    $this->email->message($this->input->post("message"));


    if ( ! $this->email->send())
    {
            echo $this->email->print_debugger(); // Generate error
    }
    else{
        $this->load->view("home_header");
        $this->load->view("site_nav");
        $this->load->view("slider");
        $this->load->view("quote_success");
        $this->load->view("footer");
    }
} 


public function mycareer(){


    $this->load->library('email');



    $this->email->from($this->input->post("email"));
    $this->email->to("me@example.com");

    $this->email->subject($this->input->post("subject"));

    $this->email->message($this->input->post("message"));

    if (!$this->email->send()){
        echo $this->email->print_debugger();
    }
    else{
        echo "Email sent";
    }


}

}

3 个答案:

答案 0 :(得分:1)

quotation() and mycareer()

中第二个参数的最后两个函数“$this->email->from()”缺失,代码中出现错误

reference link

$this->email->from($this->input->post("email") , 'Your Name');

希望它适合你。

答案 1 :(得分:0)

**尝试使用ci中的smtp配置

$this->email->initialize(array(
  'protocol' => 'smtp',
  'smtp_host' => 'xxxx.ipage.com',//SMTP servern name
  'smtp_user' => 'user@xxxx.com',
  'smtp_pass' => 'SMTP user password',
  'smtp_port' => 587,//port
  'crlf' => "\r\n",
  'mailtype' => 'html',
  'newline' => "\r\n",
'wordwrap' => TRUE
));

$this->email->from($email,$name);
$this->email->to('admin@xxxx.com'); 
$this->email->cc('user@xxxx.com'); 
$this->email->bcc('xxxxx@gmail.com'); 

$this->email->subject('Email Test');
$this->email->message($message1);   

if($this->email->send())
echo "<script>alert('Thank you... mail Sent');</script>";
else
echo "<script>alert('Not Sent Please Try again');</script>";
redirect('xxxx/abc');

或者您也可以使用PHP邮件类..只需从GitHub或php官方网站下载。然后将其粘贴到视图文件夹中,然后将其包含在内......那就是它。

<?php

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
//date_default_timezone_set('Etc/UTC');

require __DIR__.'/phpmailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug   = 2;
//$mail->DKIM_domain = '160.153.33.231';
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host        = "mail.xxxx.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port        = 465;
//Whether to use SMTP authentication
$mail->SMTPAuth    = true;
//Username to use for SMTP authentication
$mail->Username    = "info@xxxxx.com";
//Password to use for SMTP authentication
$mail->Password    = "xxxxx";
$mail->SMTPSecure  = 'ssl';

if(isset($from,$sender)){
    //Set who the message is to be sent from
    $mail->setFrom($from, $sender);
}
else $mail->setFrom("info_@xxxxx.com", 'from Website');



//Set an alternative reply-to address
//$mail->addReplyTo('info_@xxxx.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('care@xxxxxx.com', 'xxxxxx.com');

//$mail->AddBCC('xxxx@gmail.com','');
//Set the subject line
$mail->Subject = $subject;

//convert HTML into a basic plain-text alternative body
$mail->msgHTML($body_tmp);
//Replace the plain text body with one created manually

$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
//echo $body_tmp;
?>**

答案 2 :(得分:0)

许多人对“联系我们”表单也有一个非常常见的错误。

$this->email->from($this->input->post("email"));

这将破坏SPF并导致DMARC失败,如果您使用的邮件服务器上启用了DMARC,您将永远不会收到某些人的消息。

由于DMARC是一个更新的协议,很多旧的cookie切割代码用于联系我们表格 - 不考虑这一点。

您可以在此处详细了解:"DMARC - Contact Form Nightmare"

建议的解决方法是:

$this->email->from("me@example.com");
$this->email->subject($this->input->post("subject") . $this->input->post("email") );

这样 - 您可以避免文章中的问题大纲。您将无法快速点击“回复”按钮,但至少您将收到启用了DMARC的客户的电子邮件。