Unable to send email attachment stored in database - Codeigniter - codeigniter

I have a form where the user attaches a file. The file is uploaded to a folder then the full_path link is stored in a database. The link is stored in an array format along with other items.
Here is how it is stored in the database.
[{"product_id":"20","product_name":"C4(Big A4) Envelopes","quantity":"1000","image":"\"\\\/home\\\/printlin\\\/public_html\\\/uploads\\\/birthday24.jpg\""}]
Now when i want to send my email, I decode the data stored in the database and place the file_path in the attach() function.
foreach ($data['shopping_cart'] as $cart_data) {
$items = json_decode($cart_data['items'],true);
foreach ($items as $documents){
$this->email->attach(stripcslashes($documents['image']));
}
}
The email gets sent, however it does not come with any attachments.
($documents['image']) appears as:
"\/home\/printlin\/public_html\/uploads\/birthday28.jpg"
And stripcslashes($documents['image']) appears as:
"/home/printlin/public_html/uploads/birthday28.jpg"
What could be the problem with my link format??

Related

Send automatical email after save to database

I am new here.
I have a project in Laravel. I have one textarea and data from it is save in datavase. It works good. Now I would like to send automatical email to one specific email address with this data. It must be sent only one time with save to database.
I have no problem with sending email to customer with data but now I need to send email with data from this textarea to one specific email. It is a textarea what we have to buy for customer. It must be sent to our cooperation company.
Is it possible?
Ofcourse this is possible!
You should take a look at the following resources :
Observers
https://laravel.com/docs/6.0/eloquent#observers
Notifications
https://laravel.com/docs/6.0/notifications
-> specifically : https://laravel.com/docs/6.0/notifications#mail-notifications
yes, you can just trigger your function after saving: for example, after saving in controller.
public function store(Request $request){
$var = new Property; //your model
$var->title=$request->title; // the input that being save to database.
$var ->save();
// Send email to that input
Mail::send('email',['email'=>$request->title],function ($mail) use($request){
$mail->from('info#sth.com');
$mail->to($request->title);
});
return redirect()->back()->with('message','Email Successfully Sent!');
}

How to include an association with a user

I need to return a json object to my view. I see my subscriptions in tinker:
\App\User::find(1)->with('subscriptions')->where('id', 1)->get();
In my view, I get my user data but the subscriptions array is emply (length: 0)
Issue one, why I could not just \App\User::find(1)->with('subscriptions')->get() This returns all users in my database.
How can I return, as json, the current user with subscriptions? The long way is to make two api calls.
Im using Cashier for the subscriptions
Try to check the query log by running
\DB::enableQueryLog();
$data = \App\User::with('subscriptions')->find(1);
$query = \DB::getQueryLog();
dd($query);
It will show you what queries running behind the seens.
then for the json format try to decode into json and then pass to view.
$json_format = json_encode($data);
return view('user.index')->withJsonFormat($json_format);
Don't know why you want to pass the data with json format. but if you want to pass this into javascript, jquery or angular please create saperate API and call with AJAX.
The correct query is:
App\User::with('subscriptions')->find(1)
It will return User object with this user's subscriptions.

How to get image url from tweets using the Twitter API

