Codeigniter email sending is not working [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I'm trying to send an email with an codeigniter contact us form but its not sending the email.
Yes I know this question has been asked before but I just want to know if I don't have any wrong code.
This is my controller file (Contactform.php) :
<?php error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
class Contactform extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email'));
}
function index()
{
//set validation rules
//$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email');
//$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
//$this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');
//run validation on form input
if ($this->form_validation->run() == FALSE)
{
//validation fails
$this->load->view('contact_form_view');
}
else
{
//get the form data
$name = $this->input->post('name');
$from_email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
//zet to_email naar welk email je het contact form naar wilt laten sturen
$to_email = 'ferran1004#gmail.com';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'ferra#gmail.com';
$config['smtp_pass'] = 'tom10';
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
$this->load->library('email', $config);
$this->email->initialize($config);
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send())
{
// mail sent
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
redirect('contactform/index');
}
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('contactform/index');
}
}
}
//custom validation function to accept only alphabets and space input
function alpha_space_only($str)
{
if (!preg_match("/^[a-zA-Z ]+$/",$str))
{
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
And this is my view form file (contact_form_view.php):
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 well">
<?php $attributes = array("class" => "form-horizontal", "name" => "Contactform");
echo form_open("Contactform/index", $attributes);?>
<fieldset>
<legend>Contact Form</legend>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Name</label>
</div>
<div class="col-md-12">
<input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="email" class="control-label">Email ID</label>
</div>
<div class="col-md-12">
<input class="form-control" name="email" placeholder="Your Email ID" type="text" value="<?php echo set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="subject" class="control-label">Subject</label>
</div>
<div class="col-md-12">
<input class="form-control" name="subject" placeholder="Your Subject" type="text" value="<?php echo set_value('subject'); ?>" />
<span class="text-danger"><?php echo form_error('subject'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="message" class="control-label">Message</label>
</div>
<div class="col-md-12">
<textarea class="form-control" name="message" rows="4" placeholder="Your Message"><?php echo set_value('message'); ?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input name="submit" type="submit" class="btn btn-primary" value="Send" />
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
</div>
This is my email.php in my config folder: (I don't know if I need this file because I have the same code in my controller)
<?php
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
$config['smtp_port'] = '465';
$config['smtp_user'] = 'xxxxxxxx#gmail.com';
$config['smtp_pass'] = 'xxxxxxxx';
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard
?>

you add email.php in config folder
<?php
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
$config['smtp_port'] = '465';
$config['smtp_user'] = 'xxxxxxxx#gmail.com';
$config['smtp_pass'] = 'xxxxxxxx';
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard
?>
then auto load config and library in autoload.php
$autoload['libraries'] = array('email');
$autoload['config'] = array('email');
and you send mail using this
$to_email = 'ferran1004#gmail.com';
$message = "email message";
$this->email->from('noreply#gmail.com','Admin');
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send() == TRUE)
{
log_message('debug', 'Email Send SuccessFully>>>>>>>>>>>');
}
else
{
log_message('debug', 'Email Not Send>>>>>>>>>>>>>>>');
}

Related

My Email is not working am getting error in my contact us page

My view
<div class="col-sm-6">
<div class="single_contant_left padding-top-90 padding-bottom-90">
<?php $attributes = array("class" => "form-horizontal", "name" => "contactform");
echo form_open("contactform/contactus", $attributes);?>
<div id="formid">
<div class="col-lg-8 col-md-8 col-sm-10 col-lg-offset-2 col-md-offset-2 col-sm-offset-1">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<input type="text" class="form-control" name="name" value="<?php echo set_value('name'); ?>" placeholder="First Name" required>
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" value="<?php echo set_value('email'); ?>" required>
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input type="text" class="form-control" name="subject" placeholder="Subject" value="<?php echo set_value('subject'); ?>" required>
<span class="text-danger"><?php echo form_error('subject'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="7" placeholder="Message"><?php echo set_value('message'); ?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>
<div class="">
<input type="submit" name="submit" value="SEND MESSAGE" class="btn btn-lg">
</div>
</div>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
</div>
My controller
<?php
class Contactform extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email'));
}
function contactus()
{
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required');
$this->form_validation->set_rules('message', 'Message', 'trim|required');
//run validation on form input
if ($this->form_validation->run() == FALSE)
{
//validation fails
$this->load->view('header_vw');
$this->load->view('center_vw');
$this->load->view('footer_vw');
}
else
{
//get the form data
$name = $this->input->post('name');
$from_email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
//set to_email id to which you want to receive mails
$to_email = 'alshoja#gmail.com';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = '....#gmail.com';
$config['smtp_pass'] = 'als....#......';
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
$this->load->library('email', $config);
$this->email->initialize($config);
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send())
{
// mail sent
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
redirect('contactform/contactus');
}
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('contactform/contactus');
}
}
}
//custom validation function to accept only alphabets and space input
function alpha_space_only($str)
{
if (!preg_match("/^[a-zA-Z ]+$/",$str))
{
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else
{
return TRUE;
}
}
}
Am getting error while sending message my server is configured good but mail is not sending from my site, how can I configure shall I want to give my password and username for sending mail in configuring section or else how can configure that please help :(
Am confused with them to address and from address in email configuration :(
Though I have done this several times, sending email through another provider is always a painful task.
Though this maybe ok,
$config['newline'] = "\r\n"; //use double quotes
but, Sending email with gmail smtp with codeigniter email library suggests
$this->email->set_newline("\r\n");
Also are you sure you allowed less secure apps in gmail?

Codeigniter Contact form submit returning timeout

Hi I am wondering if anyone could give me some insight into why my Codeigniter contact form just times out on submit?
I understand this is a beginner error but I feel like it is something I am completely overlooking. I have provided the controller and view.
I do have email.php setup in the Config file as well, with the various helpers and libraries loaded. Thank you in advance!
Controller
<?php
class contactform extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email'));
}
function index()
{
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
$this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');
//run validation on form input
if ($this->form_validation->run() == FALSE) {
//validation fails
$this->load->view('contact_form_view');
} else {
//get the form data
$name = $this->input->post('name');
$from_email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
//set to_email id to receive emails
$to_email = 'littleliongirldesigns#gmail.com';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'email';
$config['smtp_pass'] = 'password';
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
$this->load->library('email', $config);
$this->email->initialize($config);
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send()) {
// mail sent
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
redirect('contactform/index');
} 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('contactform/index');
}
}
}
//custom validation function to accept only alphabets and space input
function alpha_space_only($str)
{
if (!preg_match("/^[a-zA-Z ]+$/",$str)) {
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
} else {
return TRUE;
}
}
}
?>
View
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 well">
<?php $attributes = array("class" => "form-horizontal", "name" => "contactform");
echo form_open("contactform/index", $attributes);?>
<fieldset>
<legend>Contact Form</legend>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Name</label>
</div>
<div class="col-md-12">
<input class="form-control" name"name" placeholder="Your Name" type="text" value="<?php echo set_value('name'); ?>"/>
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="email" class="control-label">Email ID</label>
</div>
<div class="col-md-12">
<input class="form-control" name="email" placeholder="Your email" type="text" value="<?php echo set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email');?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="subject" class="control-label">Subject</label>
</div>
<div class="col-md-12">
<input class="form-control" name="subject" placeholder="Subject" type="text" value="<?php echo set_value('subject'); ?>"/>
<span class="text-danger"><?php echo form_error('subject');?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="message" class="control-label">Message</label>
</div>
<div class="col-md-12">
<textarea class="form-control" name="message" rows="4" placeholder="Your Message"><?php echo set_value('message');?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input name="submit" type="submit" class="btn btn-primary" value="Send" />
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
</div>

