For user identification,I need to send the url of localhost via mail in php codeigniter . . I also need to pass the token which I had generated for that user along with the url. My user need to be verified by clicking that link which must be identified by the corresponding token. And I have no idea about passing variables via url . . How could I proceed my code?
My code follows. .
<?php
class Site_model extends CI_Model
{
public function __construct()
{
parent:: __construct();
$this->load->database();
}
public function insert($token)
{
$data = array(
'name'=>$this->input->post('name'),
'email'=>$this->input->post('email'),
'phone'=>$this->input->post('phone'),
'date_of_birth'=>$this->input->post('dob'),
'user_type'=>$this->input->post('utype'),
'token'=>$token,
);
$this->db->insert('tbl_user',$data);
$email=$this->input->post('email');
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => '465',
'smtp_user' => 'someone#gmail.com',
'smtp_pass' => 'something',
'mailtype' => 'html',
'starttls' => true,
'newline' => "\r\n"
);
$this->load->library('email',$config);
$this->email->From("someone#gmail.com");
$this->email->to($email);
$this->email->subject('test');
$this->email->message("worked");
$this->email->send();
if($this->email->send()) {
echo '<script>alert("Email sent successfully")</script>';
} else {
$this->email->print_debugger();
}
}
}
?>
To pass data along with a URL you can use URI segments.
In your situation the easiest thing to do would probably to be to use GET parameters. To use these you would output the link as normal and then append a parameter to the end like:
http://yoursite.com/user/verify/**token**
This example would be for if you had a user controller with a function called verify. The method signature would be something like:
public function verify($token = NULL) { ... }
The last part of the URL will populate $token which you can then use in the verify function to check the user is valid and perform any actions you need to.
You could always re-route a URL to an alternative method through routes.php if you needed to as well.
Simply pass this link with the token inserted into the email so that when the user clicks on it then the verify method runs with the necessary parameters.
Related
I started a project using symfony 4 and the mailer doesn't work, however it should be easy.
before you ask, if i copy past the login and password from my code i'm able to log into my mail account, also i also tried with a netcourrier mail account, also the 2 way authentification is not active and i allowed less secure app to access the mail account.
Here's my conf:
in my .env:
MAILER_URL=gmail://*******#gmail.com:********#localhost
in my controller:
public function contact( \Swift_Mailer $mailer){
$message = (new \Swift_Message('Hello Email'))
->setFrom('*****#gmail.com')
->setTo('*******#gmail.com')
->setBody(
$this->renderView(
// templates/emails/registration.html.twig
'email/registration.html.twig',
array('url' => $url)
),
'text/html'
);
$mailer->send($message);
return $this->render(
'email/index.html.twig');}
and the error i get doing so is :
Connection could not be established with host smtp.gmail.com [ #0]
The problem is your connection SMTP with google, This is correct:
MAILER_URL=smtp://smtp.gmail.com:587?encryption=tls&username=userGmail&password=PassGmail
I have it defined as a service in App/Services, this is the code
<?php
namespace App\Services;
class Enviomail {
private $mailer;
public function __construct(\Swift_Mailer $mailer)
{
$this->mailer = $mailer;
}
public function sendEmail($to, $subject, $texto) {
$message = (new \Swift_Message($subject))
->setFrom('juanitourquiza#gmail.com')
->setTo($to)
->setBody(($texto),'text/html');
return $this->mailer->send($message);
}
}
And to use it I call it from the controller
use App\Services\Enviomail;
........
public function mailsolucion(Request $request, Enviomail $enviomail) {
if ($request->isMethod('POST')) {
$nombre=$request->get("nombre");
$email=$request->get("email");
$numero=$request->get("numero");
$empresa=$request->get("empresa");
$solucion=$request->get("solucion");
if (($nombre=="")||($email=="")||($numero=="")||($empresa=="")){
$this->addFlash(
'alert alert-danger',
'Toda la informaciĆ³n es obligatoria'
);
return $this->redirectToRoute('registro');
}
$emailreceptor="juanitourquiza#gmail.com";
$asunto="Soluciones gruporadical.com";
$texto=$this->renderView(
'emails/soluciones.html.twig',
array(
'nombre' => $nombre,
'email' => $email,
'numero' => $numero,
'empresa' => $empresa,
'solucion' => $solucion,
)
);
$enviomail->sendEmail($emailreceptor,$asunto, $texto);
$this->addFlash(
'alert alert-success',
'Pronto nos pondremos en contacto.'
);
return $this->redirectToRoute('registro');
}
return $this->render('AppBundle:App:contacto.html.twig');
}
Works perfect on Symfony 4.x
I think this is not an issue with the mailer, but with gmail. I copied your steps to try to connect through the smtp server of gmail, but got the same error. When using a different MAILER_URL (a different smtp-server) in the .env file, everything works like it should.
I am working in laravel 5 and having difficulties with my login.
After making login i want my page redirect by permissions.
If access = 1 go to backend and if access = 0 return welcome page but I continue to go to the default page of laravel "home" and I can not change. I do not understand why.
public function postLogin() {
$email = Request::input('email');
$password = Hash::make(Request::input('password'));
//if (Auth::attempt(['email' => $email, 'password' => $password, 'acesso' => 1])) {
if (Auth::attempt(['email' => $email, 'password' => $password])) {
return redirect()->intended('backend/dashboard.index')->with('message', 'Backend!');
} elseif (Auth::attempt(['email'=> $email, 'password' => $password])) {
return redirect()->intended('welcome')->with('message', 'Frontend!');
} else {
return view('auth/login')->with('message', 'error!');;
}
}
Routes:
// Authentication routes...
Route::get('auth/login', 'BackendControlador#getLogin');
Route::post('auth/login', 'BackendControlador#postLogin');
In Laravel 5, the AuthenticatesAndRegistersUsers trait has '/home' set as the default path to redirect to in the postLogin function. You can override this by setting the redirectPath:
In Http\Auth\AuthController.php, add this:
//Replace 'path here' with the path you want to redirect to. Example: '/welcome'
protected $redirectPath = 'path here';
This is based on the use of the Laravel-provided AuthController. From your post, I gather that you've created your own postLogin method in your controller "BackendControlador" so you may need to add the property in this controller instead.
I have a problem where I can't put in the variables into the Mail::send() function in laravel. Please see the following code:
$first_name = $request->input('first_name'),
$email = $request->input('email'),
//Create account
User::create([
'first_name' => $first_name,
'last_name' => $request->input('last_name'),
'email' => $email,
'password' => bcrypt($request->input('password')),
]);
//Send email to user
Mail::send('emails.test', ['fname' => $first_name], function($message)
{
$message->to($email)
->subject('Welcome!');
});
return redirect()
->route('home')
->with('info', 'Your account has been created and an authentication link has been sent to the email address that you provided. Please go to your email inbox and click on the link in order to complete the registration.');
For some reason the code breaks when it gets to the send email because I receive the error and the data is sent to the database. Why is the variable no longer accessible afterwards?
Any help would be greatly appreciated. Thank you
Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.
Source: http://php.net/manual/en/functions.anonymous.php
In other words, $email has to be inherited like this:
Mail::send('emails.test', ['fname' => $first_name], function($message) use ($email)
{
$message->to($email)
->subject('Welcome!');
});
Note: use ($email) in the first line.
I am using the Mailgun Yii extension(https://github.com/baibaratsky/php-mailgun) and am able to send a test email from inside the 'views/site/SiteController.php' file using the following code:
$message = Yii::app()->mailgun->newMessage();
$message->setFrom('sender#domain.com', 'Sender Name');
$message->addTo('recipient#domain.com', 'Recipient Name');
$message->setSubject('Mailgun API library test');
$message->setText('Test Email Content Text');
$message->send();
Now I am trying to extend the CEmailLogRoute class so that I can send any log emails using mailgun with no success. This is the class I wrote to extend it:
class CMailGunLogRoute extends CEmailLogRoute {
protected function sendEmail($email, $subject, $message) {
$message = Yii::app()->mailgun->newMessage();
$message->setFrom('sender#domain.com', 'Sender Name');
$message->addTo($email);
$message->setSubject($subject);
$message->setText($message);
$message->send();
}
}
And this is what I added to the 'config/main.php' file:
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CEmailLogRoute',
'levels'=>'info',
'emails'=>'recipient#domain.com',
'sentFrom'=>'sender#domain.com',
'subject'=>'Email Log File Message',
),
),
),
And this is the logging function I am declaring on the root index file:
function d2l($what, $where='fb.somewhere') {
$what = print_r($what,true);
Yii::log($what, 'info','application.'.$where);
}
And this is where I am calling that function from within the 'SiteController.php' file:
d2l('Test Log Message','site.index');
Unfortunately, none of this seems to get it to send the log email. Initially I tried to send the log email without using mailgun and that didn't work either, so perhaps the issue is with the code I wrote for the mail logging.
I figured out how to do this:
CMailGunLogRoute.php
// components/CMailGunLogRoute.php
class CMailGunLogRoute extends CEmailLogRoute {
protected function sendEmail($email, $subject, $message) {
$mail = Yii::app()->mailgun->newMessage();
$mail->setFrom($this->getSentFrom());
$mail->addTo($email);
$mail->setSubject($subject);
$mail->setText($message);
$mail->send();
}
}
SiteController.php
// controllers/SiteController.php
Yii::log('Test Log Message', 'info','application');
main.php
// config/main.php
'import'=>array(
'application.components.*',
),
'mailgun' => array(
'class' => 'application.extensions.php-mailgun.MailgunYii',
'domain' => 'mydomain.com',
'key' => 'API_KEY_NUM',
'tags' => array('yii'), // You may also specify some Mailgun parameters
'enableTracking' => true,
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CMailGunLogRoute',
'levels'=>'info',
'filter'=>'CLogFilter',
'emails'=>'recipient#domain.com',
'sentFrom'=>'sender#domain.com',
'subject'=>'Email Log File Message',
),
),
),
If you have any additional tips on how to improve this, please feel free to add them below.
I am very new to codeigniter and postmark, is there a way on how to integrate the two?
I created a controller called cron.php and put the postmark code inside the method inbound, here's my code:
public function inbound()
{
try {
require_once '../lib/Postmark/Autoloader.php';
\Postmark\Autoloader::register();
// this file should be the target of the callback you set in your postmark account
$inbound = new \Postmark\Inbound(file_get_contents('php://input'));
$this->data['client'] = $this->client_model->get_where($m_where);
if(!empty($this->data['client']))
{
$m_insert = array('cme_username' => $inbound->FromName(),
'cme_title' => $inbound->Subject(),
'cme_message' => $inbound->TextBody(),
'cme_client' => $this->data['client']['ccl_id'],
'cme_from' => 'client',
'cme_through' => 'email',
'cme_date_sent' => date('Y-m-d H:i:s'),
'cme_admin' => 0);
$this->message_model->insert($m_insert);
}
}
catch (Exception $e) {
echo $e->getMessage();
}}
I'm also wondering if I am doing it correctly? Many thanks!
You can simply pull one of the example classes from the PostMarkApp.com website (I used this one and renamed it simply postmark) in the application/libraries/ directory and use it like any other CI Library. Make sure you have the proper globals defined (I defined them in application/config/config.php).
application/config/config.php
define('POSTMARKAPP_API_KEY', 'YOUR-POSTMARKAPP-API-KEY');
define('POSTMARKAPP_MAIL_FROM_ADDRESS', 'you#yourdomain.com');
application/controllers/somecontroller.php
class somecontroller extends CI_Controller {
function someControllerMethod(){
$this->load->library('postmark');
$this->postmark->addTo('someone#somewhere.com', 'John Doe')
->subject('My email subject')
->messagePlain('Here is a new message to John!')
->tag('some-tag')
->send();
}
}
Hope that helps!