How to set up email piping with codeigniter? - codeigniter

I've redirected incoming emails via cPanel to a php script which is below. At the moment it just sends a email reply so I can test that I have the correct info, once I've confirmed that I'll start adding to a database.
What I need to know is how would I use codeigniter to do this?
Also where would I redirect the emails to in cPanel, for example at the moment they go to - public_html/email/parse.php, how would I redirect them to a controller in codeigniter?
I've tried putting this code into a controller but the email bounces back.
#!/usr/bin/php -q
<?php
// get contents of email and add to $email
$email = file_get_contents('php://stdin');
// regular expression to get parts of email that I need
preg_match_all("/(To):\s(.*)\n/i", $email, $to);
preg_match_all("/(Subject):\s(.*)\n/i", $email, $subject);
preg_match_all("/(From):\s(.*)\n/i", $email, $from);
// assign parts of email to variables
$sender = $to[2][0];
$sender = trim($sender, ' " ');
$sender_id = explode('#', $sender);
$sender_id = $sender_id[0];
$subject = $subject[2][0];
$from = $from[2][0];
// make a reply email to test the above works
$reply = "Here's your information!\n\nSender: $sender\nSender ID: $sender_id\nFrom: $from\nSubject: $subject";
// send email to test
mail($from, 'From my email pipe!', $reply, 'From:noreply#mydomain.me');
?>

Related

I need to do a function to resend OTP if user has not received it to update phone number functionality in laravel

Maybe I'm losing in a glass of water but I'm not understanding how implements this function, I use OTP code to verify phone when user compile the registration and when he try to update number. I'm using vonage, I put below my update method:
public function updatePhoneNumber(Request $request)
{
$basic = new \Vonage\Client\Credentials\Basic("44bc4bb2", "fYVcLeo0lMhmtjm1");
$client = new \Vonage\Client($basic);
$request->validate([
'telefono' => 'required|unique:users,telefono'
]);
$telefono = $request->input('telefono');
$otp = VerificationCode::create([
'otp' => rand(10000, 99999),
'expire_at' => Carbon::now()->addMinutes(10)
]);
$response = $client->sms()->send(
new SMS($telefono, 'Help4You', 'Il tuo codice di verifica è:'. "\n" . $otp->otp)
);
$message = $response->current();
if ($message->getStatus() == 0) {
echo "The message was sent successfully\n";
} else {
echo "The message failed with status: " . $message->getStatus() . "\n";
}
User::where('id', '=', Auth::user()->id)->update([
'telefono'=>Hash::make($request['telefono']),
]);
}
how can I resend the otp if user has not received it?
You simply need to have 2 separate functions. The logic for creating an OTP and sending the OTP is in a different function. Myself, I create a service for it and store it in a App\Services folder since it is a functionality I commonly use.
So, back to your question, separate the logic for creating an OTP and sending the otp, in your case, the following code should not be part of your function above
$telefono = $request->input('telefono');
$otp = VerificationCode::create([
'otp' => rand(10000, 99999),
'expire_at' => Carbon::now()->addMinutes(10)
]);
$response = $client->sms()->send(
new SMS($telefono, 'Help4You', 'Il tuo codice di verifica è:'. "\n" . $otp-
>otp)
);
}
If you are implementing this is a separate class, remember to return the outcome of the sending of the OTP, it can either be success or error. Also, the only value you need to pass onto the function will be the recipient's cellphone number.
So, first time you send the OTP, you simply call the function or instance of the class, depending how you implemented.
Now, if the OTP was not delivered or a user wants to request another OTP, you call the function again.
You can add additional validation or a field on you table to check how many retries a user does for an OTP since the sms will cost your client money.
Once you separate your logic, you have now made your code to be reusable and is you need to use the same API to send a different SMS, you can do so.
Hope this helps.

How to add more parameter on controller laravel mail

I want to send email using Laravel and success with this code
Mail::to($email)->send(new SendMail($name))
Now I want to send more parameter like subject with this code, but didn't work.
No error message but email not being sent.
Mail::send(new SendMail($name function($message) use ($email){
$message->to($email)->subject('Verify your Account');
}));
Any suggestions? Thanks in advance!
You can use something like that
$template = 'view.email';
$paramets = ['data'=>$data];//Your to value to print in email
$email = 'to#email.com'// to email
$cc = 'cc#email.com' // or null
$toName = 'John Doe' // or null
$subject = 'Verify your Account';
Mail::send($template, $parameters, function($message) use($email,$cc,$toName,$subject) {
$message->from(env('sender'), env('nameSender'));
if($cc) $message->cc($cc);
$message->to($email, $toName)
->subject($subject);
});
I am using a generic method. So i can use this method to all emails that i need to send

Validation of an email address in Laravel