I'm using this code:
require_once ("twitteroauth.php");
define('CONSUMER_KEY', 'XXX');
define('CONSUMER_SECRET', 'XXX');
define('ACCESS_TOKEN', 'XXX');
define('ACCESS_TOKEN_SECRET', 'XXX');
$toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$query = array(
"q" => "#Misiones",
"result_type" => "recent",
"include_entities" => "true"
);
results = $toa->get('search/tweets', $query);
foreach ($results->statuses as $result) {
$user = $result->user->screen_name;
$text = $result->text;
To get tweets whit the hashtag #Misiones(the name of the place where i'm live). Works fine but i'm trying to get the image url (if the tweet have some).
I tryed with $result->media and $result->media->media_url and other convinations without succcess.
Tweet Entities are what you are looking for to access the pictures. Entities provide structured data from Tweets including expanded URLs and media URLs. They are located under the entities attribute in all Tweet objects from both Twitter REST and Streaming APIs.
As a result, to answer your question, if a Tweet contains one picture, its URL will be located here:
$media_url = $result->entities->media[0]->media_url;
Below is a PHP snippet you can add to your existing foreach loop, it is a bit more elaborate to handle whether or not the Tweet contains media URLs:
if (isset($result->entities->media)) {
foreach ($result->entities->media as $media) {
$media_url = $media->media_url; // Or $media->media_url_https for the SSL version.
}
}
To get the media_url back to twitter search pass tweet_mode=extended. Works across multiple endpoints (eg. /statuses/show, /statuses/user_timeline ..)
Example: https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=aneon&tweet_mode=extended
More here: https://developer.twitter.com/en/docs/tweets/tweet-updates.html
To get the image url of a Tweet with the new v2 api you must append expansions=attachments.media_keys&media.fields=url at your url.
For example to open a stream that returns tweets with their images(if any) create a get request like this
https://api.twitter.com/2/tweets/search/stream?tweet.fields=created_at&expansions=attachments.media_keys&media.fields=url
To get an image url of a tweet, you need a tweet object and a media object. The media object can be obtained with a tweet id.
https://developer.twitter.com/en/docs/twitter-api/data-dictionary/object-model/media

virtuemart 2 - how to send invoice as email attachment on confirm order

I want to send invoice as email attachment on order confirmation ,how to go about sending the invoice.
searched a lot ,not relevant data found.
The VM Order Confirmation Email work flow is as follows,
Check the function notifyCustomer() inside orders.php in administrator/components/com_virtuemart/models/
There is a section like shopFunctionsF::renderMail() It calls a function from shopefunctionsf helper file in /components/com_virtuemart/helpers/shopfunctionsf.php
renderMail() calls sendVmMail() in the same file there you can find option for attaching media or anything you want .
if (isset($view->mediaToSend)) {
foreach ((array)$view->mediaToSend as $media) {
//Todo test and such things.
$mailer->addAttachment($media);
}
}
Hope its helps..

Codeigniter form validation failing when it should succeed

I'm building an admin utility for adding a bulk of images to an app I'm working on. I also need to to log certain properties that are associated with the images and then store it all into the database.
So basically the script looks into a folder, compares the contents of the folder to records in the database. All of the info must be entered in order for the database record to be complete, hence the form validation.
The validation is working, when there are no values entered it prompts the entry of the missing fields. However it happens even when the fields ARE filled.
I'm doing something a bit funny which may be the reason.
Because I'm adding a bulk of images I'm creating the data within a for loop and adding the validation rules within the same for loop.
Here is the results:
http://s75151.gridserver.com/CI_staging/index.php/admin_panel/bulk_emo_update
Right now I have default test values in the form while testing validation. The submit button is way at the bottom. I'm printing POST variable for testing purposes.
Here is the code:
function bulk_emo_update() {
$img_folder_location = 'img/moodtracker/emos/';//set an image path
$emo_files = $this->mood_model->get_emo_images('*.{png,jpg,jpeg,gif}', $img_folder_location); //grab files from folder
$emo_records = $this->mood_model->get_all_emos(); //grab records from db
$i=1; //sets a counter to be referenced in the form
$temp_emo_info = array(); //temp vairable for holding emo data that will be sent to the form
//loop through all the files in the designated folder
foreach($emo_files as $file) {
$file_path = $img_folder_location.$file;//builds the path out of the flder location and the file name
//loops through all the database reocrds for the pupose of checking to see if the image file is preasent in the record
foreach($emo_records as $record) {
//compairs file paths, if they are the
if($record->picture_url != $file_path) {
//FORM VALIDATION STUFF:
$rules['segment_radio['.$i.']'] = "required";
$rules['emo_name_text_feild['.$i.']'] = "required";
//populating the temp array which will be used to construct the form
$temp_emo_info[$i]['path'] = $file_path;
$temp_emo_info[$i]['name'] = $file;
}
}
$i++;
}
//sets the reference to validation rules
$this->validation->set_rules($rules);
//checks to see if the form has all it's required fields
if ($this->validation->run() == FALSE) { //if validation fails:
print_r($_POST);
//prepairs the data array to pass into the view to build the form
$data['title'] = 'Bulk Emo Update';
$data['intro_text'] = 'fill out all fields below. hit submit when finished';
$data['emos_info'] = $temp_emo_info;
$this->load->view('admin_bulk_emo_update_view',$data);
} else { // if it succeeds:
//printing for test purposes
print_r($_POST);
$this->load->view('form_result');
}
}
I'm new to codeigniter and php in general so if anything looks outrageously weird please tell me, don't worry about my feelings I've got thick skin.
if ($this->validation->run() == FALSE)
if you are calling the run() method of the validation class every time the script is run, will it ever return TRUE and run the else? Maybe a different return?
I'm a little cornfused by what's going on. Generally, if I'm having a problem like this, I will figure out a way to force the result I'm looking for. e.g. in your code, I'd force that else to run... once I get it to run, break down what happened to make it run. Rudimentary, but it has served me well.
You use array of rules in
$this->form_validation->set_rules()
wrong.
If you want to pass the rules in array you must stick to the key names like described here http://codeigniter.com/user_guide/libraries/form_validation.html#validationrulesasarray
So instead of
$rules['input_name'] = "required"
try this:
array(
'field' => 'input_name',
'label' => 'Name that you output in error message',
'rules' => 'required'
)

Resources