I am trying to make a contact form for a static website hosted in an Azure blob.
When I click submit I can see the following error in the Console.
Failed to load resource: the server responded with a status of 405
(The resource doesn't support specified Http Verb.)
I think the issue may be that I need to set up CORS for mailgun.
However I don't know what values to put
Here is send.php code
<?php
if( isset($_POST) ){
$postData = $_POST;
$mailgun = sendMailgun($postData);
if($mailgun) {
echo "Great success.";
} else {
echo "Mailgun did not connect properly.";
}
}
function sendMailgun($data) {
$api_key = 'INSERT_API_KEY_HERE';
$api_domain = 'INSERT_DOMAIN_HERE';
$send_to = 'YOUR_EMAIL';
// sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');
// form data
$postcontent = $data['data'];
$reply = $data['senderAddress'];
$messageBody = "<p>You have received a new message from the contact form on your website.</p>
{$postcontent}
<p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";
$config = array();
$config['api_key'] = $api_key;
$config['api_url'] = 'https://api.mailgun.net/v3/'.$api_domain.'/messages';
$message = array();
$message['from'] = $reply;
$message['to'] = $send_to;
$message['h:Reply-To'] = $reply;
$message['subject'] = "New Message from your Mailgun Contact Form";
$message['html'] = $messageBody;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $config['api_url']);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:{$config['api_key']}");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS,$message);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
?>
This question is a follow up from this question
I have tried including the subdomain www in the api_domain.
PHP does not run client side which means it is not suitable for a static website. As mentioned in this question
Alternatives are mentioned here
Related
I have been using reCaptcha V2 for awhile now and all of a sudden the validation is timing out on the file_get_contents line.
I can copy the URL from the error and paste it into a new window and the JSON object comes back immediately.
$url = 'https://www.google.com/recaptcha/api/siteverify?secret='. urlencode($secret) .'&response='. urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
I fixed this timeout problem with the File_get_contents by changing to using $curl instead.
Like this:
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secret) . '&response=' . urlencode($captcha);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
// file get contents not used any more
//$response = file_get_contents($url,0,stream_context_create(["http"=>["timeout"=>120]]) );
$responseKeys = json_decode($response,true);
// should return JSON with success as true
I have the following code written out and the email sends fine and all but I have not been able to figure out why the email does not include the intended attachment (the pdf file).
I have also tried $mail->attach with MIME and that gives a 500 internal server error.
$pdf = public_path() . '/assets/attachments/TermSheet.pdf';
$mail = new PHPMailer\PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPSecure = '';
$mail->SMTPAuth = true;
$mail->IsHTML(true);
//ENV VARIABLES
$mail->Host = env("MAIL_HOST");
$mail->Port = env("MAIL_PORT");
$mail->Username = env("MAIL_USERNAME");
$mail->Password = env("MAIL_PASSWORD");
$mail->SetFrom($fromemail, $fromname);
$mail->Subject = $emailsubject;
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $emailsubject;
$mail->addAttachment($pdf);
$mail->Body = $initbody;
$mail->AddAddress($user);
if(isset($ccemaillist)){ //cc
$ccemail = explode(";",$ccemaillist);
foreach ($ccemail as $toccmail) {
$mail->AddAddress($toccmail);
}
}
if($mail->Send()) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $currentapp.'/mail/send?api_user_id='.$creatorid);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"recipient_email=$user&email_type=WelcomeEmail&sys_status=1&creator=$creatorid");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
return "SUC";
}
I'm not sure why it doesn't work, any input in this I would appreciate it. Thank you in advance :)
It may give
500 internal server error
Due to incorrect file paths.
You may try like
$pdf = asset('assets/attachments/TermSheet.pdf');
public_path() does not work in some cases.
Can i use the first script or i need to use the curl option for mailchimp 3.0? I read some posts that the first one may be depreciated.. is that correct? Note that i'm not running on WordPress. Thank you for your fast answers.
<?php
require("vendor/autoload.php");
use \DrewM\MailChimp\MailChimp;
$mc = new MailChimp('apikey');
$email = $_POST['email'];
$subscriber_hash = $mc->subscriberHash($email);
$response = [];
$list_id = 'listid';
$resp = $mc->get("/lists/$list_id/members/$subscriber_hash";
if ($mc->success()) {
$response['message'] = 'Thank you for subscribing to the mailing list';
// User successfully subscribed - set HTTP status code to 200
http_response_code(200);
} else {
$response['message'] = $mc->getLastError();
// User not subscribed - set HTTP status code to 400
http_response_code(400);
}
// Return json-formatted response
echo json_encode($response);
?>
Or should i use this one?
function mc_checklist($email, $debug, $apikey, $listid, $server) {
$userid = md5($email);
$auth = base64_encode( 'user:'. $apikey );
$data = array(
'apikey' => $apikey,
'email_address' => $email
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'.api.mailchimp.com/3.0/lists/'.$listid.'/members/' . $userid);
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_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
if ($debug) {
var_dump($result);
}
$json = json_decode($result);
echo $json->{'status'};
}
This row is only for stackoverflow not alowing me to post that much code without including more details.
The first is not deprecated. It uses this wrapper to make API calls using the same API version that the second block of code uses. It's just simpler to work with so you don't have to write separate CURL requests for every call. You can also take a look at some of its source code and notice that it uses CURL anyway to make its calls.
So either way will use CURL, and whichever option you choose is simply a matter of preference.
Hopefully that clears it up for you!
I have two domain one is stgportal and another is stg.I want to upload image from stgportal to stg.My code is given below
move_uploaded_file($_FILES['ThumbnailImage']['tmp_name'],"http://stg.eminencesystem.com/assets/images/th/".$thimg)
show the error: failed to open stream: HTTP wrapper does not support writeable connections
how can i resolve this issue?
You can use CURL to send file to another server securely like this
At stgportal side
function sendImageToServer($path = "", $file = null)
{
$post_url = "http://stg.eminencesystem.com/post_file";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $post_url);
//most importent curl assues #filed as file field
$post_array = array(
"folder_name" => $path
);
if ((version_compare(PHP_VERSION, '5.5') >= 0)) {
$post_array['file'] = new CURLFile($file);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, TRUE);
} else {
$post_array['file'] = "#" . $file;
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
$response = curl_exec($ch);
return $response;
}
At stg side
$ImagePath = "/var/www/html/assets/";
$files = $_FILES;
$folder_name = utf8_decode($_POST['folder_name']);
$savepath = $ImagePath . $folder_name . $files['name'];
copy($file['tmp_name'], $savepath);
echo 1;
exit;
I need to monitor some ftp servers for any changes in file structure, things that I need to monitor is how many times an file is downloaded (not sure if possible), if files are changed or not, if files are deleted or not, if ftp server still exists,
i would like this to be something that i can run server=side and would like a sms message or email if any of the above changes have occured
any one have any experience or would recommend an particular language or script?
thanks
for php i use this simple script
<
?
ini_set('auto_detect_line_endings', true);
error_reporting(E_ALL);
set_time_limit(0);
ini_set('show_errors', 1);
$file = "ftpfile.txt";
$handle = fopen($file, "r");
$date = new DateTime();
$date = $date->setTimezone(new DateTimeZone('America/New_York'));
$date = $date->format('U = Y-m-d H:i:s') . "\n";
if(!filesize($file)>0) {
echo "File is empty!";
}
else {
while (($ftpservers = fgets($handle)) !== false) {
//echo $ftpservers. "<br>";
$ftpserver= explode ("<br>", $ftpservers );
//$pattern = "//";
//preg_match ($pattern, $ftpservers, $output);
foreach ($ftpserver as $ftpserverurl) {
$ch = curl_init(); //this part we set up curl
curl_setopt($ch, CURLOPT_URL,$ftpserverurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,40);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
if(curl_errno($ch))
{
sleep(2); //add sleep in order to not overload hosting server
$echooutput = $date. 'Curl error: ' . curl_error($ch). 'Server Malfunctioned:' .$ftpserverurl;
error_log("$echooutput", 3, "ftperrors.log"); //write error to log
error_log("$echooutput", 1, "your email address"); //email alert
curl_close ($ch);
}
else
{
error_log("$date curl operation is sucessful \n", 3, "curl.log"); //keep track of successful connections for pattern anaylsis
curl_close ($ch);
}
}
}
//if (!feof($handle)) {
//echo "Error: unexpected fgets() fail\n";
//}
fclose($handle);
}
?>