I Tried to send email from codeigniter3 but it's showed my this error
The following SMTP error was encountered: 0 php_network_getaddresses: getaddrinfo failed: Name or service not known
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
I have a file on application/config/email.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
$config = array(
'protocol' => 'smtp', // 'mail', 'sendmail', or 'smtp'
'smtp_host' => 'smtp.example.com',
'smtp_port' => 465,
'smtp_user' => 'my email',
'smtp_pass' => 'pass',
'smtp_crypto' => 'ssl', //can be 'ssl' or 'tls' for example
'mailtype' => 'text', //plaintext 'text' mails or 'html'
'smtp_timeout' => '4', //in seconds
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
my controller is
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class EmailController extends CI_Controller {
public function __construct() {
parent:: __construct();
$this->load->helper('url');
}
public function index() {
$this->load->view('email/contact');
}
function send() {
$this->load->config('email');
$this->load->library('email');
$from = $this->config->item('smtp_user');
$to = $this->input->post('to');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
$this->email->set_newline("\r\n");
$this->email->from($from);
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send()) {
echo 'Your Email has successfully been sent.';
} else {
show_error($this->email->print_debugger());
}
}
}
my view is here
<!DOCTYPE html>
<html>
<head>
<title>CodeIgniter Send Email</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<h3>Use the form below to send email</h3>
<form method="post" action="<?=base_url('email')?>" enctype="multipart/form-data">
<input type="email" id="to" name="to" placeholder="Receiver Email">
<br><br>
<input type="text" id="subject" name="subject" placeholder="Subject">
<br><br>
<textarea rows="6" id="message" name="message" placeholder="Type your message here"></textarea>
<br><br>
<input type="submit" value="Send Email" />
</form>
</div>
</body>
</html>
Related
I can't understand what the problem may be:
I state that the code without the part of the attachment works
function submit(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'file' => 'mimes:pdf,doc,docx'
]);
$data = array(
'name_data' => $request->name,
'cognome_data' => $request->cognome,
'luogo_data' => $request->luogo,
'date_data' => $request->date,
'telefono_data' => $request->telefono,
'email_data' => $request->email,
'citta_data' => $request->citta,
'provincia_data' => $request->provincia,
'studio_data' => $request->studio,
'lingua_data' => $request->lingua,
'livello_data' => $request->livello,
'lingua2_data' => $request->lingua2,
'livello2_data' => $request->livello2,
'file_data' => $request->file,
'agree_data' => $request->agree
);
Mail::send('mail', $data, function($message) use ($request,$data){
$message->to('pipo#gmail.com', 'piooi')->subject('Send mail ' . $request->name);
$message->from($request->email, $request->name);
if ( isset($data['file_data']))
{
$message->attach($data['file_data']->getRealPath(), array(
'as' => $data['file_data']->getClientOriginalName(),
'mime' => $data['file_data']->getMimeType()));
}
});
Session::flash('success', 'Mail spedita con sucesso');
}
}
I put the piece of the form in question:
<form class="text-left form-email"action="#" enctype="multipart/form-data" method="POST">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupFileAddon01">Curriculum Vitae:</span>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" id="file" name="file"
aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label" for="inputGroupFile01">Seleziona il file</label>
</div>
</div>
the error that gives the summit is the following:
local.ERROR: Call to a member function getRealPath() on null {"exception":"[object] (Error(code: 0):
Change your mail part to this and see if it works
Mail::send('mail', $data, function($message) use ($request,$data){
$message->to('pipo#gmail.com', 'piooi')->subject('Send mail ' . $request->name);
$message->from($request->email, $request->name);
if($request->hasFile('file')){
$message->attach($request->file->getRealPath(), array(
'as' => $request->file->getClientOriginalName(),
'mime' => $request->file->getMimeType())
);
}
});
A PHP Error was encountered
Severity: 8192
Message: idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated
Filename: libraries/Email.php
Line Number: 1868
Backtrace:
File: /home/abc/public_html/isol/application/controllers/Home.php
Line: 261
Function: send
File: /home/abc/public_html/isol/index.php
Line: 315
Function: require_once
When ever I send email I get this error .
My code is below
$config = Array('mailtype' => 'html');
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->from(from_email,site_title);
$this->email->to('ammarkhan94x#gmail.com');
$this->email->subject(site_title.' - Account Registration');
$this->email->message('<p><b>Dear '.$fname.' '.$lname.',</b></p>'.
'<p>Thank You for registration on '.site_title.', Your account is created successfully please login & update your account details.</p>');
$result = $this->email->send();
This problem was addressed in an update.
You can read more about it here: https://github.com/bcit-ci/CodeIgniter/issues/5300
So you should upgrade CI's /system to the newest version and your problems should go away.
You could also probably silence depreciated warnings in the switch statement on top of the index.php page but that's more of a bandaid rather than a fix.
In Application/config/email.php
<?php
$config['email_conf'] = Array(
'protocol' => 'smtp',
'smtp_host' => '-your smtp port-',
'smtp_port' => -your smtp port-,
'smtp_user' => '-your email--',
'smtp_pass' => '-your smtp pass',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
In your controller
private function sendMail($mailBody) {
//en-tête de mail
$filename = 'assets/images/logo.png';
//loader les fichiers de configuration
$this->config->load('email');
$conf = $this->config->item('email_conf');
$this->load->library('email', $conf);
$this->email->set_newline("\r\n");
$this->load->library('email');
$this->email->from('mail address', 'your name');
$this->email->subject('Support Application');
$this->email->attach($filename);
$this->email->to('email dest');
$cid = $this->email->attachment_cid($filename);
$message = '<div><img src="cid:' . $cid . '" alt="photo1"/></div>';
$message .= $mailBody ;
//array à passer au template email
$data = array(
'userName' => $this->session->userdata('username'),
'email' => $this->session->userdata ('email'),
'message' => $message
);
$body = $this->load->view('email/template.php', $data, TRUE);
$this->email->message($body);
$this->email->set_crlf( "\r\n" );
$result = $this->email->send();
}
}
I created a template as a view in a new view folder views/email/template.php
<html lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Template contact / XXX</title>
</head>
<body>
<p>
<FONT face="Verdana" size = "4" ><?php echo $message; ?></FONT>
<FONT face="Verdana" size = "4" >Message envoyé par : <?php echo $userName; ?></FONT>
<FONT face="Verdana" size = "4" >test mail : <?php echo $email; ?></FONT>
</p>
</body>
</html>
Let me know if it's helpful.
i keep having "not match" authentication using manually login with my laravel
here is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Admin;
use Input;
use Validator;
use Auth;
class CobaLogin extends Controller
{
public function index(){
return view('adminlogin');
}
public function username(){
return 'username';
}
public function doLogin(Request $request){
$username = $request ->input('username');
$password = $request -> input('password');
if(Auth::attempt(['username' => $username, 'password' => $password])){
echo "yes match";
}else{
echo "not match";
}
}
}
and here is my model from admin table
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable{
protected $table = "admin";
}
here is my route
Route::get('loginadmin', 'CobaLogin#index');
Route::post('dologin', 'CobaLogin#doLogin');
here is my login blade file
<div class="wrapper">
<form class="form-signin" action="{{ url('dologin')}}" method="POST">
{{ csrf_field() }}
<br/><h2 class="form-signin-heading text-center">ADMIN LOGIN</h2>
<input type="text" class="form-control" name="username" placeholder="Email Address" required="" autofocus="" />
<input type="password" class="form-control" name="password" placeholder="Password" required=""/>
<label class="checkbox">
<input type="checkbox" value="remember-me" id="rememberMe" name="rememberMe"> Remember me
</label>
<button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
</form>
</div>
here is my auth.php file
'providers' => [
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
here is my register function
public function store(Request $request){
$data=Input::except(array('_token'));
// var_dump($data);
$rule=array(
'username' => 'required|unique:admin',
'password' => 'required|min:6',
'cpassword' => 'required|same:password',
'email' => 'required|email',
);
$message=array(
'username' => 'This username already taken',
'cpassword.min'=>'the password must at least 6 characters',
'cpassword.same'=>'the password and confirm password must same',
);
$validator= validator::make($data,$rule,$message);
if($validator->fails()){
return redirect() -> to('adduser')->withErrors($validator);
}else{
$tambah = new Admin();
$tambah->username = $request['username'];
$tambah->password = Hash::make($request['password']);
$tambah->email = $request['email'];
$tambah->alamat = $request['alamat'];
$tambah->lebih = $request['lebih'];
$tambah->save();
return redirect()->to('users')->with('success','Your Data Has Been Added');
}
}
and here is
my database table
i have use Hash for encrypt password, and i think nothing wrong with regist, what do i miss?
i keep having "not match" as the answer from this
please help master
By default, Laravel uses User model for authentication. You can customize it by editing config/auth.php:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
I have created an application in CodeIgniter. I'm trying to insert validation in a form. As I will have many forms in my application, so I'm trying to do it with validation config file.
I have created config file in application/config/form_validation.php. I have also autolaoded config file by adding $autoload['config'] = array('form_validation');
Still, my Validation is not working properly it is always returning boolean false
Below code for the same
form_validation.php file
<?php
$config= array(
'Reservation' => array(
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'required|max_length[50]|min_length[4]|trim|htmlspecialchars|alpha'
),
array(
'field' => 'date',
'label' => 'Date',
'rules' => 'required|trim|htmlspecialchars',
'errors'=> ['required'=>'You must provide a %s for the reservation.']
),
array(
'field' => 'no_people',
'label' => 'No of People',
'rules' => 'required|trim|htmlspecialchars'
)
)
);
?>
index.php - View
<div class="book-reservation wow slideInUp" data-wow-duration="1s" data-wow-delay=".5s">
<?php echo "<script>alert('".validation_errors()."')</script>"; ?>
<?php echo form_open('Entry/Reservation'); ?>
<div class="col-md-12 form-left">
<label><i class="glyphicon glyphicon-calendar"></i>Enter Name:</label>
<input type="text" name='name' value="<?php echo set_value('name'); ?>">
</div>
<div class="col-md-3 form-left">
<label><i class="glyphicon glyphicon-calendar"></i> Date :</label>
<input type="date" name='date' value="<?php echo set_value('date'); ?>">
</div>
<div class="col-md-3 form-left">
<label><i class="glyphicon glyphicon-user"></i> No.of People :</label>
<select class="form-control" name='no_people' value="<?php echo set_value('no_people'); ?>">
<option>1 Person</option>
<option>2 People</option>
<option>3 People</option>
<option>4 People</option>
<option>5 People</option>
<option>More</option>
</select>
</div>
</div>
Entry.php - Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Entry extends CI_Controller {
public function __construct()
{
Parent::__construct();
$this->load->model('Restaurant');
}
public function index()
{
$this->load->view('index');
}
public function Reservation()
{
var_dump($this->form_validation->run('Reservation'));
die;
if($this->form_validation->run() == FALSE)
{
redirect();
}
else
{
#getting values of form
$data['a'] = array(
'Person_Name' => $this->input->post('name'),
'Phone_Number' => $this->input->post('phone'),
'Reservation_Date' => $this->input->post('date'),
'No_People' => $this->input->post('no_people'),
'Reservation_Time' => $this->input->post('time')
);
$data['a'] = $this->security->xss_clean($data['a']); #cleaning data
#calling model
//echo"<pre>";print_r($data['a']);
//die;
if($this->Restaurant->insert_entries($data['a']))
{
$this->session->set_flashdata('message','Your Reservation has accepted, We will call you back shortly.');
redirect();
}
else
{
$this->session->set_flashdata('message','Some techincal issue occured, your reservation did not complete.');
}
}
}
}
Error in below code as it is not redirecting anywhere
if($this->form_validation->run() == FALSE)
{
redirect();
}
Correct Answer:
if($this->form_validation->run() == FALSE)
{
$this->load->view('index');
}
now i'm trying user images(user_pic,background_image) upoload.
but my codes not working.
should i be doing something different? Not really sure what's going wrong.
Here my html code
<form action="<?=base_url();?>edit/up_profile/" method="post" enctype="multipart/form-data">
<input type="file" name="pic_file">
<input type="file" name="pic_bgfile" />
<button type="submit">
my controller code is
if($this->form_validation->run() === false){
}else{
$config = array(
'upload_path' => "./images/u/photo",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload',$config);
if($this->upload->do_upload('pic_file')){
$data['profile_image'] = $this->upload->data();
}else if($this->upload->do_upload('pic_bgfile')){
$data['pic_bgfile'] = $this->upload->data();
}
$this->load->model('user_model');
$data = array(
'user_id' => $this->session->userdata('user_id'),
'info_tit' => $this->input->post('info_tit'),
'scope' => $this->input->post('scope')
);
$this->user_model->p_update($data);
//redirect
redirect('/edit/profile/', 'location', 301);
}
You should use CodeIgniter's own upload class instead of using html code.
In the view, or rather, the .html file:
<?php
echo form_open_multipart('user/upload_picture');
?>
<input type="file" name="userfile" />
<br /><br />
<input type="submit" value="submit" />
</form>
Noted that in 'user/upload_picture', 'user' is the name of the controller, 'upload_picture' is the method in 'user'.
In the 'upload_picture' method:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg';
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['file_name'] = 'Apink.jpg';
$this->load->library('upload', $config);
$this->upload->do_upload();
The View :
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?> <!-- Error Message will show up here -->
<?php echo form_open_multipart('upload_controller/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
</body>
</html>
The Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function file_view(){
$this->load->view('file_view', array('error' => ' ' ));
}
public function do_upload(){
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success',$data);
}
else
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('file_view', $error);
}
}
}
?>
Note : You can change preferences depending upon the file you wish to upload.
The Page to show uploaded file:
<html>
<head>
<title>Upload File Specification</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?></p>
</body>
</html>
You can use clear function after first image upload
unset($config);
$this->image_lib->clear();
See CI Manual
https://codeigniter.com/user_guide/libraries/image_lib.html