douya1061
2017-03-30 07:39
浏览 49
已采纳

在codeigniter php中发送电子邮件后无法显示成功消息

Hi i am having a request form same as contact form once i send the mail not able to display any success message once i send email.Not displaying any success message or failure message after sending email.

Controller:

function request()
{
    $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
        $this->form_validation->set_rules('name','First Name' , 'required');
        $this->form_validation->set_rules('email','Email','required|valid_email');
        $this->form_validation->set_rules('phone','Mobile Number','required|numeric');
        $this->form_validation->set_rules('description','Description','required');
        if($this->form_validation->run()== FALSE)   
        {   
        $data['mainpage']='index';
        $this->load->view('templates/template',$data);
        }

    else
    {
        echo "hi";
        //get the form data
        $name = $this->input->post('name');
        $from_email = $this->input->post('email');
        $phone = $this->input->post('phone');
        $description = $this->input->post('description');       

        //set to_email id to which you want to receive mails
        $to_email = 'mail@gmail.com';

        $config=Array(
    'protocol'=> 'smtp',
    'smtp_host' => 'ssl://smtp.gmail.com', //smtp host name
    'smtp_port' => '465', //smtp port number
    'smtp_user' => 'mail@gmail.com',
    'smtp_pass' => 'abcdefgh', //$from_email password
    'mailtype' =>'html',
    'newline'  =>"
",
    'crlf' =>"
",
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE
    );

    $message            = array();    
$message[] = 'Username  :  '.trim($name).' ';
$message[] = 'Phone Number :  '.trim($phone).' ';
$message[] = 'Description :  '.trim($description).' ';
$message = implode(PHP_EOL, $message);
    //send mail
    $this->load->library('email',$config);
    $this->email->from($from_email);
    $this->email->to($to_email);
    $this->email->subject('Request Free Quote');
    $this->email->message($message);
    $this->email->set_newline("
");

        if ($this->email->send())
        {
            // mail sent
            echo "hi";
            $this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
            redirect('welcome');
        }
        else
        {
            //error
            $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
            redirect('welcome');
        }
    }
}

View:

<div class="col-md-3 requestfree">
<img src="<?php echo base_url();?>theme/images/icon1.png" class="requesting">
<h3 class="request">REQUEST</h3>
<h5 class="free"> FREE QUOTE</h5>
<h5 class="receive">You will receive quote within 24 hours</h5>
<form name="contact"  id="contactform" enctype="multipart/form-data" method="post" action="<?php echo base_url();?>welcome/request">
<?php if(isset($msg)){?>
     <?php echo $msg;?>
       <?php } ?>

<div class="name"><span class="mandatory"></span>
    <input type="text" class="form-control" name="name" placeholder="Name" required>
    <?php echo form_error('name', '<div class="error">', '</div>'); ?>
</div>
<div class="name">
    <input type="text" class="form-control" name="phone" placeholder="Phone" required>
    <?php echo form_error('phone', '<div class="error">', '</div>'); ?>
</div>
<div class="email">
    <input type="email" class="form-control" name="email" placeholder="Email" required>
    <?php echo form_error('email', '<div class="error">', '</div>'); ?>
</div>
<div class="description">
    <textarea name="description"  style="width:100%;overflow:auto;" placeholder="Description" required></textarea>
</div>
<div class="">
    <div class="captchas">
        <img src="captcha.php?rand=<?php echo rand();?>" class="captchass" id='captchaimg'/>
        <p class="change"><a href="javascript: refreshCaptcha();" class="clickto">click to change</a></p>
    </div>
    <div class="captcha">
        <input type="text" class="form-control"id="captcha_code" name="captcha_code"  style="background-color: #EAD5D2;border: none;">
    </div>
</div>
<input type="hidden" value="true" name="isSubmitted">
<button type="submit" class="btn btn-success" onclick="return validate();"  >Submit</button><!--  id="demo"!-->
<div class='message'>  
<p class="thankyou">
Thank You
</p>
<p>
Our team will contact you shortly.
</p>  
</div>
<p class="privacy">We respect your privacy</p>
</form>
</div>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumang20060820 2017-03-30 08:05
    已采纳

    try in your view

    echo $this->session->flashdata('msg');
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题