Sending Email using SMTP gmail in CodeIgniter from website - php

I'm stuck on my problem to send email with codeigniter.
I've already done successful sending gmail, but the problem is the recipient and the sender are the same. I want this email received to me from sender in website,
There is my Controller :
public function index(){
$this->sendEmail();
$this->load->view('content_contact');
}
public function sendEmail(){
$this->load->helper(array('form', 'url'));
$this->load->library('email');
//konfigurasi email
$config = array();
$config['charset'] = 'utf-8';
$config['useragent'] = 'Codeigniter';
$config['protocol']= "smtp";
$config['mailtype']= "html";
$config['smtp_host']= "ssl://smtp.gmail.com";
$config['smtp_port']= 465;
$config['smtp_timeout']= "5";
$config['smtp_user']= "my_email$gmail.com";
$config['smtp_pass']= "my_pass";
$config['crlf']="\r\n";
$config['newline']="\r\n";
$config['wordwrap'] = TRUE;
$this->load->view('content_contact');
$this->email->initialize($config);
$this->email->from($this->input->post('from'));
$this->email->to("my_email#gmail.com");
$this->email->subject($this->input->post('subject'));
$this->email->message($this->input->post('isi'));
if($this->email->send()){
echo "pengiriman email Success";
}
else{
echo "pengiriman email Failed";
}
}
And This is my View : content_contact.php
content_contact.php

use below mentioned solution
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'my_email#gmail.com',
'smtp_pass' => 'yourpassword',
'mailtype' => 'html',
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->load->library('email', $config);

Related

why did my attempt to send my email not work status HTTP 303 See Other in codeigniter

why is my attempt to send the email unsuccessful, I can't see the error, all I get is the http status code 303 See More
In my opinion, what I'm doing is in accordance with the official documentation https://codeigniter.com/userguide3/libraries/email.html
the result i expect is an email sent
Can anyone help me?
, How can I solve this?
config/email.php
$config['settings'] = array(
'smtp_host' => 'smtp.mailtrap.io',
'smtp_port' => '2525',
'smtp_user' => '9cf16dcde1asdasde5f8',
'smtp_pass' => '079205e1asdasd26bf43',
'from_email' => 'no-reply#test.com',
'from_name' => 'Test',
'protocol' => 'smtp',
'smtp_crypto' => 'tls',
'mailtype' => 'html',
'charset' => 'utf-8',
'newline' => "\r\n",
'crlf' => "\r\n",
);
libraries/Ah_Email.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Ah_Email {
public function __construct()
{
$CI =& get_instance();
$CI->load->config('email', true);
$this->configs = $CI->config->config['email'];
$CI->load->library('email');
$this->email = $CI->email;
}
public function send($email_to, $subject, $template, $template_vars = array(), $settings = array())
{
$settings = array_merge(
$this->configs['settings'],
$settings
);
$this->email->initialize($settings);
$this->email->from($settings['from_email'], $settings['from_name']);
if (isset($settings['send_email']) && $settings['send_email']) {
$this->email->to($email_to);
}
$CI =& get_instance();
$this->email->subject($subject);
$this->email->message($CI->load->view('emails/' . $template, $template_vars, true));
$res = $this->email->send();
return $res;
}
}
My Controller
public function SendMail() {
$this->load->library('ah_email', 'ah_email');
$send = $this->ah_email->send('receiver#gmail.com', 'Informasi Kode OTP Akun Kamu', 'example', array(
'username' => 'Ndo Malau',
'message' => 'Hello World',
'mailtype' => 'text',
));
if($send) {
$result = redirect(base_url('Systems/Users/index/'), $this->session->set_flashdata('succ_msg', 'success, email has been sending!'));
} else {
$result = redirect(base_url('Systems/Users/index/'), $this->session->set_flashdata('err_msg', 'failed, error while email sending'));
}
return $result;
}

How to send email using php codeigniter?

