With \Mail::send can not set title - laravel

In Laravel 6 I send email with \Mail::send (DataGrip in .env) and I recieve the email, can not set title.
is is the same as content of the email.
I do as :
$data = array ( // That is not applied
'title' => 'Title 234',
'bodyMessage' => $message_text,
'sender_username' => $name,
'sender_email' => $email,
'site_name' => $site_name,
);
\Mail::send ( 'emails/contact_us', $data, function ($message) use($data, $donotreply_email, $contact_us_email) {
$message->from ( $donotreply_email, 'Support of ' . $data['site_name'] );
$message->to ( $contact_us_email )->subject ( $data['bodyMessage'] );
// $message->title('Message 34 Title'); // If to uncomment it it does not work too.
} );
Which is valid way ?
Thanks!

If you want to set a mail title it means mail subject. So set subject instead of title.
$message->subject('Message 34 Title');

Related

Wordpress Lazy Loading using Pagination

im doing a lazy loading with WP_Query Pagination
it's working fine but the content duplicate itself when it reaches it's end
and when i search for a specific result it shows the result correctly
but after that it still want to do lazy load so it load random data
here is my code
lazy-load.php
<?php
add_action('wp_ajax_nopriv_load_posts_by_ajax', 'load_posts_by_ajax_callback');
add_action('wp_ajax_load_posts_by_ajax', 'load_posts_by_ajax_callback');
function load_posts_by_ajax_callback(){
// check_ajax_referer( 'load_more_posts', 'security' );
$paged = $_POST['page'];
$args = array(
'post_type' => 'unit',
'post_status' => 'publish',
'posts_per_page' => 4,
'paged' => $paged
);
if ( !empty($_POST['taxonomy']) && !empty($_POST['term_id']) ){
$args['tax_query'] = array (
array(
'taxonomy' => $_POST['taxonomy'],
'terms' => $_POST['term_id'],
),
);
}
if ( ! is_null($_POST['offer']) ) {
$args['meta_query'][] = array(
'key' => 'WAKEB_hot',
'value' => '1',
'compare' => '=',
);
}
if ( ! is_null($_POST['purpose']) ) {
$args['meta_query'][] = array(
'key' => 'WAKEB_vacation',
'value' => '1',
'compare' => '=',
);
}
if (!empty($_POST['project'])){
$args['meta_query'] = array (
array(
'key' => 'WAKEB_project',
'value' => $_POST['project'],
'compare' => '='
),
);
}
// start buffer
ob_start();
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while($query->have_posts()){ $query->the_post();
get_template_part("template-parts/units");
}
endif; wp_reset_postdata();
// start buffered data in data variable
$data = ob_get_clean();
wp_send_json_success( $data );
wp_die();
}
add_action('wp_ajax_nopriv_load_projects_by_ajax', 'load_projects_by_ajax_callback');
add_action('wp_ajax_load_projects_by_ajax', 'load_projects_by_ajax_callback');
function load_projects_by_ajax_callback(){
// check_ajax_referer( 'load_more_posts', 'security' );
$paged = $_POST['page'];
$args = array(
'post_type' => 'project',
'post_status' => 'publish',
'posts_per_page' => 4,
'paged' => $paged
);
if ( ! is_null($_POST['ptype']) ) {
$args['tax_query'] = array (
array(
'taxonomy' => 'pptypes',
'field' => 'slug',
'terms' => $_POST['ptype'],
),
);
}
if ( !empty($_POST['taxonomy']) && !empty($_POST['term_id']) ){
$args['tax_query'] = array (
array(
'taxonomy' => $_POST['taxonomy'],
'terms' => $_POST['term_id'],
),
);
}
// start buffer
ob_start();
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while($query->have_posts()){ $query->the_post();
get_template_part("template-parts/projects");
}
endif; wp_reset_postdata();
// start buffered data in data variable
$data = ob_get_clean();
wp_send_json_success( $data );
wp_die();
}
lazy-load.js
$('.unit-terms li a').each( function() {
if ( this.href == window.location.href ) {
$(this).parent().addClass('current');
}
});
main.js
(function($){
$('.isotope a').on('click', function(){
$('.isotope .active').removeClass('active');
$(this).addClass('active');
var filter = $(this).data('filter');
if(filter=='*'){
$('.property').show();
}else{
$('.property').not(filter).hide();
$('.property'+filter).show();
}
return false;
});
})(jQuery);
so how can i make it work? i don't know what im doing wrong here
Here is the repo link for the full project
https://github.com/Ov3rControl/hoomerz
ok, now I understand what you meant ;) During lazy load you send to backend only page number without current state of filters and / or search string. So it sends all posttype items based on page number only. You should send also current state of filters
main.js: add this to your after-page-load function:
var currentUrl = new URL(window.location.href);
var searchQuery = urlObj.searchParams.get("k");
lazy-load.js: add search param to data posted to backend
var data = {
'action': 'load_posts_by_ajax',
'page': page,
'search: searchQuery // new field
};
lazy-load.php: add search param to WP_Query
if ( isset($_POST['search']) && !empty($_POST['search']) ){ // new section
$args['s'] = sanitize_text_field($_POST['search']);
}
That's example for text search filter. For all filters you must
1. match every filter from front (URL get param) (main.js)
2. than put it in data object sent to backend (lazy-load.js)
3. address this variable in lazy-load.php in if(isset($_POST['param-name'])) section ( new or existing one as there are some )
I suggest to try without ob_start() / ob_get_clean(). Also if you generate html instead of raw data structure, I would simply print it to output without wp_send_json_success().
Other solution would be sending raw data (1. array in php, 2. json_encode(), 3. wp_send_json() ) and than processing in javascript (dynamic dom element creation after request to backend made).

