有联系表格,但无法通过codeigniter PHP中的联系表格发送电子邮件。这是我的代码。如果我提交表格并通过联系表格发送请求,则不会收到任何错误消息,也不会收到任何电子邮件。更改代码时,它会重定向到显示为“失败”的失败状态
控制器:
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('fullname','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('text','Description','required');
$this->form_validation->set_rules('subject','Description','required');
if($this->form_validation->run()== FALSE)
{
$data['mainpage']='contact';
$this->load->view('templates/template',$data);
}
else
{
//echo "hi";
//get the form data
$name = $this->input->post('fullname');
$from_email = $this->input->post('email');
$phone = $this->input->post('phone');
$description = $this->input->post('text');
$subject=$this->input->post('subject');
//set to_email id to which you want to receive mails
$to_email = 'email@gmail.com';
$config=Array(
'protocol'=> 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com', //smtp host name
'smtp_port' => '465', //smtp port number
'smtp_user' => 'gmail@gmail.com',
'smtp_pass' => 'PASSword1@3', //$from_email password
'mailtype' =>'html',
'newline' =>"\r\n",
'crlf' =>"\r\n",
'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($subject);
$this->email->message($message);
if ($this->email->send())
{
echo "sent";
//$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
// redirect('welcome');
}
else
{
echo "failure";
}
}
查看:
<div class ="contactus">
<div class="contactname">
<input type="text" class="form-control names" name="fullname" id="fullname" placeholder="FullName" >
<?php echo form_error('fullname', '<div class="error">', '</div>'); ?>
</div>
<div class="contactemail">
<input type="email" class="form-control emails" id="email" name="email" placeholder="Email">
<?php echo form_error('email', '<div class="error">', '</div>'); ?>
</div>
<div class="contactsubject">
<input type="text" class="form-control subjects" id="subject" name="subject" placeholder="Subject">
</div>
<div class="contactphone">
<input id="phone" type="tel">
<input type="text" class="form-control phones" name="phone" placeholder="Mobile">
</div>
<div class="contactmessage">
<textarea id="text" name="text" class="textarea" placeholder="Message"></textarea>
</div>
<div class="">
<div class="contactpagecaptchas">
<img src="/captcha.php?rand=<?php echo rand();?>" class="contactpagecaptchass" id='captchaimg'/>
<p class="change"><a href="javascript: refreshCaptcha();" class="clickto">click to change</a></p>
</div>
<div class="contactcaptcha">
<input type="text" class="form-control" name="captcha" placeholder="Captcha" style="background-color: #f4f4f4;border: none;">
</div>
</div>
<button type="submit" class="btn btn-success contact1">Submit</button>
</div>
答案 0 :(得分:0)
from()
的第一个参数必须是电子邮件地址。如果您喜欢添加名称,请将其作为第二个参数传递。请尝试关注from()
:
$from_email = 'yourfrom@example.com'; //fill with valid email that authorized in your email host
$this->email->from($from_email, $name);
我在codeigniter文档中找不到set_newline()
。也许有效,如果删除它!