I've a registration form and and want to send email to "To Email" and at the same time want to insert user record in db , insertion is successfull but email is not sending using codeigniter here is my code snippet from model and controller resp.
model:
public function add_user()
{
$data=array(
'username'=>$this->input->post('user_name'),
'first_name' => $this->input->post('fname'),
'last_name' => $this->input->post('lname'),
'email'=>$this->input->post('email_address'),
'password'=>md5($this->input->post('password'))
);
return $this->db->insert('user',$data);
//return $data;
}
and here is controller code:
$this->user_model->add_user();
$this->thank();
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'abc#gmail.com',
'smtp_pass' => '******',
'smtp_timeout' => '4',
'mailtype' => 'text',
'charset' => 'iso-8859-1'
);
$this->load->library('email',$config);
$this->email->from('mubbashir.azez#gmail.com');
$this->email->to($data['Email']);
$this->email->subject('test subject');
$this->email->message('hello CI');
$this->email->send();
echo $this->email->print_debugger();
here is view code:
<label for="email_address">Your Email:</label>
<?php echo form_error('email_address','<div class="error">', '</div>');?>
<input type="text" id="email_address" name="email_address" value="<?php echo set_value('email_address'); ?>" />
</p>
print debugger result:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: controllers/user.php
Line Number: 82
A PHP Error was encountered
Severity: Warning
Message: fgets() [function.fgets]: SSL: The operation completed successfully.
Filename: libraries/Email.php
Line Number: 1869
220 mx.google.com ESMTP ea4sm1055346wib.7 - gsmtp
hello: 250-mx.google.com at your service, [139.190.86.52]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
250-ENHANCEDSTATUSCODES
250 CHUNKING
from: 250 2.1.0 OK ea4sm1055346wib.7 - gsmtp
to: 555 5.5.2 Syntax error. ea4sm1055346wib.7 - gsmtp
The following SMTP error was encountered: 555 5.5.2 Syntax error. ea4sm1055346wib.7 - gsmtp
data: 503 5.5.1 RCPT first. ea4sm1055346wib.7 - gsmtp
The following SMTP error was encountered: 503 5.5.1 RCPT first. ea4sm1055346wib.7 - gsmtp
The following SMTP error was encountered:
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
User-Agent: CodeIgniter
Date: Wed, 12 Feb 2014 03:26:52 +0500
From: <mubbashir.azez#gmail.com>
Return-Path: <mubbashir.azez#gmail.com>
Subject: =?utf-8?Q?test_subject?=
Reply-To: "mubbashir.azez#gmail.com" <mubbashir.azez#gmail.com>
X-Sender: mubbashir.azez#gmail.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <52faa3ac53dcf#gmail.com>
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="B_ALT_52faa3ac53dd9"
This is a multi-part message in MIME format.
Your email application may not support this format.
--B_ALT_52faa3ac53dd9
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
hello CI
--B_ALT_52faa3ac53dd9
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
hello CI
--B_ALT_52faa3ac53dd9--
The return value in your model must be
$data
and in your controller code:
replace this
$this->user_model->add_user();
with
$data = $this->user_model->add_user();
You have to put this into your controller:
$data=array(
'username'=>$this->input->post('user_name'),
'first_name' => $this->input->post('fname'),
'last_name' => $this->input->post('lname'),
'email'=>$this->input->post('email_address'),
'password'=>md5($this->input->post('password'))
);
Pass this $data to your model:
$this->user_model->add_user($data);
In your model also write:
add_user($data);
Then change this line also
$this->email->to($data['Email']);
to
$this->email->to($data['email']);
Related
i have use CodeIgniter 2.2.6
i am trying to sent email but i can't get error and not received email
<?php
class email_otp_demo extends CI_Controller
{
public function index()
{
$this->load->library('email');
$config = Array(
'protocol' => 'smpt',
'mailpath' => '/usr/sbin/sendmail,
//'mailpath' => 'www.google.com', also try
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => 587,
//'smtp_port' => 456, also try
'smtp_user' => 'my.email#gmail.com',
'smtp_pass' => 'mypassword',
'smtp_timeout'=>50,
'mailtype' => 'text',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE,
'newline' =>'\r\n'
);
// glnlwboynexfjqxj
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$this->email->set_crlf("\r\n");
$this->email->from('my.email#gmail.com', 'Social media website');
$this->email->to('sender#yahoo.com');
//$this->email->to('sender#gmail.com'); also try this
// $this->email->cc('another#another-example.com');
// $this->email->bcc('them#their-example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
echo $this->email->send();
echo "<hr>";
echo $this->email->print_debugger();
}
}
?>
i have get following output but not received email
1
Your message has been successfully sent using the following protocol: mail
User-Agent: CodeIgniter
Date: Sun, 4 Dec 2022 16:53:27 +0530
From: "Social media website" <renish.batada#gmail.com>
Return-Path: <renish.batada#gmail.com>
Reply-To: "renish.batada#gmail.com" <renish.batada#gmail.com>
X-Sender: renish.batada#gmail.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <638c832f6b8c0#gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
=?iso-8859-1?Q?Email_Test?=
Testing the email class.
i have also try to enable and disable to step verification
but not received email
i use app password and original password but not workworking
Code is working but can't send many files.... only images can be sended controller
public function sende(){
$from = $_POST['from'];
$address = $_POST['address'];
$to_name = $_POST['to_name'];
$to_email = $_POST['to_email'];
$cc = $_POST['cc'];
$subject = $_POST['subject'];
$mesg = $_POST['mesg'];
$start = $_POST['start'];
$end = $_POST['end'];
$mid1 = $_POST['mid1'];
$mid2 = $_POST['mid2'];
$message = $start.$mid1.$mesg.$mid2.$end;
$config = array(
'useragent' => 'CodeIgniter',
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => '465',
'smtp_user' => '***',
'smtp_pass' => '**',
'mailtype' => 'html',
'charset' => 'utf-8',
'newline' => '\r\n'
);
$this->load->library('email',$config);
$this->email->set_newline('\r\n');
$this->email->from($address,$from);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
$this->email->cc($cc);
$path = $this->config->item('server_root');
$file = $path.'/mshaadi/';
$this->load->library('upload');
$m = count($_FILES['atta']['name']);
$files = $_FILES;
for($i=0; $i< $m; $i++){
$_FILES['atta']['name']= $files['atta']['name'][$i];
$_FILES['atta']['type']= $files['atta']['type'][$i];
$_FILES['atta']['tmp_name']= $files['atta']['tmp_name'][$i];
$_FILES['atta']['error']= $files['atta']['error'][$i];
$_FILES['atta']['size']= $files['atta']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload('atta');
$a = $files['atta']['name'][$i];
$mm = base_url().'images/'.$a;
$this->email->attach($mm);
}
if($this->email->send()){
redirect('Email');
}else{
show_error($this->email->print_debugger());
}
}
private function set_upload_options(){
$config = array();
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png|pdf|sql|docx|pptx';
$config['max_size'] = '10000';
$config['overwrite'] = TRUE;
return $config;
}
Error while sending multiple attachment of docx, pptx
220-gator3150.hostgator.com ESMTP Exim 4.85 #2 Thu, 19 Nov 2015 00:10:43 -0600 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
220-gator3150.hostgator.com ESMTP Exim 4.85 #2 Thu, 19 Nov 2015 00:10:43 -0600 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
hello: 250-gator3150.hostgator.com Hello 127.0.0.1 [112.196.141.163]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
250 HELP
from: 250 OK
to: 501 <>: missing or malformed local part
The following SMTP error was encountered: 501 <>: missing or malformed local part
data: 503-All RCPT commands were rejected with this error:
503-501 <>: missing or malformed local part
503 Valid RCPT command must precede DATA
The following SMTP error was encountered: 503-All RCPT commands were rejected with this error: 503-501 <>: missing or malformed local part 503 Valid RCPT command must precede DATA
500 unrecognized command
The following SMTP error was encountered: 500 unrecognized command
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
User-Agent: CodeIgniter
Date: Thu, 19 Nov 2015 07:10:37 +0100
From: "" <>
Return-Path: <>
Subject: =?ISO-8859-1?Q??=
Reply-To: "" <>
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <564d67dddeddc>
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="B_ALT_564d67dddeddc"
This is a multi-part message in MIME format.
Your email application may not support this format.
--B_ALT_564d67dddeddc
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
--B_ALT_564d67dddeddc
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
--B_ALT_564d67dddeddc--
$this->load->library('email');
$this->load->helper('path');
$this->load->helper('directory');
//setting path to attach files
$path = set_realpath('assets/your_folder/');
$file_names = directory_map($path);
$this->email->clear(TRUE);
$this->email->to($address);
$this->email->from('your#example.com');
$this->email->subject('Here is your info '.$name);
$this->email->message('Hi Here is the info you requested.');
foreach($file_names as $file_name)
{
$this->email->attach($file_name);
}
$this->email->send();
For multiple attachments you can use the same function multiple times,On your code you haven't used multiple times and not given loop too.
Try using the full path instead of just the url-
$this->email->attach('C:\Users\xyz\Desktop\images\abc.png');
or
$attched_file= $_SERVER["DOCUMENT_ROOT"]."/uploads/".$file_name;
$this->email->attach($attched_file);
also try changing the charset as-
$config['charset'] = 'iso-8859-1';
Codeigniter send email with attach file
https://ellislab.com/codeigniter/user-guide/libraries/email.html
i was testing to send email by using codeigniter and i successfully send the email,but when i checked my inbox,i didn't find my email i send before.
here's controller:
$this->load->library('email');
$this->email->from('email#yahoo.com', 'my name');
$this->email->to('email#yahoo.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
success message:
Your message has been successfully sent using the following protocol: mail
From: "XXXXXXXXXXXXXX"
Return-Path:
Reply-To: "XXXXXXX#yahoo.com"
X-Sender: XXXXXXX#yahoo.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <XXXXXXX#yahoo.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
=?utf-8?Q?Email_Test?=
Testing the email class.
You may check your Spam folder in your email account.
Sometimes gmail/yahoo set email as spam when the mail send from local server.
Try using the following config...
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'your host',
'smtp_port' => 465,
'smtp_user' => 'your email address',
'smtp_pass' => 'password',
'mailtype' => 'html',
'mailpath' => '/usr/sbin/sendmail',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE;
);
$this->load->library('email', $config);
Depending on your server, you should also check your mail server logs to see if the email actually made it off the box. Sometimes you might have success sending it, as the mail is passed to EXIM on your server, but the mail might die in a queue.
If this is a linux box, typically start looking in /var/log/exim4/ etc; (of course depends on your mail server setup).
i m trying to send email It properly working on localhost
when i live it on server it give me warning
i m using codeigniter here is the code
public function send_me($from,$name,$toEmail,$subject,$msg){
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxxxxxxxxxx',//user name herer
'smtp_pass' => 'xxxxxxxx', //password hre
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from($from,$name);
$this->email->to($toEmail);//to address here
$this->email->subject($subject);
$this->email->message($msg);
if($this->email->send())
return true;
else
return false;
}
and warning are
A PHP Error was encountered
Severity: Warning
Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.googlemail.com:465 (Connection timed out)
Filename: libraries/Email.php
Line Number: 1689
A PHP Error was encountered
Severity: Warning
Message: fwrite() expects parameter 1 to be resource, boolean given
Filename: libraries/Email.php
Line Number: 1846
I have answered several issues like this one. If your local email is working fine then follow the steps below:
To use Google SMTP in CodeIgniter you need to make 2 (two) changes into your Gmail account setting: (N.B. Please be aware that it is now easier for an attacker to break into your account -says Google)
Set off 2-step Verification.
Allow less secure apps: ON (or Enable)
Now use 'smtp_host' as ssl://smtp.gmail.com instead of smtp.googlemail.com
Hope upcoming users can get this help too.
I have a problem in sending mails using CI. I used the "sendmail" protocol to send mail but it is being filtered as spam. I used the SMTP protocol to solve the problem, but it's not getting sent.
My code is as follows:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '*******#gmail.com',
'smtp_pass' => '********',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$message = "test mail";
$this->email->set_newline("\r\n");
$this->email->from('dsiddharth2#gmail.com', 'St. Maries');
$this->email->to("dsiddharth2#gmail.com");
$this->email->subject('Contact - St. Marians Belguam');
$this->email->message($message);
if($this->email->send())
{
echo "mail sent successfully";
}
else
{
show_error($this->email->print_debugger());
}
When I send the email using "smtp" protocol in CI, I get the following error:
A PHP Error was encountered
Severity: Warning
Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.googlemail.com:465 (Connection timed out)
Filename: libraries/Email.php
Line Number: 1673
and other list of errors...
I asked the server admin to enable the "openssl". It's been already taken care of and is enabled.
If you want to try it out, please visit this link and check for yourself.
If it's not working, check your php.ini file. Change this line
;extension=php_openssl.dll
to
extension=php_openssl.dll
It means remove the comment from the line in php.ini file.