How to send attachment using mailgun? - php

I am trying to send attachment using curl through mailgun but its sending email with out attachment.(I am receiving email with only html part but no attachments.)
Here is my code
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:'.$api_key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/'.$domain.'/messages');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
$result = curl_exec($ch);
curl_close($ch);
return $result;
$postFields is array with following value.
Array
(
[from] => ***
[to] => ***
[subject] => ***
[html] => ***
[attachment] => Array
(
[0] => /home/***/n.jpg
[1] => /home/***/n2.jpg
)
)

Related

chatwoot api - create message

I would like to send a message with the chatwoot api.
https://www.chatwoot.com/developers/api/#tag/Messages/operation/create-a-new-message-in-a-conversation
I tried this code:
<?php
$ch = curl_init("https://mydomain.de/api/v1/accounts/1/conversations/100/messages");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(
array(
"content" => "string",
"message_type" => "outgoing",
"private" => true,
"content_type" => "cards"
)
));
curl_setopt($ch, CURLOPT_HTTPHEADER,
array (
'api_access_token: XXX'
),
);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
echo '<pre>';
print_r($response);
echo '</pre>';
?>
Response:
Array
(
[errors] => Array
(
[0] => You must log in or register before you can proceed.
)
)
Any idea?

VirusTotal Api unclear usage

I scan files from folder ($cfile has fake content intentionally)
$api_key = getenv('VT_API_KEY') ? getenv('VT_API_KEY') :$this->_key;
$cfile = 'asdasdas';
$post = array('apikey' => $api_key,'file'=> $cfile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.virustotal.com/vtapi/v2/file/scan');
curl_setopt($ch, CURLOPT_POST, True);
curl_setopt($ch, CURLOPT_VERBOSE, 1); // remove this if your not debugging
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); // please compress data
curl_setopt($ch, CURLOPT_USERAGENT, "gzip, My php curl client");
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,True);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
And get fine result with something like :
stdClass Object
(
[scan_id] => ffd8f48291484e1373573bfedf591cdf38565fcc9fc89a4a1d0f16a98da4d4ec-1573916013
[sha1] => d8cc708a1760020e00e1da0e83dc1f7b25e5a5ab
[resource] => ffd8f48291484e1373573bfedf591cdf38565fcc9fc89a4a1d0f16a98da4d4ec
[response_code] => 1
[sha256] => ffd8f48291484e1373573bfedf591cdf38565fcc9fc89a4a1d0f16a98da4d4ec
[permalink] => https://www.virustotal.com/file/ffd8f48291484e1373573bfedf591cdf38565fcc9fc89a4a1d0f16a98da4d4ec/analysis/1573916013/
[md5] => 7ae6fa4e9595a6e9d7987b4cd66f5b7f
[verbose_msg] => Scan request successfully queued, come back later for the report
)
Why do I set file wrong and have good result ?

Get size of remote file from google drive URL

Is there a way to get the size of a remote file https://drive.google.com/uc?export=download&id=54fs54dg45f1f112f2 before downloading the file?
You could make a curl request to get sizeBytes.
<?php
$id = '0ByzJffaEk18uN2ZLeGlIRGVOaDJmWS1WU1RUN3d***';
$ch = curl_init('https://drive.google.com/uc?id='.$id.'&export=download');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, []);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
$object = json_decode(str_replace(')]}\'', '', $result));
print_r($object);
Result:
stdClass Object
(
[disposition] => SCAN_CLEAN
[downloadUrl] => https://doc-08-8o-docs.googleusercontent.com/docs/...
[fileName] => filename.sh
[scanResult] => OK
[sizeBytes] => 4936
)

how to send email attachments using curl php

I am using mailgun api to send emails. My php version is 5.3
I have to attach files which is not multipart/form data. I mean I have absolute paths of files, so how can I send attachments through Curl php?
I saw mailgun document. I found this:
File attachment. You can post multiple attachment values. Important: You must use multipart/form-data encoding when sending attachments.
But I have only absolute paths not multipart/form data. So how can I attach now?
Adding headers as:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
Here is my input array sending through mailgun API:
Array (
[from] => xyz
[to] => abc#gmail.com
[subject] => ryreyreyre
[text] => yreyreyreyreyre<br /> <br />Sincerely,<br />xyz
[html] => yreyreyreyreyre<br /> <br />Sincerely,<br />xyz
[attachment] => Array
(
[0] => #/var/www/vhosts/download/attachment/1418034032618discover.png
[1] => #/var/www/vhosts/download/attachment/1418034032395master.png
[2] => #/var/www/vhosts/download/attachment/1418034032208visa.png
)
)
Here is my curl request
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:'.MAILGUN_APIKEY);
if(!empty($postArr['attachments']))
{
curl_setopt ($ch, CURLOPT_VERBOSE, 0);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/'.MAIL_VIA_DOMAIN.'/messages');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArr);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result = curl_exec($ch);
curl_close($ch);
mail is being sent to my mail inbox but i am not receiving attachments. looking at the logs also it says attachments empty

how to delete photo post id facebook by grab api php with curl

i try to delete photos i have uploaded with app by php
the result of upload example :
{"id":"429774393794352","post_id":"276744849097308_429774413794350"}
first which one id or post_id will be use
and how to select it with php ??
and how to make order to delete it with php code by curl library
my idea is , i will store all photos ids into sql to delete it any time
my upload code i used
$file='./'.$new_string;
$args = array(
'message' => $message,
);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/album id /photos?access_token='.$accsesss;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$a1=$data = curl_exec($ch);
print_r(json_decode($data,true));
echo'<br/>';
i try
$args = array(
'id' => '276744849097308_429774413794350',
'access_token' => $accsesss ,
);
$ch = curl_init();
$url = 'https://graph.facebook.com/276744849097308_429774413794350?method=DELETE&access_token=$accsesss';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true); $a1=$data = curl_exec($ch);
print_r(json_decode($data,true));
echo'<br/>';
result
Array ( [error] => Array ( [message] => Invalid OAuth access token. [type] => OAuthException [code] => 190 ) )
$Curl_Session = curl_init('https://graph.facebook.com/429774393794352');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "method=DELETE&access_token=$masteracsess");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, 1);
echo $a8=curl_exec ($Curl_Session);
curl_close ($Curl_Session);
thanks for your help #CBroe

Categories