Use Sendgrid to send email with attachment from a form - php

I am trying to send an attachment from a form to an email, using the Sendgrid web api.
However, I have no clue on how to edit the following code in order to make use of the input file of the form.
Any help please?
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => 'the recipient email',
'subject' => 'New Careers',
'html' => $email,
'text' => $email,
'from' => 'the sender email',
);
$request = $url.'api/mail.send.json';
$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);

This solution may help you.
if (function_exists('curl_file_create')) { // php 5.5+
$cFile = curl_file_create($file_name_with_full_path);
} else { //
$cFile = '#' . realpath($file_name_with_full_path);
}
$post = array('extra_info' => '123456','file_contents'=> $cFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
Here is Reference URL

Related

error_code 129 in VK api appWidgets.saveAppImage

Good day! I'm trying to load an image using the method appWidgets.saveAppImage. In the beginning I receive URL the server for loading -> getAppImageUploadServer, there all ok!
I receive a hash and an image, send them a POST request and get an error. Here is my code:
$token = "Service_access_key";
$tmp_image = file_get_contents('https://www.ejin.ru/wp-content/uploads/2017/12/667108931864_667108931864-150x150.jpg');
file_put_contents(dirname(__FILE__).'/tmp.jpg',$tmp_image);
$img_path = dirname(__FILE__).'/tmp.jpg';
$post_data = array("image" => "#".$img_path);
$upload_url = file_get_contents("https://api.vk.com/method/appWidgets.getAppImageUploadServer?v=5.85&image_type=50x50&access_token=".$token);
$url = json_decode($upload_url)->response->upload_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = json_decode(curl_exec($ch),true);
$safe = file_get_contents("https://api.vk.com/method/appWidgets.saveAppImage?v=5.85&hash=".$result['hash']."&image=".$result['image']."&access_token=".$token);
echo $safe;
echo:
"error_code":129,"error_msg":"Invalid photo: file not found, from upl_850128?act=app_widget_image"
what's my mistake?
<?
$request_params = array(
'image_type' => '510x128',
'access_token' => 'xxx',
'v' => '5.92'
);
$t = json_decode(file_get_contents('https://api.vk.com/method/appWidgets.getAppImageUploadServer?'. http_build_query($request_params)));
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch, CURLOPT_URL, $t->response->upload_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("image" => new CURLFile(dirname(__FILE__).'\img.jpg')));
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$result = json_decode(curl_exec($ch));
curl_close($ch);
$request_params = array(
'hash' => $result->hash,
'image' => $result->image,
'access_token' => 'xxx',
'v' => '5.92'
);
print_r(file_get_contents('https://api.vk.com/method/appWidgets.saveAppImage?'. http_build_query($request_params)));
?>

MailChimp API subscribe in PHP