Need help creating registration/login form in CodeIgniter

So that we're on the same page, I'm following along with this tutorial:
http://www.kodingmadesimple.com/2015/08/create-simple-registration-form-codeigniter-email-verification.html
Besides the starting CodeIgniter files, above is exactly what my code looks like. But here it is for those who can't view the link:
I have the model:
<?php
class user_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//insert into user table
function insertUser($data)
{
return $this->db->insert('user', $data);
}
//send verification email to user's email id
function sendEmail($to_email)
{
$from_email = 'team#mydomain.com'; //change this to yours
$subject = 'Verify Your Email Address';
$message = 'Dear User,<br /><br />Please click on the below activation link to verify your email address.<br /><br /> http://www.example.com/user/verify/' . md5($to_email) . '<br /><br /><br />Thanks<br />Mydomain Team';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.mydomain.com'; //smtp host name
$config['smtp_port'] = '465'; //smtp port number
$config['smtp_user'] = $from_email;
$config['smtp_pass'] = '********'; //$from_email password
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
$this->email->initialize($config);
//send mail
$this->email->from($from_email, 'Mydomain');
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
return $this->email->send();
}
//activate user account
function verifyEmailID($key)
{
$data = array('status' => 1);
$this->db->where('md5(email)', $key);
return $this->db->update('user', $data);
}
}
?>
The Controller:
<?php
class user extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email'));
$this->load->database();
$this->load->model('user_model');
}
function index()
{
$this->register();
}
function register()
{
//set validation rules
$this->form_validation->set_rules('fname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
$this->form_validation->set_rules('lname', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email|is_unique[user.email]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[cpassword]|md5');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required');
//validate form input
if ($this->form_validation->run() == FALSE)
{
// fails
$this->load->view('user_registration_view');
}
else
{
//insert the user registration details into database
$data = array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
// insert form data into database
if ($this->user_model->insertUser($data))
{
// send email
if ($this->user_model->sendEmail($this->input->post('email')))
{
// successfully sent mail
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully Registered! Please confirm the mail sent to your Email-ID!!!</div>');
redirect('user/register');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('user/register');
}
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('user/register');
}
}
}
function verify($hash=NULL)
{
if ($this->user_model->verifyEmailID($hash))
{
$this->session->set_flashdata('verify_msg','<div class="alert alert-success text-center">Your Email Address is successfully verified! Please login to access your account!</div>');
redirect('user/register');
}
else
{
$this->session->set_flashdata('verify_msg','<div class="alert alert-danger text-center">Sorry! There is error verifying your Email Address!</div>');
redirect('user/register');
}
}
}
?>
And the View:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CodeIgniter User Registration Form Demo</title>
<link href="<?php echo base_url("bootstrap/css/bootstrap.css"); ?>" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<?php echo $this->session->flashdata('verify_msg'); ?>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-default">
<div class="panel-heading">
<h4>User Registration Form</h4>
</div>
<div class="panel-body">
<?php $attributes = array("name" => "registrationform");
echo form_open("user/register", $attributes);?>
<div class="form-group">
<label for="name">First Name</label>
<input class="form-control" name="fname" placeholder="Your First Name" type="text" value="<?php echo set_value('fname'); ?>" />
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
<div class="form-group">
<label for="name">Last Name</label>
<input class="form-control" name="lname" placeholder="Last Name" type="text" value="<?php echo set_value('lname'); ?>" />
<span class="text-danger"><?php echo form_error('lname'); ?></span>
</div>
<div class="form-group">
<label for="email">Email ID</label>
<input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php echo set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group">
<label for="subject">Password</label>
<input class="form-control" name="password" placeholder="Password" type="password" />
<span class="text-danger"><?php echo form_error('password'); ?></span>
</div>
<div class="form-group">
<label for="subject">Confirm Password</label>
<input class="form-control" name="cpassword" placeholder="Confirm Password" type="password" />
<span class="text-danger"><?php echo form_error('cpassword'); ?></span>
</div>
<div class="form-group">
<button name="submit" type="submit" class="btn btn-default">Signup</button>
<button name="cancel" type="reset" class="btn btn-default">Cancel</button>
</div>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
</div>
</div>
</div>
</body>
I've set up my database in mySQL and my form comes out just fine when connecting to my local server.
But upon filling out the form and hitting "Sign Up" I hit this error in Safari:
Any idea what could be causing this and how to fix it?
If your using codeigniter 3
You have your base url blank and that is why ::1 you can leave it blank will work most of the time but you may run it to error.
$config['base_url'] = '';
Set your base url.
$config['base_url'] = 'http://localhost/project/';
Or A Live Domain Example
$config['base_url'] = 'http://www.example.com/';
Update:
Check your controllers etc make sure your file name has first letter upper case and same with class. Example: User.php and class User extends CI_Controller {}

