I am trying to put an if statement when calling a class, but the operator -> produces a problem I cant figure out. So calling the class like:
$email = WP_Mail::init()
->to('myemail#hotmail.com')
->subject('This is an automated message pleas do not reply')
->template(plugin_dir_path( __DIR__ ) .'email-templates/email.php', [
'name' => 'Anthony Budd',
'email' => 'test#test.com',
'skills' => [
'PHP',
'AWS',
]
]);
if (site_url() === 'mysite'){
->send();
}else {
->render();
echo $email;
}
normally it would look like:
$email = WP_Mail::init()
->to('john.doe#gmail.com')
->template(get_template_directory() .'/emails/demo.php', [
'name' => 'Anthony Budd',
'location' => 'London',
'skills' => [
'PHP',
'AWS',
]
])
->send();
this above produces an error due to the operator, how can I return "->render()" or "->send()". I want to display the data if on my dev computer but when I push the code to me server it needs to send the email.
You're missing the object $email before calling the methods. Take a look at this:
if (site_url() === 'mysite'){
$email->send();
}else {
$email->render();
echo $email;
}
Related
I am developing an api using Laravel/Lumen. I have seen very few users are complaining that even though their emails are completely fine, my api response says The email must be a valid email address.
What I have seen is that they are giving a space by mistake after their email like 'noob#user.com '. As a result the email is not accepted by the system. What I'm using in my code so far is:
try {
$this->validate($request, [
'first_name' => 'required|min:3|max:40',
'last_name' => 'required|min:3|max:40',
'email' => 'required|email|unique:clients,email',
'profile_photo' => ''
]);
} catch (ValidationException $e) {
return response()->json($this->clientTransformer->validationFailed($e), 200);
}
I have tried adding the following lines inside the first line of try block but failed to change the $request object property.
try{
$request->email = trim($request->email, ' '); //<= or
$request->email = str_replace(' ', '', $request->email); // <= this line
$this->validate($request, [
'first_name' => 'required|min:3|max:40',
'last_name' => 'required|min:3|max:40',
'email' => 'required|email|unique:clients,email',
'profile_photo' => ''
]);
}
but these arent working. this is passing the exact same email to the validate method. Is there any quick way to do it?
You can use:
$request->replace(array('email' => trim($request->email)));
or
$request->merge(array('email' => trim($request->email)));
Source:
https://laracasts.com/discuss/channels/general-discussion/laravel-5-modify-input-before-validation
I'm trying to add a user to a list through an API. However, I'm getting this error returned:
{"errors":[{"code":"parsing_error","message":"JSON parsing error: The property '#/' of type Hash did not match one or more of the required schemas"}]}
This is what I'm sending: {"subscribers":{"email":"me#gmail.com"}}
Here is the PHP code:
$subscriberInfo = [
'subscribers' => array (
'email' => $email
)
];
$encoded = json_encode($subscriberInfo);
Is there something wrong with the structure of the JSON?
That's not the format described in the documentation. subscribers should be an array, not an object:
$subscriberInfo = [
'subscribers' => [
['email' => $email]
]
];
$encoded = json_encode($subscriberInfo);
Another possible issue that I encountered when receiving the same error code is that it didn't like a null value.
$firstName = 'Paul';
$lastName = null;
$subscriberInfo = [
'subscribers' => [
[
'email' => $email,
'first_name' => $firstName,
'last_name' => $lastName,
],
]
];
With the null coalescing operator, it can fall back to an empty string.
$firstName = 'Paul';
$lastName = null;
$subscriberInfo = [
'subscribers' => [
[
'email' => $email,
'first_name' => $firstName,
'last_name' => $lastName ?? '',
],
]
];
The above snippet resolved my error code when attempting a batch subscriber 'create or update'.
I am using SparkPost PHP API for sending emails and it seems like reply_to feature is not working. I tried to both ways with headers and with reply_to field. Any ideas what could be wrong? Domain name of reply_to emails is different as senders one. I didn't found any restrictions regarding this in their documentation. Any ideas?
Here is my code:
$emailData = array(
'from'=> $data["from_name"].' <'.$data["from_email"].'>',
'html'=> $data["html"],
'inline_css' => true,
'transactional' => true,
'subject'=> $data["subject"],
'recipients'=> $rec["r"]
);
if(isset($data["headers"]["Reply-To"]))
$emailData['reply_to'] = $data["headers"]["Reply-To"];
try {
// Build your email and send it!
$this->mandrill->transmission->send($emailData);
} catch (\Exception $err) {
echo "<pre>";
print_r($err);
echo "</pre>";
}
Regarding: SparkPost PHP ReplyTo, reply_to, Reply
For anyone else wondering the same thing. Here's my implementation using SparkPost client library for PHP v2.1. I hope it helps.
I used the transmissions endpoint as seen in the docs.
https://github.com/sparkpost/php-sparkpost
$promise = $sparky->transmissions->post([
'content' => [
'from' => [
'name' => 'Company Name',
'email' => 'noreply#company.com',
],
'reply_to' => $email,
'subject' => 'Some Subject',
'html' => $html_message,
'text' => $text_message,
],
'substitution_data' => $subData,
'recipients' => [
[
'address' => [
'name' => 'My Recipient',
'email' => 'me#company.com',
]
],
],
]);
Thank god for slack :)
Solution is that SparkPost has different name for parameters in API documentation. Correct parameter for PHP API is not reply_to (as it's written in doc) but replyTo.
I'm new to web dev and I'm experimenting with Braintree webhooks. I'm using their create submerchant example code to create a submerchant and then supposedly a notification is supposed to reach my server that says if it was successful or not.
My method: I refresh the submerchant.php page (I'm using Wordpress on a NameCheap server), which then echo's "Success!". Then I go to the webhooks.php page and refresh it. However, the var_dump's only return NULL NULL and the print_r's don't return anything. Why does print_r not show anything?
submerchant.php - this creates the submerchant when I set $one = 1 and set a new id for the submerchant
<?php
require_once(__DIR__ . '/../braintree/lib/Braintree.php');
Braintree_Configuration::environment('sandbox');
Braintree_Configuration::merchantId('A');
Braintree_Configuration::publicKey('B');
Braintree_Configuration::privateKey('C');
function fd_create_sm() {
$one;
$one = 1;
if($one=1) {
$merchantAccountParams = [
'individual' => [
'firstName' => 'Janez',
'lastName' => 'Doe',
'email' => 'jane#14ladders.com',
'phone' => '5553334444',
'dateOfBirth' => '1981-11-19',
'ssn' => '456-45-4567',
'address' => [
'streetAddress' => '111 Main St',
'locality' => 'Chicago',
'region' => 'IL',
'postalCode' => '60622'
]
],
'business' => [
'legalName' => 'Jane\'s Ladders',
'dbaName' => 'Jane\'s Ladders',
'taxId' => '98-7654321',
'address' => [
'streetAddress' => '111 Main St',
'locality' => 'Chicago',
'region' => 'IL',
'postalCode' => '60622'
]
],
'funding' => [
'descriptor' => 'Red Ladders',
'destination' => Braintree_MerchantAccount::FUNDING_DESTINATION_BANK,
'email' => 'funding#blueladders.com',
'mobilePhone' => '5555555555',
'accountNumber' => '1123581321',
'routingNumber' => '071101307'
],
'tosAccepted' => true,
'masterMerchantAccountId' => "na",
'id' => "green_ladders"
];
$result = Braintree_MerchantAccount::create($merchantAccountParams);
$result->success;
if($result->success) {
echo 'Success!';
} else {
print_r($result->errors);
$errordata;
echo '***********';
$BT_Errors = new Braintree_Error_ErrorCollection($errordata);
echo '***********';
$BT_Errors->deepAll();
echo '***********';
$BT_Errors->onHtmlField("transaction[amount]");
}
$result->merchantAccount->status;
$result->merchantAccount->id;
// "blue_ladders_store"
$result->merchantAccount->masterMerchantAccount->id;
// "14ladders_marketplace"
$result->merchantAccount->masterMerchantAccount->status;
// "active"
} else {
return;
}
}
fd_create_sm();
?>
webhooks.php
<?php
var_dump($_POST['bt_signature']);
var_dump($_POST['bt_payload']);
print_r($_POST['bt_signature']);
print_r($_POST['bt_payload']);
?>
Most likely, the outputted data is stored within some output buffer. If you're pretty sure you want to debug your code this way, try adding wp_die(); call right after you output data using print_r. That should help!
One more thing: sometimes some of the code (not this particular case) is actually never outputted due to more complex data flow. For this cases it might be a good idea to use some 3-rd party debugging tools or, if you're looking for simpler solution, you can write some of the output to some log file, and check the file afterwards.
Good Luck!
add die; after last line of print_r()
I am using sync (local driver) for pushing up a queue in a update method of EmailCampaignController, which uses another method of the same controller named emailQueue
like this
Queue::push('EmailNewsletterController#emailQueue', array('campaign_id' => $campaign_id));
The emailQueue uses a foreach loop which runs correctly for once after that it gives error as if the $campaign_id is undefined
here is the emailQueue method
public function emailQueue($job, $data) {
// Queue Starts here
$emailCampaign = EmailCampaign::find($data['campaign_id']);
$emailCampaign->status = 'In Progress';
$emailCampaign->last_activity = Carbon::now();
$emailCampaign->save();
$data = $emailCampaign->emailCampaignNewsletter;
$contacts = $emailCampaign->contactList->contacts;
foreach ($contacts as $contact) {
$emailBody = [
'message' => [
'subject' => $data['email_title'],
'html' => $data['email_body'],
'from_email' => $data['from_email'],
'to' => [['email' => $contact['email_address']]]
]
];
$response = Mandrill::request('messages/send', $emailBody);
EmailCampaignRecord::create([
'email_campaign_id' => $data['campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
$contact->last_activity = Carbon::now();
$contact->save();
}
$emailCampaign->status = 'Sent';
$emailCampaign->save();
$job->delete();
// Ends here
}
What am I doing wrong here? why is it not working like a normal loop ?
The problem was with email_campaign_id to be null because $data['campaign_id'] was null the correct foreign key was $data['email_campaign_id'] that's what stopped the process - I should have tested it before putting it in the queue
after changing the code
EmailCampaignRecord::create([
'email_campaign_id' => $data['campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
to
EmailCampaignRecord::create([
'email_campaign_id' => $data['email_campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
the problem was solved