codeigniter - send email - codeigniter

I have a CRUD "clientes" and I added a button "email", when I click on it I would like to send an email with a username and a password
here is my button
$crud->add_action('Email', '', 'pendientes/pendientes_v/enviarmail', 'ui-icon-plus');
and then the function in my controller
function copyrows($id)
{
$this->load->model('cliente_model', 'client');
$clientInfo = $this->client->getInfo($client_id);
$this->email->from('myemail#gmail.com', 'Demo');
$this->email->to($clientInfo['email']);
$this->email->subject('Email Test');
$this->email->message('Hola tu usuario es ' . $clientInfo['email'] . ' y tu contraseƱa es ' . $clientInfo['clave']);
$this->email->send();
echo $this->email->print_debugger();
}
my model cliente_model.php
<?php
class Cliente_Model extends CI_Model{
public function getInfo($client_id){
return array('id' => $cliente_id,'email' => $email,'clave' => $clave);
}
}
here the errors
Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
From: "Demo"
Return-Path:
Reply-To: "myemail#gmail.com"
X-Sender: myemail#gmail.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <51478bcb599c0#gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
=?utf-8?Q?Email_Test?=
Hola tu usuario es y tu contraseƱa es#gmail.com>#gmail.com>#gmail.com>
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /home/oscar/public_html/crud/system/core/Exceptions.php:185)
Filename: core/Common.php
Line Number: 438
I know I am receiving a null value but I can't find the solution, hope someone can help me.
thanks in advance!

i had the same problem and it's a hell, i fixed only using php native:
mail();
instead of using CI mail library
maybe it fixes your problem too

Related

How to debug "Failed to send AUTH LOGIN command" when sending to GMail in CodeIgniter?