I can't get this form to work either from localhost or online using codeigniter

I have the Contact Me form that I can't get to send me an email. I am still a newbie but I don't see anything wrong. Help me out guys.
Here's the contactform controller
<?php
class contactform extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email'));
}
function index() {
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
$this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');
//run validation on form input
if ($this->form_validation->run() == FALSE) {
//validation fails
//$this->load->view('contact_view');
$this->load->view('header_view');
$this->load->view('nav_view');
$this->load->view('contact_view');
$this->load->view('footer_view');
}
else {
//get the form data
$name = $this->input->post('name');
$from_email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
//set to_email id to which you want to receive mails
$to_email = 'mygmail#gmail.com';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'mygmail#gmail.com';
$config['smtp_pass'] = 'mypass';
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
//$this->load->library('email', $config);
$this->email->initialize($config);
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send())
{
// mail sent
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
redirect('contactform/index');
}
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('contactform/index');
}
}
}
//custom validation function to accept only alphabets and space input
function alpha_space_only($str) {
if (!preg_match("/^[a-zA-Z ]+$/",$str)) {
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else {
return TRUE;
}
}
}
?>
and here is my contact_view
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 well">
<?php $attributes = array("class" => "form-horizontal", "name" => "contactform");
echo form_open("contactform/index", $attributes);?>
<fieldset>
<legend>Contact Form</legend>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Name</label>
</div>
<div class="col-md-12">
<input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="email" class="control-label">Email ID</label>
</div>
<div class="col-md-12">
<input class="form-control" name="email" placeholder="Your Email ID" type="text" value="<?php echo set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="subject" class="control-label">Subject</label>
</div>
<div class="col-md-12">
<input class="form-control" name="subject" placeholder="Your Subject" type="text" value="<?php echo set_value('subject'); ?>" />
<span class="text-danger"><?php echo form_error('subject'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="message" class="control-label">Message</label>
</div>
<div class="col-md-12">
<textarea class="form-control" name="message" rows="4" placeholder="Your Message"><?php echo set_value('message'); ?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input name="submit" type="submit" class="btn btn-primary" value="Send" />
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
I already checked my codes. But the problem is, I can't see why it cant send. I even tried to upload it to my website, but it still won't let me send an email.

PHPmailer setting From email the same as to email

I'm using PHP mailer to build a contact form and bear in mind I'm new to PHP. I have the form sending nicely, however, in the email that I receive the From address does not match the email that the user inputs into the contact form and is instead displaying the same email as the recipient. I'm 100% sure I'm entering the correct email into the live contact form. I have replaced my sensitive information with $$$$$$$$$
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
if ($name == "" OR $email == "" OR $message == "") {
$error_message = "You must specify a value for name, email address, and message.";
}
if (!isset($error_message)) {
foreach( $_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
$error_message = "There was a problem with the information you entered.";
}
}
}
if (!isset($error_message) && $_POST["address"] != "") {
$error_message = "Your form submission has an error.";
}
require_once("../inc/phpmailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
if (!isset($error_message) && !$mail->ValidateAddress($email)){
$error_message = "You must specify a valid email address.";
}
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "$$$$$$$$$$$$$";
$mail->Password = "$$$$$$$$$$$$$";
$mail->SMTPSecure = 'tls';
if (!isset($error_message)) {
$mail->From = $email;
$mail->FromName = $name;
$mail->addAddress('$$$$$$$$$$$$$$', '$$$$$$$$$$l');
$mail->isHTML(true);
$mail->Subject = '$$$$$$$$$$| ' . $name;
$mail->Body = $message;
$mail->AltBody = $message;
if($mail->Send()) {
header("Location: ../contact/?status=thanks");
exit;
} else {
$error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
}
}
}
require_once("../inc/header.php"); ?>
<div class="section page">
<div class="wrapper">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="center-block">
<h1>Contact</h1>
</div>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<div class="alert alert-success">
<p>Thanks for the email! I’ll be in touch shortly!</p>
</div>
<?php } else { ?>
<?php
if (!isset($error_message)) {
echo '<p>I’d love to hear from you! Complete the form to send me an email.</p>';
} else {
?><div class="alert alert-warning"><?php
echo '<p>' . $error_message . '</p>';
?></div><?php
}
?>
</div>
</div>
<form class="form-horizontal" method="post" role="form">
<div class="form-group">
<label for="name" class="col-sm-4 control-label">Name</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" value="<?php if (isset($name)) { echo htmlspecialchars($name); } ?>">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-4 control-label">Email</label>
<div class="col-sm-4">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" value="<?php if(isset($email)) { echo htmlspecialchars($email); } ?>">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-4 control-label">Message</label>
<div class="col-sm-4">
<textarea class="form-control" id="message" name="message" rows="3"><?php if (isset($message)) { echo htmlspecialchars($message); } ?></textarea>
</div>
</div>
<div class="form-group" style="display:none;">
<label for="address" class="col-sm-4 control-label">Address</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="address" name="address" placeholder="Name">
<p>Humans: please leave this field blank.</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
<?php } ?>
</div>
</div>
<?php require_once("../inc/footer.php"); ?>
change this
$mail->From = $email;
$mail->FromName = $name;
$mail->addAddress('$$$$$$$$$$$$$$', '$$$$$$$$$$l');
to
$mail->setFrom($email, $name);
$mail->addAddress($EMAIL THAT ITS GOING TO, $NAME THATS ITS GOING TO);
Also see this link on sending FROM other address with GMAIL
https://support.google.com/mail/answer/22370?hl=en
It looks like you forgot to call the Sender variable:
public $Sender = '';
/**
* The Return-Path of the message.
* If empty, it will be set to either From or Sender.
* #type string
*/
https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php
Thanks for your replies guys. I did a bit of external research and it turns out that it is an issue with gmail itself. They don't allow you to use their smtp service with a from address different to your gmail account. I think it's because of spam.

Categories