PHP CURL not sending attachment in an email - php

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;
}

Related

Json received = false

I am using ci framework, and i try to create notification using
onesignal API. I have hosted the website using https. But why my JSON
received is always false using onesignal API, but if i test using
postman it runs fine without error.
This is my code
function send_message($id, $nama) {
$content = array(
"en" => $nama
);
$heading =array(
"en" => $id
);
$fields = array(
'app_id' => "2cd5ad24-a2d9-4e7d-ac66-7494cebd8e84",
'included_segments' => array(
'Active Users'
),
'contents' => $content,
'headings' => $heading,
'url' => "https://esop.matahariled.com"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic MDQ0NjY0ZmYtZjQ2Yi00ODVmLTkzZjgtZmVkZDBkODk0MDFl'
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_CAINFO, $_SERVER['DOCUMENT_ROOT']."/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}

Use Sendgrid to send email with attachment from a form

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

Getting "bool(false)" from MailChimp API 3 POST request

All,
I'm trying to do a simple "post" to Mailchimp using the 3.0 API; however I'm just getting a bool(false) response from the below code. I know the MAILCHIMP_API_KEY and LIST_ID variables are correct... Help?
All I want to do is add an email & first name to a specific list.
$auth = base64_encode( 'user:'.MAILCHIMP_API_KEY);
$data = array(
'apikey' => MAILCHIMP_API_KEY,
'email' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $name
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/lists/'.NEW_LIST_ID.'/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_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
var_dump($result);
die('<br /><br />Mailchimp executed');
Just missed the dot after $server in the URL, and also should be email_address rather than email. But below is a working example if anyone needs it:
$auth = base64_encode( 'user:'.MAILCHIMP_API_KEY);
$data = array(
'apikey' => MAILCHIMP_API_KEY,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $name
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'.api.mailchimp.com/3.0/lists/'.NEW_LIST_ID.'/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_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
die('<br /><br />Mailchimp executed');

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;

Categories