Here is my code :
// Set the default options for the email
$options = array(
'sender_email' => 'test1#test1.com',
'recipient_email' => 'test2#test2.com',
'subject' => 'subject'
);
// Check the value of the "data1" column in the table
if ($insert['data1'] == 'sample1') {
$options['recipient_email'] = 'test1#test1.com';
} elseif ($insert['data1'] == 'sample2') {
$options['recipient_email'] = 'test2#test1.com';
}
// Send the email using the updated options
$sent_message = Form::sendMail($options);
No matter what I do, the email is always sent to "test2#test1.com". I have written, and rewritten a dozen different ways to the same result. The sendMail() function is based off of the phpmailer found on PHP Form Builder. Is there a better alternate to phpMailer?
I swapped values between emails, I wrote :
$recipientEmail = "email1.email.com";
$options = array(
'sender_email' => 'test1#test1.com',
'recipient_email' => $recipientEmail,
'subject' => 'subject'
);
and wrote a loop that looked at the values of the post data, and changed the $recipientEmail corresponding to that data.
Related
I have a table with a lots of data in my live server database. In that case if I load the total data at a time it get 12-15 seconds which is awkward. So I need to load the table server side. I have done it via ssp.class.php . It going fine when I load the table as normal structure like below:
array( 'db' => 'tel1', 'dt' => 0 ),
array(
'db' => 'is_active',
'dt' => 1,
'formatter' => function( $d, $row) {
if($d==0){
$tr_color='no';
}else{
$tr_color='yes';
}
return $tr_color;
}
);
But I need to access a array which is define outside formatter function. I have tried to access it as global variable but it is not available formatter function. Also it is not passed as parameter. So how can I access outside variable inside formatter function:
I have tried like below:
array(
'db' => 'genere',
'dt' => 0,
'formatter' => function( $d, $row, $lang ) {
$gender = '';
if($d=='M'){
$gender = $lang['maschio'];
}else if($d=='F'){
$gender = $lang['femmina'];
}else if($d=='A'){
$gender = $lang['azienda'];
}
return $gender;
}
The above code does not return anything. It returns blank in browser network. $lang is the array of outside array. How can I pass this array inside the formatter function. Sometime it is database connection also. May be I need to fetch some record from another table. SO this time I can not do the operation.
I have tried to fetch Datatable data from ajax as server side loading. But can not access outside array or variable which is defined outside formatter function.
Currently, I have the following problem:
I have created a WordPress environment that sends personalized emails to subscribers based on their preferences. This has worked for quite some time but for a couple of months, we are experiencing some inconsistencies. These inconsistencies are as followed:
Once in a while, the foreach loop for sending the emails stops in the middle of its execution. For example, we have a newsletter with 4000 subscribers. Once in a while, the program randomly stops its sending procedure at around 2500 emails. When this happens, there are literally no signs of any errors and there is also nothing to be seen in the debug log.
I have tried the following things to fix the issue:
Different sender; we switched from Sendgrid to SMTPeter (Dutch SMTP service)
Delays; we have tried whether placing a wait after x number of emails would have any impact because there might be too many requests per minute, but this was not the case.
Disable plugins; For 5 weeks we thought we had found the problem. WordFence seemed to be the problem, unfortunately, the send function stopped again last week and this did not appear to be causing the problems. Just to show how unstable it really is. It can go well for 5 weeks and then not for 2 weeks.
Rewriting of functions
Logging, we write values to a txt file after every important step to keep track of where the send function stops. This is just to see which users have received an email and which still need to receive it so that we can continue sending it from there.
Debug log, the annoying thing is that even when we have the wp_debug on, nothing comes up that indicates a cause of crashing.
To schedule the sender I use the WP_Cron to run the task in the background. From there the following function is triggered;
Below, the code I wrote in stripped format. I removed all the $message additions as this is just HTML with some variables of ACF for the email. I translated it so it's easier to understand.
<?php
function send_email($edition_id, $post)
{
require_once('SMTPeter.php'); //Init SMTPeter Sender
$myfile = fopen("log.txt", "a") or die("Unable to open file!"); //Open custom logfile
$editionmeta = get_post_meta($edition_id); //Get data of edition
$users = get_users();
$args = array(
'post_type' => 'articles',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'asc',
'meta_key' => 'position',
'orderby' => 'meta_value_num',
'meta_query' => array(
array(
'key' => 'edition_id',
'value' => $edition_id,
'compare' => 'LIKE',
),
),
);
$all_articles = new WP_Query($args); // Get all articles of edition
$i = 0; // Counter users interrested in topic
$j = 0; // Counter sent emails
foreach ($users as $user) { //Loop over all users <---- This is the loop that not always finishes all itterations
$topic_ids = get_field('topicselect_', 'user_' . $user->ID);
$topic_id = $editionmeta['topic_id'][0];
if (in_array($editionmeta['topic_id'][0], $topic_ids)) { // Check if user is interrested in topic.
$i++; // Counter interrested in topic +1.
// Header info
$headerid = $editionmeta['header_id'][0];
$headerimage = get_field('header_image', $headerid);
$headerimagesmall = get_field('header_image_small', $headerid);
// Footer info
$footerid = $editionmeta['footer_id'][0];
$footer1 = get_field('footerblock_1', $footerid);
$footer2 = get_field('footerblock_2', $footerid);
$footer3 = get_field('footerblock_3', $footerid);
$message = '*HTML header newsletter*'; // First piece of content email
if ($all_articles->have_posts()) :
$articlecount = 0; // Set article count to check for empty newsletters
while ($all_articles->have_posts()) : $all_articles->the_post();
global $post;
$art_categories = get_the_category($post->ID); // Get categories of article
$user_categories = get_field('user_categories_', 'user_' . $user->ID); // Get categories user is interrested in
$user_cats = array();
foreach ($user_categories as $user_category) {
$user_cats[] = $user_category->name; // right format for comparison
}
$art_cats = array();
foreach ($art_categories as $art_category) {
$art_cats[] = $art_category->name; // right format for comparison
}
$catcheck = array_intersect($user_cats, $art_cats); // Check if 1 of the article's categories matches one of a user's categories
if (count($catcheck) > 0) { // The moment the array intersect count is greater than 0 (at least 1 category matches), the article is added to the newsletter.
$message .= "*Content of article*"; // Append article to content of newsletter
$articlecount++;
}
endwhile;
endif;
if ($articlecount > 0) { //As soon as the newsletter contains at least 1 article, it will be sent.
$j++; //Sent email counter.
$mailtitle = $editionmeta['mail_subject'][0]; // Title of the email
$sender = new SMTPeter("*API Key*"); // Class SMTPeter sender
$output = $sender->post("send", array(
'recipients' => $user->user_email, // The receiving email address
'subject' => $mailtitle, // MIME's subject
'from' => "*Sender*", // MIME's sending email address
'html' => $message,
'replyto' => "*Reply To*",
'trackclicks' => true,
'trackopens' => true,
'trackbounces' => true,
'tags' => array("$edition_id")
));
error_log(print_r($output, TRUE));
fwrite($myfile, print_r($output, true));
}
}
}
fclose($myfile);
}
All I want to know is the following;
Why can't my code run the foreach completely, every time? I mean, it's quite frustrating to see that it sometimes works like a charm, and the next time it could get stuck again.
Some things I thought about but did not yet implement:
Rewrite parts of the function into separate functions. Retrieving the content and setting up the HTML for the newsletter could be done in a different function. Besides the fact that it would obviously be an improvement for cleaner code, I just wonder if this could actually be the problem.
Can a foreach crash due to a fwrite trying to write to a file that is already being written to? So does our log cause the function to not run properly? (Concurrency, but is this a thing in PHP with its workers?)
Could the entire sending process be written in a different way?
Thanks in advance,
Really looking forward to your feedback and findings
Trying to update a batch of emails. I think I've tried every way to do this, but my use of DrewM's MailChimp wrapper only returns the following $result content:
Array ( [id] => 1234abcd [status] => pending [total_operations] => 0 [finished_operations] => 0
And so on. No errors, but no operations!
Essentially, my code looks like this, where $emails stores all the emails in an array.
include("MailChimp.php");
include("Batch.php");
$list_id = "1234abcd";
use \DrewM\MailChimp\MailChimp;
use \DrewM\MailChimp\Batch;
$apiKey = 'aslkjf84983hg84938h89gd-us13';
if(!isset($emails)){ // If not sending bulk requests
$MailChimp = new MailChimp($apiKey);
$subscriber_hash = $MailChimp->subscriberHash($email);
$result = $MailChimp->patch("lists/$list_id/members/$subscriber_hash",
array(
'status' => 'subscribed',
)
);
/* SENDING BATCH OF EMAILS */
} else if($emails){
$MailChimp = new MailChimp($apiKey);
$Batch = $MailChimp->new_batch();
$i = 1;
foreach($emails as &$value){
$Batch->post("op".$i, "lists/$list_id/members", [
'email_address' => $value,
'status' => 'subscribed',
]);
$i++;
}
$result = $Batch->execute(); // Send the request (not working I guess)
$MailChimp->new_batch($batch_id); // Now get results
$result = $Batch->check_status();
print_r($result);
}
If anyone can see what I'm not seeing, I'll be very grateful!
Problem solved. After talking with a rep at MailChimp, he helped to find two major problems.
Instead of using a POST method, he said to use PUT, when working with already existing emails. POST is best used for adding emails, while PUT can add and update emails.
So, change
$Batch->post
to
$Batch->put
Secondly, after successfully sending requests and getting errors in the $result, he found they were 405 errors and told me to add the md5 hash to my emails.
So, change
$Batch->post("op".$i, "lists/$list_id/members", [ ...
to
$subscriber_hash = $MailChimp->subscriberHash($value);
$Batch->put("op$i", "lists/$list_id/members/$subscriber_hash", [ ...
And they sent me a MailChimp stocking cap for being a good sport :-)
Veni. Vidi. Vici.
I'm trying to update Aweber subscriber information, specifically the custom fields and I'm using Aweber API but it's not working and probably I'm not writing correctly the code:
require_once('../AweberAPI/aweber_api/aweber_api.php');
include("../config.php");
$email=$_POST["email"];
$threefears=$_POST["3fears"];
$handlefears=$_POST["handlefears"];
$threeactions=$_POST["3actions"];
$changelife=$_POST["changelife"];
$consumerKey = '';
$consumerSecret = '';
$accessKey = '***'; # put your credentials here
$accessSecret = '***'; # put your credentials here
$account_id = ''; # put the Account ID here
$list_id = ''; # put the List ID here
$aweber = new AWeberAPI($consumerKey, $consumerSecret);
try {
$custom_field->name = 'Favorite Color';
$custom_field->save();
$params = array('email' => '$email');
$found_subscribers = $account->findSubscribers($params);
foreach($found_subscribers as $subscriber) {
$subscriber->custom_fields = array(
'Top 3 biggest fears related to dating' => '$threefears',
'How would the person you most admire handle these fears' => '$handlefears',
'What are 3 actions you can take today to act more like the person you most admire' => '$threeactions',
'How will taking these actions change your attitude towards dating and your life' => '$changelife',
);
$subscriber->save();
}
}
The custom fields you are submitting must already exist on your list before you can submit them via the API. This can be done within your aweber control panel using this process: https://help.aweber.com/hc/en-us/articles/204027516-How-Do-I-Create-Custom-Fields-
So if you created a custom field named 'age' then your code would look something like this (assuming an existing $subscriber object):
$fields = array(
'age' => '21',
);
$subscriber->custom_fields = $fields;
$subscriber->save();
or
$subscriber['custom_fields']['age'] = '21';
$subscriber->save();
I guess that instead of writing the values you are writing $threefears, $handlefears etc as text.
In your example you are putting variables as '$variable' instead of just $variable. That would write variable name instead of variable content.
so, instead of
$subscriber->custom_fields = array(
'Top 3 biggest fears related to dating' => '$threefears',
'How would the person you most admire handle these fears' => '$handlefears',
'What are 3 actions you can take today to act more like the person you most admire' => '$threeactions',
'How will taking these actions change your attitude towards dating and your life' => '$changelife',
);
try
$subscriber->custom_fields = array(
'Top 3 biggest fears related to dating' => $threefears,
'How would the person you most admire handle these fears' => $handlefears,
'What are 3 actions you can take today to act more like the person you most admire' => $threeactions,
'How will taking these actions change your attitude towards dating and your life' => $changelife
);
Note that even stackoverflow is correctly hightlighting variable names now.
And for Pete's sake, make the custom field's names shorter :) Most certainly there is a limit on the post you can make. Having such long variable name makes for less space in variable value, per post.
Oh and delete
$custom_field->name = 'Favorite Color';
$custom_field->save();
And modify from
$params = array('email' => '$email');
to
$params = array('email' => $email);
or to
$params = array('email' => $email, 'status' => 'subscribed');
It is correct that the custom fields you are submitting must already exist on your list before you can submit them via the API. This can be done within your aweber control panel using this process: https://help.aweber.com/hc/en-us/articles/204027516-How-Do-I-Create-Custom-Fields-
after creating a custom field named 'age' , the php code will be like this
$fields = array(
'age' => '21',
);
$subscriber->custom_fields = $fields;
$subscriber->save();
Am using this function to sent bulk mail through mail gun
`//sending batch mail
public function BatchMail($subject=null,$body=null, $record=null)
{
# Instantiate the client.
$mg = new Mailgun(MAILGUN_KEY);
$domain = MAILGUN_DOMAIN;
# Next, instantiate a Message Builder object from the SDK, pass in your sending domain
if(!empty($record))
{
$batchMsg = $mg->BatchMessage($domain);
# Define the from address.
$batchMsg->setFromAddress(FROM_EMAIL, array("first"=>FIRST_NAME, "last" => LAST_NAME));
# Define the subject.
$batchMsg->setSubject($subject);
# Define the body of the message.
$batchMsg->setHtmlBody($body);
# Next, let's add a few recipients to the batch job.
foreach ($record as $key => $rec) {
$batchMsg->addToRecipient($rec['email'], array("first" => $rec['fname'], "last" => $rec['lname']));
}
$batchMsg->addCcRecipient("mayname#mycompany.in", array("first" => "Sally", "last" => "Doe"));
$re = $batchMsg->finalize();
$result = $batchMsg->getMessage();
$log = $mg->get("$domain/log");
$respbody = $log->http_response_body;
$result = $mg->get("$domain/stats",array('event' => array('sent', 'opened')));
$resp = $log->http_response_code;
//$event = $mg->get("$domain/events");
$response = array("log"=>$log,"result"=>$result,"body"=>$respbody,"code"=>$resp);
return $response;
}
}`
Here mails are sending properly but I have a problem in my cc mail.
$batchMsg->addCcRecipient("mayname#mycompany.in", array("first" => "Sally", "last" => "Doe"));
This function using for adding CC mails. Mails are delivering properly but the mail reviving with headers like To: mayname#mycompany.in, Cc:mayname#mycompany.in. But the recipients mails are not listing in the headers.
Normally gamil showing recipients in the headers like this
to: test#gmail.com
cc: myname#mycompany.in
Anyone know why mail gun showing issue like this ??
Replace:
foreach ($record as $key => $rec) {
$batchMsg->addToRecipient($rec['email'], array("first" => $rec['fname'], "last" => $rec['lname']));
}
With:
foreach ($record as $key => $rec) {
$batchMsg->addBccRecipient($rec['email'], array("first" => $rec['fname'], "last" => $rec['lname']));
}
Or if you want your recipients to know who else was emailed don't use the batch message builder. See the "To" parameter in the mailgun API documentation for proper formatting.
The batch builder is for sending many distinct emails and not for composing a single email to many different recipients. It's possible that MailGun didn't anticipate you building an email to multiple recipients this way and thus there is no test coverage of this bug. You might try filing a bug report on their github page.
More details about the specification you're trying to implement might lead to a better answer.