Drupal 7 how to send drupal_email only BCC without to addres - drupal

I want to send email only Bcc no need to address. is it passible in Drupal 7 drupal_mail()

Yes, You can send BCC email in D7.
here is sample code you can try:
$cc = 'ccmail#mail.com';
$bcc = 'bccmail#mail.com';
$to = '';
$from = 'tomail#mail.com';
$params = array(
'body' => 'Body Message',
'subject' => 'Your Subject',
'headers' => array(
'Cc' => $cc, // optional
'Bcc' => $bcc, // optional
),
);
drupal_mail('mymodule', 'key', $to, $lang, $params, $from);
This will send email to all the email address mentioned in Bcc/cc and $to would be blank.
or you can simply add mutiple email ids without space in $to for individual mail
$to = 'emailone#mail.com,emailtwo#mail.com';
Or Refere this Post - https://drupal.stackexchange.com/questions/6677/how-to-programmatically-send-an-email
hope this will help you.

Related

How to connect WordPress / WooCommerce to a REST API with PHP?

I am trying to write my first REST API with PHP. We want to send shipping data to the shipping company and get labels for printing in return. The shipping company has given me test api credentials.
So I started to write a plugin with this code inside:
$url = "https://mywebsite/endpoint/";
$username = "myusername";
$password = "mypassword";
$data = array(
'key1' => 'value1',
'key2' => 'value2'
);
$response = wp_remote_post( $url, array(
'body' => $data,
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
),
)
);
var_dump($response); // not being called
When I run this code, I get a white screen with no error message, the last line with var_dump($response) is not being called. I am a bit stuck here because I se no success and no error...what could it be? Why this behaviour of the remote server? Does it mean "sorry, wrong credentials" or "data has the wrong format"? I have the feeling that the server doesn't even notice that I'm trying to contact him...
I tried a number of other variatons of the above code that I found somwehere online, and also outside of WordPress, but no success.
The shipping company gave me a documentation. It says:
HTTP Method: POST
Authentication Header: Basic Authentication (user name/ password)
so I thought I could do no wrong when trying that. Hm.
I found a solution, so I can share it here with you.
I installed the free Software Postman and created a simple POST query there. After entering $url, $username and $password I ran a query and it worked well. the good thing abort Postman is that it creates code snippets for you. "very_long_cryptic_string" is obviously created from $username and $password. The PHP for my case goes like this, and it works:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Authorization: Basic very_long_cryptic_string'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

How to submit contact form 7 programmatically

I want to submit contact form by custom function
The code below is getting the instance of form but when submitted. It submit the form but not the fields which I wanted.
$item = wpcf7_contact_form( $formId );
$result = $item->submit();
Here where I can pass the fields I define in admin panel like "textarea-123" & "email-234" ?
I did not get exact answer for what I look but I found the alternate solution.
function cf7Submit($formId , $args) {
$url = 'http://example.com/wp-json/contact-form-7/v1/contact-forms/'.$formId.'/feedback';
$response = wp_remote_post( $url, array(
'method' => 'POST',
'body' => $args
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
echo 'Response:<pre>';
print_r( $response );
echo '</pre>';
}
}
I can call this function like this:
cf7Submit(128, array(
'textarea-123' => 'test email',
'email-234' => 'asd#asd.com'));
#daraptoor has found a good solution, but as #davevsdave noticed in the comment, it does not work properly in CF7 5.6.
Error 415 is caused by added to API check for content type passed into a request header:
// part of create_feedback() from CF7's rest-api.php
if ( ! str_starts_with( $content_type, 'multipart/form-data' ) ) {
To figure it out, just add the expected content type into a request header:
$response = wp_remote_post( $url, array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'multipart/form-data'
),
'body' => $args
)
);
UPD
Faced with an issue, that wp_remote_post() send data in body and not in POST, so CF7 API does not get any fields. It is caused because the WP's function uses http_build_query() (read more here).
I have used cURL request as a workaround:
// Same user agent as in regular wp_remote_post().
$userAgent = 'WordPress/' . get_bloginfo('version') . '; ' . get_bloginfo('url');
// Note that Content-Type wrote in a bit different way.
$header = ['Content-Type: multipart/form-data'];
// Same array with fields to pass, not changed.
$body = ['foo' => 'bar'];
$curlOpts = [
// Send as POST
CURLOPT_POST => 1,
// Get a response data instead of true
CURLOPT_RETURNTRANSFER => 1,
// CF7 will reject your request as spam without it.
CURLOPT_USERAGENT => $userAgent,
CURLOPT_HTTPHEADER => $header,
CURLOPT_POSTFIELDS => $body,
];
$ch = curl_init($apiUrl); // Create a new cURL resource.
curl_setopt_array($ch, $curlOpts); // Set options.
$response = curl_exec($ch); // Grab response.
if (!$response) {
// Do something if an error occurred.
} else {
$response = json_decode($response);
// Do something with the response data.
}
// Close cURL resource, and free up system resources.
curl_close($ch);
Hope it saves someones time :)
You can add a piece of JS code, like:
$("form.wpcf7").submit()