<?php
class SendEmail extends Controller
{
function SendEmail()
{
parent::Controller();
$this->load->library('email'); // load the library
}
function index()
{
$this->sendEmail();
}
public function sendEmail()
{
// Email configuration
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com.',
'smtp_port' => 465,
'smtp_user' => 'xxxx', // change it to yours
'smtp_pass' => 'xxxx', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->from('xxxx', "Admin Team");
$this->email->to("xxxx");
$this->email->cc("xxxx");
$this->email->subject("This is test subject line");
$this->email->message("Mail sent test message...");
$data['message'] = "Sorry Unable to send email...";
if ($this->email->send()) {
$data['message'] = "Mail sent...";
}
// forward to index page
$this->load->view('index', $data);
}
}
?>
I get an error - I am doing it in codeigniter PHP Error was encountered
Severity: Compile Error
Message: Cannot redeclare SendEmail::sendEmail()
Filename: controllers/SendEmail.php
Line Number: 16
Backtrace:
Controller code
<?php
class SendEmail extends Controller
{
function __construct()
{
parent::__construct();
$this->load->library('email'); // load the library
}
function index()
{
$this->sendEmail();
}
public function sendEmail()
{
// Email configuration
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com.',
'smtp_port' => 465,
'smtp_user' => 'xxxx', // change it to yours
'smtp_pass' => 'xxxx', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->from('xxxx', "Admin Team");
$this->email->to("xxxx");
$this->email->cc("xxxx");
$this->email->subject("This is test subject line");
$this->email->message("Mail sent test message...");
$data['message'] = "Sorry Unable to send email...";
if ($this->email->send()) {
$data['message'] = "Mail sent...";
}
// forward to index page
$this->load->view('index', $data);
}
}
?>

Consult email from the database and send

I am trying to send an email with this function, if I write the email statically, it works correctly, but when trying to query it from the database, the $email_user variable does not get the value of the sql query. What am I doing wrong ?
Model
public function send(){
$this->db->select('email_user');
$this->db->from('users');
$this->db->where('id_user= "8" ');
$email_user = $this->db->get()->row();
$this->load->library('email');
$this->email->from('xxxx#gmail.com');
$this->email->to($email_user);
$this->email->subject('example');
$this->email->message('Hello World');
$this->email->send();
}
Make sure you have set your email config and also try like below with db
public function send(){
$id = '8';
$this->db->where('id_user', $id);
$query = $this->db->get('users');
$row = $query->row();
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '****',
'smtp_pass' => '****',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('xxxx#gmail.com', 'Admin');
$this->email->to($row->email_user);
$this->email->subject('example');
$this->email->message('Hello World');
$this->email->send();
}

Uploading a resume and sending an email displaying blank page in codeigniter php