Email Attachments Joining together

I am building a system in which you can send email at a desired date and time, there is an interface which captures this data and through a cron the email is sent.
This is all working as expected but I am having an issue with the attachments.
When the cron runs and sends the email it seems as if there is more than 1 email going out, the 2nd email adds the attachments of the previous email sent to itself, and this carries on. I have tried unsetting the value of the array which holds the file names but still no luck.
Email A -- (Has 3 Attachments / Sends 3 Attachments)
Email B -- (Has 2 Attachments / Sends 5 Attachments)
Email C -- (Has 2 Attachments / Sends 7 Attachments)
This is strange because this is my array output after each loop:
Successful
Array
(
[id] => 38
[company_id] => 225
[message_sender] => Favour Sanctuary
[message_subject] => Delayed Email Testing
[recipients] => xxxxxxxx#gmail.com
[message_body] => I hope you get this and the pictures
[message_type_id] =>
[time_sent] => 2019-05-08 14:17:27
[attach_file] => ["instagram8.png","networking.jpg"]
[date] => 2019-05-08
[time] => 13:19:00
[sent] => 0
)
Successful
Array
(
[id] => 39
[company_id] => 225
[message_sender] => Verdana State
[message_subject] => Attachment Mail
[recipients] => xxxxxxxx#gmail.com
[message_body] => This time you will get it
[message_type_id] =>
[time_sent] => 2019-05-08 14:33:16
[attach_file] => ["checkout.gif","kokoseller_new_logo.jpg","data_empty.png"]
[date] => 2019-05-08
[time] => 14:35:00
[sent] => 0
)
Successful
Array
(
[id] => 40
[company_id] => 225
[message_sender] => Kwame Eugene
[message_subject] => Memories of FFx
[recipients] => xxxxxxxx#gmail.com
[message_body] => Lets hope this goes well
[message_type_id] =>
[time_sent] => 2019-05-08 16:31:34
[attach_file] => ["evidence.pdf","kokoelec.jpg"]
[date] => 2019-05-08
[time] => 16:35:00
[sent] => 0
)
Here is my Cron Function:
function delayed_email(){
$check_date = date('Y-m-d');
$check_time = date('H:i:s');
$events = $this->db->query('SELECT * from record_mail where `date` IS NOT NULL and `time` IS NOT NULL and (`sent` = 0 OR `sent` IS NULL)')->result_array();
foreach($events as $e){
if($e['date']==$check_date && $e['time'] <= $check_time){
$company_id = $e['company_id'];
$subject = $e['message_subject'];
$message = $e['message_body'];
$sender = $e['message_sender'];
$recipients = $e['recipients'];
$attach_files = json_decode($e['attach_file'], TRUE);
$send = regular_email($recipients,$message,$subject,$attach_files,$company_id,$sender);
unset($attach_files);
$attach_files = array();
}
if ($send == TRUE){
echo 'Successful';
echo '<pre>';
print_r($e);
echo '</pre>';
//$params =array('sent'=>1);
//$this->db->where('id', $e['id']);
//$this->db->update('record_mail',$params);
}else{
echo 'failed';
}
}
}
Here is my send email Helper:
function regular_email($recipients, $message=NULL, $subject=NULL, $attach_files= NULL, $company_id=NULL, $sender=NULL){
$CI =& get_instance();
$CI->load->database();
$CI->load->library('email');
$CI->load->helper('url');
$to = $recipients ? $recipients : 'xxxxxx#gmail.com';
$subject = $subject ? $subject: 'Correspondence';
$body = $message;
$sender = $sender ?: company_name($company_id);
$CI->email->from('xxxxxxx#gmail.com' , $sender);
$CI->email->reply_to('xxxxxxx#gmail.com');
$CI->email->to($to);
$CI->email->subject($subject);
$CI->email->message($body);
if(is_array($attach_files)) {
foreach($attach_files as $k => $v){
$attach = base_url('uploads/email_attach_file/') . '/' . $v;
$CI->email->attach($attach);
}
} else {
//ATTACH ONE
$attach = base_url('uploads/email_attach_file/') . '/' . $attach_files;
$CI->email->attach($attach);
}
$result = $CI->email->send();
if($result){
return TRUE;
} else {
return FALSE;
};
/*if(!$CI->email->send()){
return $CI->email->print_debugger();
} */
}
Since you tagged this with PHPMailer, I assume that PHPMailer is being used behind the scenes. When you call $CI->email->attach($attach);, it retains the list of files you've attached, i.e. it always adds attachments, it doesn't set them. Notice that the number of attachments always increases by the number of files you've added each time, so the first one gets 3, you add 2, making 5, then add another 2, making 7. It's not your input arrays that are at fault, it's the internal list of attachments held within your mail instance ($CI->email).
If PHPMailer is inside there, you need to call the clearAttachments() method to clear them - I'd assume that CI has an equivalent or a wrapper for it - refer to their docs.