I have created an edit form in my Laravel application form which accepts an email address. I have used Laravel validation on the server-side. It is validating the email address correctly when I pass a clearly invalid value like 'xxxxxx'.
But the problem is when I send an email address with only a top level domain and no dot like 'xxxxxx#yyyyyy', it accepts it as a valid email address.
How can I validate the email address to ensure it's using a proper domain?
With Laravel 7: you can use
'email' => 'email:rfc,dns'
You can simply do:
$validator = Validator::make($request->all(), [
'Email'=>'required|email'
]);
Try this:
$this->validate($request, [
'email' => 'required|regex:/(.+)#(.+)\.(.+)/i',
]);
It is not a Laravel issue. That is technically a valid email address.
Notice that if you tell the browser to validate the email address, it will also pass.
But you can use package EmailValidator for validating email addresses.
At first, also check these: https://laravel.com/docs/6.x/validation#rule-email
Or,
use the checkdnsrr function.
<?php
$email = 'email#gmail.com';
list($username, $domain) = explode('#', $email);
if (checkdnsrr($domain, 'MX')) {
echo "verified";
}
else {
echo "failed";
}
Laravel email validation and unique email insert database use for code:
'email_address' => 'required|email|unique:customers,email_address'

Sending email notification to existing user Laravel 5.8

I am trying to send email to all existing admins once a user sends a request. I am using a Notifiable class to send emails but I am getting an error of :-
Call to a member function notify() on string
The code for me to notify all admins:-
$all_admins = User::whereHas('roles', function ($query) {
$query->where('admin', '1');
})->paginate(100);
foreach($all_admins as $admin){
if ($type == 1){
$subject = $username.' made a lawyer request.';
$message = "User: ". $user;
$message2 = "Please handle the request in the admin portal under the request tab.";
}
$user = $admin->email;
//$admin->email has a string of an email
$user->notify(new EmailAdmin($subject, $message, $message2)); <--
***The above code is the code that is marked when laravel is returning the error to me.
}
You wrote it yourself. $admin->email is a string. The error says you're calling a function on a string. You're doing it wrong.
The documentation says you're supposed to call notify() on a User model that has the trait.
(answered in the comments by #IGP)

omnipay paypal express not returning address

I am using the omnipay setup here: https://github.com/adrianmacneil/omnipay to process a paypal express checkout.
The process works fine in that the user is redirected to paypal -> they login and choose to pay -> they get returned to my site at which point I capture the payment.
The problem I've got is that I need to capture the address they have entered into paypal as their billing / shipping address.
To send the user across to paypal I have the following:
$gateway = GatewayFactory::create('PayPal_Express');
$gateway->setUsername('XX-USERNAME_XX');
$gateway->setPassword('XX_PASSWORDXX');
$gateway->setSignature('XX_SIG_XX');
$gateway->setTestMode(true);
$response = $gateway->purchase(
array(
'cancelUrl'=>'http://www.XXX.co.uk/',
'returnUrl'=>'http://www.XXX.co.uk/paypalexpress_confirm',
'amount' => $totalamount,
'currency' => 'GBP'
)
)->send();
$response->redirect();
When the user is returned I have the following:
$gateway = GatewayFactory::create('PayPal_Express');
$gateway->setUsername('XX-USERNAME_XX');
$gateway->setPassword('XX_PASSWORDXX');
$gateway->setSignature('XX_SIG_XX');
$gateway->setTestMode(true);
$response = $gateway->completePurchase(
array(
'cancelUrl'=>'http://www.XXX.co.uk/',
'returnUrl'=>'http://www.XXX.co.uk/paypalexpress_confirm',
'amount' => $totalamount,
'currency' => 'GBP'
)
)->send();
echo $responsemsg=$response->getMessage();
echo '<br><br><br>';
$data = $response->getData();
print_r($data);
Nothing in the response message or the raw data contains the customer address.
Has anyone got this working as i'm struggling and it's the final step to complete the transaction.
For those who are trying to get this work it's as Adrian said.
You first do the normal omnipay paypal payment and then afterwards:
get the token you were given
preform a second call to paypal using the call getexpresscheckoutdetails method
this returns all the info you need
API info here: https://cms.paypal.com/uk/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_r_GetExpressCheckoutDetails
The php script paypal provide to do it all for you:
https://cms.paypal.com/cms_content/ES/es_ES/files/developer/nvp_ECGetExpressCheckout_php.txt
omnipay\paypal\ProGateway.php add new function
public function fetchExpressCheckoutDetail(array $parameters = array())
{
return $this->createRequest('\Omnipay\PayPal\Message\FetchExpressCheckoutRequest', $parameters);
}
omnipay\paypal\src\Message add new file FetchExpressCheckoutRequest.php
namespace Omnipay\PayPal\Message;
class FetchExpressCheckoutRequest extends AbstractRequest
{
public function getData()
{
$data = $this->getBaseData('GetExpressCheckoutDetails');
$this->validate('transactionReference');
$data['TOKEN'] = $this->getTransactionReference();
$url = $this->getEndpoint()."?USER={$data['USER']}&PWD={$data['PWD']}&SIGNATURE={$data['SIGNATURE']}&METHOD=GetExpressCheckoutDetails&VERSION={$data['VERSION']}&TOKEN={$data['TOKEN']}";
parse_str (file_get_contents( $url ),$output);
$data = array_merge($data,$output);
return $data;
}
}
Usage:
$response = $gateway->completePurchase($params)->send();
$data = $response->getData();
$gateway->fetchExpressCheckoutDetail(array('transactionReference'=>$data['TOKEN']))->getData();
It will be not the best. But it works. :)
If it's not returned by the $response->getData() method, you might need to call PayPal's GetExpressCheckoutDetails API method to get the extra details about the transaction.
Omnipay doesn't support this out of the box, so you will probably need to copy and customize one of the existing requests to make a separate API call after you have confirmed payment.

Resources