How to pass variables from Job Queue class to Mail view template - php

I am trying to pass variables from Job Queue class to Mail view template like the following example.
$snippet = Constant_model::getDataOneColumn("snippets","id",$this->details['snippet_id']);
$snippet_details = [
'snippet_name'=> $snippet[0]->snippets_name,
'snippet_image'=> $snippet[0]->snippet_image,
'snippet_url'=> $snippet[0]->url_slug
];
$subscribers = Constant_model::getDataOneColumn("user_followers","receiver_id",$this->details['published_by']);
foreach($subscribers as $subscriber){
$user =User::find($subscriber->sender_id);
Mail::to($user->email)->send(new Notifynewsnippet($snippet_details));
}
Mail template
{{$snippet_details['snippet_name']}}
But I am getting the error. please help

You can pass on variablea via view. It is available in the class Mailable:
$this->view('mails.hello', ['name' => $name])

Related

Telegram Laravel Bot Reply

I'm pretty new to laravel and Telegram API and I want to develop a bot that can reply your input messages.
For now I can send messages when I refresh a route, but I would like it to send a message and reply once your start a conversation with him.
I'm using this sdk: https://telegram-bot-sdk.readme.io/docs
and so far I managed to call the send message method:
class TelegramController extends Controller {
public function index(){
$chatID = 'xxxxxxx';
Telegram::sendMessage([
'chat_id' => $chatID,
'text' => '[▼皿▼]'
]);;
}
}
How can I add some sort of listener that can reply to user input?
Thanks in advance.
At first, you should add the route in routes/web.php, like this:
Route::post('/bot_webhook', 'TelegramController#index')->middleware('api');
And you should get update improve TelegramController like this:
class TelegramController extends Controller {
public function index(){
$update = json_decode(file_get_contents('php://input'));
# I'm not sure what library do you use, but it should work if it was like this
Telegram::sendMessage([
'chat_id' => $update->message->chat->id,
'text' => '[▼皿▼]'
]);;
}
}

Add custom property to Mail in Laravel

I'm trying to get Postmark's new Message Streams working with Laravel.
Is there a way of adding the property + value '"MessageStream": "notifications" to the JSON body of an email sent using Laravel mail? I'm guessing I will need to extend the Mailable class in some way to do this.
Ideally, i'd like to be able to do something like the following in my Mailable class:
DiscountMailable.php
public function build()
{
return $this->from('hello#example.com')
->markdown('emails.coupons.created')
->subject('🎟 Your Discount')
->with([
'coupon' => $this->coupon,
])
->messageStream(
'notifications',
);
}
Just to make sure this question has an answer, this is what was necessary to make it work (and it worked for my issue in Laravel 7 today as well, where I wanted to use different Postmark Streams for different mail notification types).
public function build()
{
return $this->from('hello#example.com')
->markdown('emails.coupons.created')
->subject('🎟 Your Discount')
->with([
'coupon' => $this->coupon,
])
->withSwiftMessage(function ($message) {
$message->getHeaders()
->addTextHeader('X-PM-Message-Stream', 'notifications')
});
}

Laravel: Use Email and Name in Mail::to

I have a contact form where someone provides his name and email. I want to send him an email now with Laravel.
I found in the docs
To send a message, use the to method on the Mail facade. The to method
accepts an email address, a user instance, or a collection of users.
and in fact
\Mail::to('example#gmail.com')->send(new \App\Mail\Hello);
works. But is it also possible to provide the name for the email receipt?
I wanted to look that up in the Laravel API for the Mail Facade but to my surprise the facade has no to function?
So how can I find out what the to function really does and if I can pass a name parameter as well?
In laravel 5.6, answer to your question is: use associative array for every recpient with 'email' and 'name' keys, should work with $to, $cc, $bcc
$to = [
[
'email' => $email,
'name' => $name,
]
];
\Mail::to($to)->send(new \App\Mail\Hello);
You can use the Mail::send() function that inject a Message class in the callable. The Message class has a function to($email, $name) with the signature you're searching, i.e.:
Mail::send($view, $data, function($message) use ($email, $name) {
$m->to($email, $name);
$m->from('youremail#example.com', 'Your Name');
$m->subject('Hi there');
})
The $view could be a string (an actual view) or an array like these:
['text'=> 'body here']
['html'=> 'body here']
['raw'=> 'body here']
The $data argument will be passed to the $view.
For Laravel < 5.6 one can use this:
$object = new \stdClass();
$object->email = $email;
$object->name = $user->getName();
\Mail::to($object)->queue($mailclass);
see here
I prefer this solution as more readable (no need to use arrays and static string keys).
\Mail::send((new \App\Mail\Hello)
->to('example#gmail.com', 'John Doe');
You can use Mailable class in Laravel:
https://laravel.com/docs/5.5/mail
php artisan make:mail YouMail
These classes are stored in the app/Mail directory.
In config/mail.php you can configue email settings:
'from' => ['address' => 'example#example.com', 'name' => 'App Name'],
for Laravel 8 is like this:
$user = new User;
$user->email = 'example#example.com';
Mail::to($user)->send(new YourMail);
YourMail is Mailable class created by php artisan make:mail YourMail

Laravel 5.1 PHP

I'm new to coding and Laravel 5.1, and after watching the tutorials by Laracasts I have been creating my own webpage. I came across and error that I cant fix...
Method [send] does not exist.
My code looks like this:
namespace App\Http\Controllers;
use Mail;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ContactController extends Controller
{
/**
*
* #param Request $request
*/
public function emailContactForm (Request $request){
$msg = $request->input('message');
$name = $request->input('name');
$email = $request->input('email');
//
$this->validate($request, [
'title' => 'required|max 500',
'name' => 'required',
'email' => 'required',
]);
//
Mail::send(
'emails.contactForm',
[
'message'=>$msg,
'name'=>$name,
],
function($m) use ($email) {
$m->to('jessica.blake#autumndev.co.uk', 'say hi')
->subject('new message')
->from($email);
}
);
//
return;
}
}
I'm trying to use the mail function, which we have now got working, but the send still doesn't? Any suggestions? Thanks!
EDIT: Full stack trace as per laravel log file: http://pastebin.com/ZLiQ7Wgu
At the very first sight, you are calling the controller method send() but you actually named it emailContactForm()
You dont post routes and actions so the quick fix by now is trying to rename emailContactForm to send, despite instead you should probably need to review all your related routing logic.

CodeIgniter and Postmark Integration

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!

Categories