Send Gravity Forms to 3rd Party API and check for duplicates at API before sending

Ok I'm hoping that this is pretty straightforward. But I doubt it.
I currently have a Gravity Forms form which is sending to an external API successfully. Using the PUT method. This is fine. But now my client wants to add some extra functionality to check for duplicates within the API before it will submit. Essentially making sure that the user hasn't already registered. I'm using this method within my functions.php but this only sends to the third party it doesn't do any checking of duplicates:
add_action( 'gform_after_submission_1', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {
$post_url = '#URL#';
$body = array(
'name' => rgar( $entry, '1' ),
'email' => rgar( $entry, '2' ),
'password' => rgar( $entry, '3' ),
);
GFCommon::log_debug( 'gform_after_submission: body => ' . print_r( $body, true
) );
$request = new WP_Http();
$response = $request->post( $post_url, array( 'body' => $body ) );
GFCommon::log_debug( 'gform_after_submission: response => ' . print_r(
$response, true ) );
}
I need to be able to get the form to check if the email address is used on the system and if so then to not send the form, and to display an error.
Please help.... i'm really stuck :(
Using post_to_third_party you are able only to send it where third party can compare values.
If I understood well what you need is:
-get email field from the form,
-get list of registered emails,
-compare it,
-submit or don't submit the form.
Take a look at https://docs.gravityforms.com/gform_pre_submission/
I would start from this point:
//MySQL
$servername = "localhost";
$username = "root";
$password = "password.";
$dbname = "wordpress";
//Create connection
$wpdb2 = new wpdb($username,$password,$dbname,$servername);
To get email of user who is submmiting the form:
$user = wp_get_current_user();
$user_email = $user->email;
OR you are able to get the user email from form field inside of the hook that you will use
add_action( 'gform_pre_submission', 'pre_submission_handler' );
function pre_submission_handler( $form ) {
$user_email = $entry["1"];
}
To get emails of registered users you can use
$wpdb->get_results( 'SELECT * FROM `users` WHERE `email` = '.$user_email , ARRAY_A);
I hope this hints will make you hit the road again.
If you need more help - I will try my best.

Get email from shipping adrdress in woocommerce

I'm developing a website where we sell digital products.
For requirement when the buyer ask to send the license to another email the buyer uses shipping address to enter the extra email.
so I added the email with the following function.
/*
* Add email to "shipping address"
*/
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_email'] = array(
'label' => __('Email Address', 'woocommerce'),
'placeholder' => _x('Email', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
The shipping form displays properly the email on the shipping section, but when order is completed I'm trying to catch that email and I haven't had it.
I'm using the following function to add the email to the recipient before the email is sent.
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'my_email_shipping_filter_function', 20, 2);
function my_email_shipping_filter_function ($recipient, $order){
$shipping_email = get_post_meta( $order->id, '_shipping_email', true );
if ($shipping_email != '') {
$recipient = $recipient.', '.$shipping_email;
return $recipient;
}
}
But it is not working when the confirmation is sent, however when Im redirected to thank you page I can use the same function and display the shipping email.
Hope someone can help.

Send request to other server in wordpress

I have two website in wordpress
Main site
1.)http://www.abc.com
Other Site
2.)http://www.xyz.com
From mainsite I want to send username and password to othersite for this i am using this code
$creds = array();
echo $creds['user_login'] = "sachin";
echo $creds['user_password'] = "pass";
$user = wp_signon( $creds, false );
// echo ABSPATH . WPINC;
if ( is_wp_error($user) ){
echo $user->get_error_message();
if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC. '/class-http.php' );
// You would edit the following:
$username = $creds['user_login']; // Twitter login
$password = $creds['user_password']; // Twitter password
$message = "Hey neat, I'm posting with the API";
// Now, the HTTP request:
$api_url = 'http://www.xyz.com';
$body = array( 'status' => $message );
$headers = array( 'Authorization' => 'Basic '.base64_encode("$username:$password") );
$request = new WP_Http;
$result = $request->request( $api_url , array( 'method' => 'POST', 'body' => $body, 'headers' => $headers ) );
// echo"<pre>";print_r($result['body']);
I dont know i am sending wp http request in right way or not but my problem is i want this username and password(i am sending from main site) to other server (http://www.xyz.com)
How I can do that please suggest me.please provide me your valuable suggestion how I can send username and password from one server to other server.
Thanks
To share 2 wordpress installs so that users can access both then this page will let you do that.
http://www.remicorson.com/share-users-database-on-different-wordpress-installs/
I think this is what your trying to do?

Resources