I'm incredibly rusty but trying to change the email product used on a website. I've been reading through some previously answered questions and have the below code.
The page is progressing, adding the user to the site database but not the MailChimp list. Where am I falling down?
$apikey = 'api key-us16';
$auth = base64_encode( 'user:'.$apikey );
$data = array(
'apikey' => $apikey,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $firstname
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://us16.api.mailchimp.com/3.0/lists/mylistid/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);

Mailgun send mail with attachment

I am trying to send a mail with attachments with mailgun.
The mail itself is fine, but it is missing the attachment.
Also in the mailgun log it shows up fine, but the attachment array is empty.
I replaced my credentials with example.com.
The file is placed in a subdirectory and is readable.
$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <example#mail.example.com>',
'to' => 'example#example.com',
'subject' => 'Subject',
'text' => 'This is just a test.',
'attachment[1]' => '#' . $filename);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/mail.example.com/messages');
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-ThisIsJustARandomString');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
I don't get an error, this is the $response:
string(103) "{
"id": "<20170514122638.8820.55203.4543B111#mail.example.com>",
"message": "Queued. Thank you."
}"
Within the mailgun logs no attachments are listed:
"message": {
"headers": {
"to": "example#example.com",
"message-id": "20170514122638.8820.55203.4543B111#mail.example.com",
"from": "Example <example#mail.example.com>",
"subject": "Subject"
},
"attachments": [],
"size": 349
},
According to all documentation I found this would be the correct solution, but it is not working.
Thanks in advance for all replies.
Change your first code to:
$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <example#mail.example.com>',
'to' => 'example#example.com',
'subject' => 'Subject',
'text' => 'This is just a test.',
'attachment[1]' => curl_file_create($filename, 'application/pdf', 'example.pdf'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/mail.example.com/messages');
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-ThisIsJustARandomString');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
I changed '#'.$filename to curl_file_create($filename, 'application/pdf', 'example.pdf').
See documentation curl_file_create and check the notes for PHP < 5.5.
or use the api https://documentation.mailgun.com/en/latest/api-sending.html#examples
once the domains and parameters are set you can then just use $result = $mgClient->messages()->send( $domain, $params );
This worked for me.
<?php
$path = $filename;
define('MAILGUN_URL', 'https://api.mailgun.net/v3/domainname');
define('MAILGUN_KEY', 'private api key from mail gun');
**function sendmailbymailgun**($to,$toname,$mailfromname,$mailfrom,$subject,
$html,$text,$tag,$replyto, $path){
$array_data = array(
'from'=> $mailfromname .'<'.$mailfrom.'>',
'to'=>$toname.'<'.$to.'>',
'subject'=>$subject,
'html'=>$html,
'text'=>$text,
'o:tracking'=>'yes',
'o:tracking-clicks'=>'yes',
'o:tracking-opens'=>'yes',
'o:tag'=>$tag,
'h:Reply-To'=>$replyto,
'attachment[0]' => curl_file_create(__dir__."\\".$path, 'application/pdf', $path),
'attachment[1]' => curl_file_create(__dir__."\\".$path, 'application/pdf', "example.pdf")
);
$session = curl_init(MAILGUN_URL.'/messages');
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_USERPWD, 'api:'.MAILGUN_KEY);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $array_data);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($session);
curl_close($session);
$results = json_decode($response, true);
return $results;
}
//: call the function
$res = sendmailbymailgun("example#yahoo.com","Recipeint Name", "Sender Name", "sender#example.com","Email subject","Email body. find two attachment","","tags", "no-reply#example.com", $path);
echo "<pre>".print_r($res, true)."</pre>";
?>
$dest = "filepath/test.pdf";
$result = $mgClient->messages()->send($domain,
array('from' => 'Tester <test#test.com>',
'to' => 'test#test02.com',
'subject' => 'Testing',
'html' => '<h1>Normal Testing</h1>',
'attachment' => array(
array(
'filePath' => $dest,
'filename' => 'test.pdf'
)
)
));
This code is worked properly for me.
This works for me. I have passed the attachment file URL in the attachment array. now I can send multiple attachments in email.
$attachment = [ 0 =>
'https://www.crm.truerater.com/public/assets/client_upload_images/1634327873.png',
1 => 'https://www.crm.truerater.com/public/assets/client_upload_images/1634327873.png'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/truerater.com/messages');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'from' => $mailfromname .'<'.$mailfrom.'>',
'to' => $toname.'<'.$to.'>',
'cc' => '',
'bcc' => '',
'subject' => $subject,
'html'=>$html,
'text'=>$text,
'o:tracking'=>'yes',
'o:tracking-clicks'=>'yes',
'o:tracking-opens'=>'yes',
'o:tag'=>$tag,
'h:Reply-To'=>$replyto,
);
if(sizeOf($attachment) > 0){
$i=0;
foreach ($attachment as $attach){
$attachPath = substr($attach, strrpos($attach, '/public/') + 1);
$post['attachment['.$i.']'] = curl_file_create($attach, '', substr($attachPath, strrpos($attachPath, '/') + 1));
$i=$i+1;
}
}
$headers_arr = array("Content-Type:multipart/form-data");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERPWD, 'api' . ':' . $key);
curl_setopt($ch, CURLOPT_HEADER, $headers_arr);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result = curl_error($ch);
\Log::info($result);
}
else{
$result = json_decode($result,true);
}
curl_close($ch);
\Log::info($result);
return $result;

PHP CURL not sending attachment in an email

I am using mailgun to send email, the attachment doesn't goes through email, works
function send_mail($email,$subject,$msg) {
$api_key="key-532cf742c1";/* Api Key got from https://mailgun.com/cp/my_account */
$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/test.com/messages');
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'from' => 'Open <test#gmail.com>',
'to' => $email,
'subject' => $subject,
'html' => $msg,
'attachment'=>"/home/public_html/cms/upload/quiz.jpg"
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

multiple facebook posting using curl

i need to post multiple message to facebook wall from the mysql database. first i fetch the data from mysql and put it in while loop
while($row=mysql_fetch_array($result))
{
$des=$row[1];
$purpose=$row[3];
$price_sale=$row[4];
$price_rent=$row[5];
$img="example.com/images".mysql_result($result,0,2);
$attachment = array(
'access_token' => "$token",
'message' => $des,
'picture' => $img,
'link' => "example.com"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/xxxxxxxxxxx/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
echo $result;
}
the $result contain 3 records. But only post the first row. Plz give a solution for this
Try changing the name of the variable that accepts the curl output.You are using the same variable above.
while($row=mysql_fetch_array($result))
{
$des=$row[1];
$purpose=$row[3];
$price_sale=$row[4];
$price_rent=$row[5];
$img="example.com/images".mysql_result($result,0,2);
$attachment = array(
'access_token' => "$token",
'message' => $des,
'picture' => $img,
'link' => "example.com"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/xxxxxxxxxxx/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$curlresult = curl_exec($ch);
curl_close ($ch);
echo $curlresult;
}

Categories