Telegram Bot, how to send a phone number as a link on a message

I'm trying to send a message with a phone number as a link. Is there a way to acomplish that? I'm using the Laravel Telegram Bot API
public function start( $telegram, $chatid ) {
$keyboard = [
['Continue'],
];
$reply_markup = Telegram::replyKeyboardMarkup([
'keyboard' => $keyboard,
'resize_keyboard' => true,
'one_time_keyboard' => true
]);
$text = "<b>¡Hola!</b> " . config('constants.emojis.waving-hand') .
" Hey I'm Bot. " . config('constants.emojis.hug-face') .
"\nPlease chose some option" .
config('constants.emojis.glass') . "\nOr contact us at <a href='tel:018099999999'>HEREÍ</a> to talk with an specialist.";
$this->sendMessage( $telegram, $chatid, $text, $reply_markup );
}
Also here is my send message function
public function sendMessage( $telegram, $chatid, $text, $reply_markup ) {
if ( !is_null( $reply_markup ) ) {
$response = $telegram->sendMessage([
'chat_id' => $chatid,
'text' => $text,
'parse_mode' => 'HTML',
'reply_markup' => $reply_markup
]);
} else {
$response = $telegram->sendMessage([
'chat_id' => $chatid,
'text' => $text,
'parse_mode' => 'HTML',
]);
}
}
Thanks in advance.
you can use the phone number with plus and country code:
+989123456789
if you use that format, telegram automatically link the number!