Having a career page where i will be filling the details and attaching the resume and sending an email once the user fills the data along with the resume displaying blank page.
Working fine if we are not adding upload resume option.
var $image_path;
var $image_path_url;
function __construct()
{
parent::__construct();
$this->image_path =realpath(APPPATH . '..admin/images/blogimages');
$this->image_path_url = base_url().'admin/images/blogimages/thumbs';
}
function apply($email)
{
$name = $this->input->post('fullname');
$email = $this->input->post('email');
$phone = $this->input->post('mobilenumber');
if ( $_FILES AND $_FILES['image_path']['name'])
{
$file_name = $this->do_upload2();
if(is_array($file_name))
{
$error['imageerror'] = $file_name['error'];
}
else
$data['image_path']=$file_name;
}
if(!isset($data['image_path']) && !isset($error['imageerror']))
$error['imageerror'] ="Please Upload an image";
if(isset($error))return $error;
//set to_email id to which you want to receive mails
$to_email = 'yyyy#gmail.com';
$config=Array(
'protocol'=> 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com', //smtp host name
'smtp_port' => '465', //smtp port number
'smtp_user' => 'xxxxx#gmail.com',
'smtp_pass' => 'PASSWORD123', //$from_email password
'mailtype' =>'html',
'newline' =>"\r\n",
'crlf' =>"\r\n",
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = array();
$message[] = 'Fullname : '.trim($name).' ';
$message[] = 'Email : '.trim($email).' ';
$message[] = 'Mobile : '.trim($phone).' ';
//$message = implode(PHP_EOL, $message);
$message = implode('<br>', $message);
//send mail
$this->load->library('email',$config);
$this->email->from($email);
$this->email->to($to_email);
//$list = array();
$path = set_realpath('admin/images/blogimages/thumbs');
$this->email->message($message);
$this->email->attach($path . 'image_path');
$this->email->set_newline("\r\n");
$this->email->set_mailtype("html");
if ($this->email->send())
{
$this->flash->success('Thank you for applying to this post we will get back to you soon!</div>');
redirect('apply');
}
else
{
$this->flash->success('There is error in sending mail! Please try again later');
redirect('apply');
}
}
function do_upload2()
{
$config = array(
'allowed_types' => 'doc|docx|pdf',
'upload_path' => $this->image_path,
'max_size' => 20000,
'maintain_ratio'=>FALSE
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('image_path'))
{
return $error = array('error' => $this->upload->display_errors());
}
else
{
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' =>$this->image_path . '/thumbs',
'maintain_ratio' => FALSE,
'width' => 83,
'height' => 83
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$filename =time().preg_replace('/[^A-Za-z0-9\s.\s-]/', '_', $image_data['file_name']);
rename($image_data['full_path'],$image_data['file_path'].$filename);
rename($image_data['file_path'].'thumbs/'.$image_data['file_name'],$image_data['file_path'].'thumbs/'.$filename);
return $filename;
}
}
Add these lines for uploading images and files while sending email.
$config2 = array(
'upload_path'=>'../admin/images/blogimages',
'allowed_types' => 'pdf|doc|docx',
'max_size' => 20000,
);
$this->load->library('upload', $config2);
$this->upload->do_upload('attachment');
$upload_data = $this->upload->data();
$this->email->attach($upload_data['full_path']);

Registration email won't send in code igniter

I wrote an function to send an email to the user using the email they put on their registration form. However while testing it the email does not go to the email address entered. I checked to make sure that the function was working by having the email cc to an address, which worked. Below is the function.
View
<div class="col-lg-4 col-lg-offset-4">
<h2>Hello There</h2>
<h5>Please enter the required information below.</h5>
<?php
$fattr = array('class' => 'form-signin');
echo form_open('/main/register', $fattr); ?>
<div class="form-group">
<?php echo form_input(array('name'=>'firstname', 'id'=> 'firstname', 'placeholder'=>'First Name', 'class'=>'form-control', 'value' => set_value('firstname'))); ?>
<?php echo form_error('firstname');?>
</div>
<div class="form-group">
<?php echo form_input(array('name'=>'lastname', 'id'=> 'lastname', 'placeholder'=>'Last Name', 'class'=>'form-control', 'value'=> set_value('lastname'))); ?>
<?php echo form_error('lastname');?>
</div>
<div class="form-group">
<?php echo form_input(array('name'=>'email', 'id'=> 'email', 'placeholder'=>'Email', 'class'=>'form-control', 'value'=> set_value('email'))); ?>
<?php echo form_error('email');?>
</div>
<?php echo form_submit(array('value'=>'Sign up', 'class'=>'btn btn-lg btn-primary btn-block')); ?>
<?php echo form_close(); ?>
</div>
Controller
public function register()
{
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('register');
$this->load->view('footer');
}else{
if($this->user_model->isDuplicate($this->input->post('email'))){
$this->session->set_flashdata('flash_message', 'User email already exists');
redirect(site_url().'main/login');
}else{
$clean = $this->security->xss_clean($this->input->post(NULL, TRUE));
$id = $this->user_model->insertUser($clean);
$token = $this->user_model->insertToken($id);
$qstring = base64_encode($token);
$url = site_url() . 'main/complete/token/' . $qstring;
$link = '' . $url . '';
$message = '';
$message .= '<strong>You have signed up with our website</strong><br>';
$message .= '<strong>Please click:</strong> ' . $link;
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'host',
'smtp_port' => 'port',
'smtp_user' => 'username',
'smtp_pass' => 'password');
$this->load->library('email', $config);
$this->email->set_newline("/r/n");
$this->email->from('***#****.com', 'High Ball Run8');
$this->email->to(set_value("email"));
$this->email->cc('****#*****.com');
$this->email->subject('Highball Registration Email Activation');
$this->email->message($message);
if ($this->email->send()) {
redirect(site_url());
}else{
show_error($this->email->print_debugger());
}
exit;
Have you tried
var_dump(set_value("email")); exit();
To see what the output is? Chances are you are not getting the to address correctly, if the hardcoded CC works.
Please check following working code
function sendMail()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx#gmail.com', // change it to yours
'smtp_pass' => 'xxx', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = '';
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('xxx#gmail.com'); // change it to yours
$this->email->to('xxx#gmail.com');// change it to yours
$this->email->subject('Resume from JobsBuddy for your Job posting');
$this->email->message($message);
if($this->email->send())
{
echo 'Email sent.';
}
else
{
show_error($this->email->print_debugger());
}
}
Reference

Categories