After I use print debugger to check whether it's sent or not, and here's what I got in return:
bool(false) 220 smtp.gmail.com ESMTP t8sm1776565pgr.21 - gsmtp
hello: 250-smtp.gmail.com at your service, [45.116.123.5]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
Failed to send AUTH LOGIN command. Error: 530 5.7.0 Must issue a STARTTLS command first. t8sm1776565pgr.21 - gsmtp
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
Date: Fri, 2 Feb 2018 13:02:25 +0530
To: demo#example.com
From: "MyWebsite" <demo#example.com>
Return-Path: <demo#example.com>
Subject: =?UTF-8?Q?How=20to=20send=20email=20vi?==?UTF-8?Q?a=20SMTP=20?= =?UTF-8?Q?server=20in=20CodeIgniter?=
Reply-To: <demo#example.com>
User-Agent: CodeIgniter
X-Sender: demo#example.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <5a741409bc741#gmail.com>
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="B_ALT_5a741409bc741"
This is a multi-part message in MIME format.
Your email application may not support this format.
--B_ALT_5a741409bc741
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Sending email via SMTP serverThis email has sent via SMTP server from
CodeIgniter application.
--B_ALT_5a741409bc741
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
=3Ch1=3ESending email via SMTP server=3C/h1=3E=3Cp=3EThis email has sent vi=
a SMTP server from CodeIgniter application.=3C/p=3E
--B_ALT_5a741409bc741--
When I changed the smtp_port to 465 and I got in response like
bool(false)
hello: F
The following SMTP error was encountered: F
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
Date: Fri, 2 Feb 2018 13:12:43 +0530
To: demo#example.com
From: "MyWebsite" <demo#example.com>
Return-Path: <demo#example.com>
Subject: =?UTF-8?Q?How=20to=20send=20email=20vi?==?UTF-8?Q?a=20SMTP=20?= =?UTF-8?Q?server=20in=20CodeIgniter?=
Reply-To: <demo#example.com>
User-Agent: CodeIgniter
X-Sender: demo#example.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <5a7416736d6ea#gmail.com>
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="B_ALT_5a7416736d6ea"
This is a multi-part message in MIME format.
Your email application may not support this format.
--B_ALT_5a7416736d6ea
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Sending email via SMTP serverThis email has sent via SMTP server from
CodeIgniter application.
--B_ALT_5a7416736d6ea
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
=3Ch1=3ESending email via SMTP server=3C/h1=3E=3Cp=3EThis email has sent vi=
a SMTP server from CodeIgniter application.=3C/p=3E
--B_ALT_5a7416736d6ea--
I have hidden my email id in this question for security purposes.
I find so many code examples that they wanted to change local php.ini and sendmail.ini files to send emails but I think that's not the requirement to send mail using smtp.
I also use the PHPmailer lib and it works for smtp mail, but why CodeIgniter email lib for SMTP is not working? Which PHP version is supported for this CodeIgniter SMTP settings?
Try this:-
$ci = get_instance();
$ci->load->library('email');
$config['protocol'] = "smtp";
$config['smtp_host'] = "mail.google.com";
$config['smtp_port'] = "26";
$config['smtp_user'] = "********#gmail.com";
$config['smtp_pass'] = "************";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$ci->email->initialize($config);
$ci->email->from('*********#gmail.com');
$email_body = "Sending Email from server.";
$this->email->to($userEmail']);
$this->email->subject('Email Subject');
$this->email->message($email_body);
$email = $this->email->send();

want to send an email from localhost in Codeigniter and i'm using lampp

i want to send an email from my local host using code igniter. I'm using lamp here. My code shows below error
Exit status code: 127
Unable to open a socket to `Sendmail`. Please check settings.
Unable to send email using `PHP` `Sendmail`. Your server might not be configured to send mail using this method.
Date: Fri, 12 May 2017 08:01:46 +0200
From: "`jatin`"
Return-Path: <xxx>
To: xxx
Subject: =?UTF-8?Q?Hi..=20?=
Reply-To: <xxx>
User-Agent: `CodeIgniter`
X-Sender: xxx
X-Mailer: `CodeIgniter`
X-Priority: 3 (Normal)
Message-ID: <xxx>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Use PHP mailer for sending mail.
using this you can send mail easily without any warning and error
Download PHP mailer from here: PHPMailer
require_once(APPPATH.'third_party/PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "test#gmail.com"; // your email address
$mail->Password = "test"; // your gmail password
$mail->SetFrom('test#gmail.com', 'Test'); //sender mail address
$mail->isHTML(true);
$mail->Subject = "Subject";
$mail->Body = 'hello there';
$destino = 'test#gmail.com'; // receiver email address
$mail->AddAddress($destino, "Receiver");
if(!$mail->Send()) {
return false;
} else {
return true;
}
You have to set access for less trusted apps in your Gmail account also:
Login to your gmail account
Click on MyAccount from Google Apps
Click on 'Signing in to Google' from left panel
And Turn on 'Allow less secure apps' from bottom of page
now try to send mail.it's works 100%.

Laravel 5 API using json and Guzzle

I'm quite new to Laravel 5 but I'm working on project involing 2 servers (S1 and S2). Each one is running a Laravel 5 REST WebService (API) WS1 and WS2.
The workflow is the following :
WS1 : just get a query like http://s1.ip/api/object/1 where 1 is the id of the object. It just "reroute" the query to WS2.
WS2 : get the same kind of query http://s2.ip/api/object/1 using a personnal access token from Laravel/Passport.
WS2 : Ask the local mysql DB to know if the object with id 1 is 'valid' or not.
WS2 : Create a response to the WS1 Query using json. Something like :
{"id": "2", "valid": true}
WS1 : get the response from WS2 and from it create its own response to the initial GET query.
I test my REST API using Postman.
WS2 is work well when using Postman.
But when I try to query WS1, I never get the json from the response that is send from WS2.
Hope I clear.
Here are some of my source code:
routes\api.php
Route::get('/object/{obj_id?}', 'ObjectController#check')->where('obj_id', '[0-9]+');
app\Http\Controllers\ObjectController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ObjectController extends Controller
{
public function check($obj_id)
{
$token = 'my personnal access tokens from WS2 Laravel/Passport'
$client = new Client();
$body = $client->request('GET','http://S2.ip/api/object/' . $object_id, [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'bearer ' . $token,
])->getBody();
$contents = (string) $body;
$data = json_decode($contents);
dd($data); // to see what's inside $data
The dd($data) output the following : null
I have try many things but I never manager to get the json.
What am I doing wrong. I would really appreciate some help on this.
Thanks
Edit:
Here is the S2 answer I got from PostMan or Rested:
S2 returns the following when using Postman:
Response - http://s2.ip/api/object/1
200 OK
Headers
Date: Thu, 12 Jan 2017 08:38:31 GMT
Vary: Authorization
Server: Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.21
X-Powered-By: PHP/5.6.21
X-RateLimit-Remaining: 59
Content-Type: application/json
Cache-Control: no-cache
X-RateLimit-Limit: 60
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Content-Length: 46
Response body
{
"id": "1",
"authorized": false
}

Why don't I receive an email using codeigniter email library when using local server?

I am using codeigniter's email library on mac os x apache 2 server. The weird thing I am running into is that when I use it on live server which is on hostgator.com the email goes through but when I run the same code on local apache2 server, I don't get any emails(even in spam box). What can be the problem? Here is the code
$this->load->library('email');
$this->email->from('your#example.com', 'Your Name');
$this->email->to('myemailhere#gmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
if($this->email->send())
{
var_export($this->email->print_debugger());
}
On both local and live server, this is var_dump data
'Your message has been successfully sent using the following protocol: mail
From: "Your Name"
Return-Path:
Reply-To: "your#example.com"
X-Sender: your#example.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <5122a6ed242b8#example.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 can just try this.
Specify path for send mail in php.ini
sendmail_path = "path/to/php path/to/sendmail.php"
The second step - you can try to use this script
define('DIR','path/to/sendmail_dir');
$stream = '';
$fp = fopen('php://stdin','r');
while ($t = fread($fp,2048)) {
if ($t === chr(0)) {
break;
}
$stream .= $t;
}
fclose($fp);
$fp = fopen(mkname(),'w');
fwrite($fp,$stream);
fclose($fp);
function mkname($i = 0) {
$fn = DIR.date('Y-m-d_H-i-s_').$i.'.eml';
if (file_exists($fn)) {
return mkname(++$i);
} else {
return $fn;
}
}

Sending E-mail from Aol account SMTP via CodeIgniter

application/config/email.php
<?php
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'tls://smtp.aol.com';
$config['smtp_user'] = 'chris.muench';
$config['smtp_pass'] = 'PASSWORD';
$config['smtp_port'] = '587';
$config['charset']='utf-8';
$config['newline']="\r\n";
$config['crlf'] = "\r\n";
?>
E-Mail Debug Statement:
The following SMTP error was encountered: 0
Unable to send data: AUTH LOGIN
Failed to send AUTH LOGIN command. Error:
Unable to send data: MAIL FROM:
from:
The following SMTP error was encountered:
Unable to send data: RCPT TO:
to:
The following SMTP error was encountered:
Unable to send data: DATA
data:
The following SMTP error was encountered:
Unable to send data: User-Agent: CodeIgniter Date: Fri, 9 Nov 2012 20:39:52 -0500 From: "PHP Point Of Sale, Inc" Return-Path: To: chris.muench#aol.com Subject: =?utf-8?Q?Reset_password?= Reply-To: "admin#phppointofsale.com" X-Sender: admin#phppointofsale.com X-Mailer: CodeIgniter X-Priority: 3 (Normal) Message-ID: <509db0685db38#phppointofsale.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="B_ALT_509db0685db40" This is a multi-part message in MIME format. Your email application may not support this format. --B_ALT_509db0685db40 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit You have requested to reset your password, please click the link below in order to complete the password reset process Reset password --B_ALT_509db0685db40 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable You have requested to reset your password, please click the link below in o= rder to complete the password reset process
Note: I am able to send e-mail from a gmail account. What is wrong?

Resources