Sending a campaign to a segment

Anyone know how to get a campaign to send to a segment? This code isn't working. It will send an email to all people in the campaign. It will not use the segment. (This is some more text so I can get it to pass the validation of SO.)
My code:
//get member list
$memberArray = $api->listMembers($inStockListId);
foreach ($memberArray['data'] as $member) {
$memberInfo = $api->listMemberInfo($inStockListId, $member['email']);
$_productId = $memberInfo['data'][0]['merges']['PRODUCTID'];
$productId='34';
if ($productId == $_productId) {
array_push($emailArray, $member['email']);
}
}
//create new segment for campaign
$listStaticSegmentId = $api->listStaticSegmentAdd($inStockListId, 'inStockStaticSegment');
//add members to segment
$val = $api->listStaticSegmentMembersAdd($inStockListId, $listStaticSegmentId, $emailArray);
$conditions = array();
$conditions[] = array(
'field' => 'email',
'op' => 'like',
'value' => '%'
);
$segment_options = array(
'match' => 'all',
'conditions' => $conditions
);
$type = 'regular';
$options = array(
'template_id' => $campaignTemplateId,
'list_id' => $inStockListId,
'subject' => 'In-Stock Notification',
'from_email' => 'from#email.com',
'from_name' => 'My From Name'
);
$content = array(
'html_main' => 'some pretty html content',
'html_sidecolumn' => 'this goes in a side column',
'html_header' => 'this gets placed in the header',
'html_footer' => 'the footer with an *|UNSUB|* message',
'text' => 'text content text content *|UNSUB|*'
);
$newCampaignId = $api->campaignCreate($type, $options, $content, $segment_options);
I figured it out. Essentially, here is the flow. If you want detailed code, send me a message and I'll be glad to help.
$api = new MCAPI($this->_apiKey);
$api->listMemberInfo($this->_listId, $member['email']);
$api->listUpdateMember($this->_listId, $member['email'], $mergeVars);
$api->listStaticSegmentDel($this->_listId, $segment['id']);
$api->listStaticSegmentAdd($this->_listId, $segmentName);
$api->listStaticSegmentMembersAdd($this->_listId, $segmentId, $emailArray);
$api->campaignCreate($type, $options, $content, $segment_options);
//$api->campaignSendTest($newCampaignId, array($member['email']));
$api->campaignSendNow($newCampaignId);
Note, this is done via the MailChimp PHP API.

How to get and set image attachment metadata in wordpress?

I have post in WP. This post has attached image as attachment. I set description of this image via dialog (gallery tab) in post edit section.
Now I need WP functions to programmatically get all metadata for this attachment (description,title,caption,...) and another function to set the same data.
What functions to use ?
use get_children() to retrive atachement for posts.
$args = array(
'numberposts' => -1,
'order'=> 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => null,
'post_type' => 'attachment'
);
$attachments = get_children( $args );
a full example here Get URLs, Captions & Titles for Images Attached to Posts in WordPress
This works for me:
<?php
foreach ( $attachments as $attachment_id => $attachment ) {
$src = wp_get_attachment_image_src( $attachment_id, 'full' );
var_dump($src);
} ?>
array
0 => string 'http://example.com/wp-content/uploads/2009/08/DSC00261.JPG' (length=63)
1 => int 1632
2 => int 1224
3 => boolean false
The order of the array is allocated as follows.
$src[0] => url
$src[1] => width
$src[2] => height
$src[3] => icon

Resources