fsockopen demo at localhost - fsockopen

Can anybody help me to sort out fsockopen issue in localhost.
I created fsock.php to post a variable to test121.php in the same folder.
http://localhost/ftp/fsock.php
<?php
$fp = fsockopen('localhost/ftp');
$vars = array(
'hello' => 'world'
);
$content = http_build_query($vars);
fwrite($fp, "POST /test121.php HTTP/1.1\r\n");
fwrite($fp, "Host: localhost/ftp \r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");
fwrite($fp, $content);
header('Content-type: text/plain');
while (!feof($fp)) {
echo fgets($fp, 1024);
}?>
This was the code in test121.php
http://localhost/ftp/test121.php
<?php
echo "<br><br>";
$raw_data = $GLOBALS['HTTP_RAW_POST_DATA'];
parse_str( $raw_data, $_POST );
//test 1
var_dump($raw_data);
echo "<br><br>";
//test 2
print_r( $_POST );
?>
I am getting Failed to parse address error in fsock.php.
Can anybody explain me how to sortout this issue in localhost.
Thank you in advance.

fsock.php
$fp = fsockopen("localhost", 80, $errno, $errstr, 30);
$vars = array(
'hello' => 'world'
);
$content = http_build_query($vars);
fwrite($fp, "POST http://localhost/ftp/test121.php HTTP/1.1\r\n");
fwrite($fp, "Host: localhost \r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");
fwrite($fp, $content);
header('Content-type: text/plain');
while (!feof($fp)) {
echo fgets($fp, 1024);
}?>
And you might also want to change test121.php and remove $GLOBALS (Not sure why you using this):
<?php
echo "<br><br>";
$raw_data = $_POST;
//test 1
var_dump($raw_data);
echo "<br><br>";
//test 2
print_r( $_POST );
?>

Related

Laravel Ajax does not get the value from the form to Controller

In Ajax code I have action = 'contact';, contact is used in route file:
Route::post('contact', array('uses' => 'FormsController#send', 'as' => 'post_form'));
In route file I have FormsController#send it is file php to send email:
$name = Input::get('name');
$getSubject = "Subject of my email";
$myEmail = 'myEmail#gmail.com';
$uid = md5(uniqid(time()));
$eol = "\r\n";
// header
$header = "From: " . $name . " <" . $email . ">\r\n";
$header .= "MIME-Version: 1.0" . $eol;
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"" . $eol;
$header .= "Content-Transfer-Encoding: 7bit" . $eol;
$header .= "This is a MIME encoded message." . $eol;
// message & attachment
$nmessage = "--" . $uid . "\r\n";
$nmessage .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$nmessage .= "Content-Transfer-Encoding: 8bit" . $eol;
if($name != ''){$nmessage .= "Name: " . $name . "\r\n\r\n";}
// $nmessage .= "Wiadomość:\r\n" . $getMessage . "\r\n\r\n";
$nmessage .= "--" . $uid . "\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "--" . $uid . "--";
$send = mail($myEmail, $getSubject, $nmessage, $header);
Ajax directs to the controller file and bypasses the form, so the controller file does not download any data from the form, and the mail can not be sent. I have no idea how to pass data from the form to the controller file.
My Ajax:
const sendForm = function () {
action = 'contact';
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open('post', action, true);
xmlhttp.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
const getMessageSend = document.querySelector("#messageSend");
getMessageSend.innerText = "Thank you for sending an email. You will receive an answer shortly.";
} else {
const getMessageSendError = document.querySelector("#messageSendError");
getMessageSendError.innerText = "An error occurred and the email was not sent.";
}
};
// xmlhttp.open("post", action, true);
// xmlhttp.send();
const token = document.querySelector('meta[name="csrf-token"]').content;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
xmlhttp.setRequestHeader('X-CSRF-TOKEN', token);
xmlhttp.send();
};
const sendMail = function() {
options.form.addEventListener('submit', function (e) {
e.preventDefault();
let validate = true;
const elementsRequired = document.querySelectorAll(":scope [formHr]");
[].forEach.call(elementsRequired, function(element) {
const type = element.type.toUpperCase();
if (type === 'TEXT') {
if (!validateText(element)) {validate = false;}
}
});
if (validate) {
sendForm();
// this.submit();
} else {
return false;
}
});
};
My form:
{!! Form::open(['action'=>['FormsController#send'], 'method' => 'post', 'class' => 'form', 'novalidate' => 'novalidate', 'files' => true]) !!}
<input type="text" name="name" placeholder="Name" formHr>
{!! Form::submit('Send', ['class' => 'submit-btn']); !!}
{!! Form::close() !!}
I found a solution. Help me this theme Submit form laravel using AJAX
I just add data to my code:
$.ajax({
type: "POST",
url: 'contact',
data: $(this).serialize(),
success: function () {
$("#messageSend").addClass("message-send");
$("#messageSend").text("Thank you for sending an email. You will receive an answer shortly.");
}
});
Remember to use full version Jquery!

How to Send email with html templates in laravel 5?

i want to try simple email sent like in php but i get templates based email sending in Laravel 5 ??
`$to = Session::get('email');
$subject = 'Order confirmation';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: Test <rajdipvekriya1992#gmail.com>";
$message = 'test body';
mail($to, $subject, $message, $headers);`
but i want get templates based body html pass $message like to
$body = $this->load-
>view('admin/email_template/test_template',$data,TRUE);
$this->email->message($body);
$this->email->send();
Use Mail::send like this
\Mail::send('view', $data, function ($message)
{
$message->subject('Email Subject');
$message->from('acb#example.com');
$message->to('xyz#example.com');
});
and create view.blade.php, write in laravel blade.
create a Mailable class with make command
php artisan make:mail
then write your code in handle method
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('emails.complaint-reply')
->subject('Cotint Group')
->with(['complaint'=>$this->complaint]);
}
And in emails.complaint-reply.blade.php file write down your HTML template
I think Mail::raw is what you need:
Mail::raw('Text to e-mail', function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
});
https://laravel.com/docs/5.0/mail
use Illuminate\Support\Facades\Mail;
Mail::send('email.relatorlead', ['data' => $message], function ($m) use ($message) {
$m->from('no-reply#twostructureshomes.com', 'Two Structures Homes');
$m->to('nikunj#whitelabeliq.com');
$m->subject('Realtor Registration Lead From ' . $message['firstname']);
});
have a look to this code, this will surely help you.
<?php
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);
?>

wp_mail not working in my ajax function - functions.php file

WP_MAIL functionality is not working in my ajax function in functions.php file.
Please have an look on the code and help me out !!
Do i need to load any files for the working of wp_mail function ??
function et_contact_form() { ?>
<script type="text/javascript" >
jQuery('#contact_modal').on('submit', function (e) {
e.preventDefault();
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
var name = jQuery("#name").val();
var data = {
'action':'et_contact_modal',
'name' : name
};
jQuery.post(ajaxurl, data, function(response) {
alert(response);
});
});
</script> <?php
}
add_action( 'wp_footer', 'et_contact_form' );
function et_contact_modal() {
global $wpdb;
$headers .= "Reply-To: test#gmail.com \r\n";
//$headers .= "CC: test#gmail.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = 'New Enquiry From ';
$message .= '<p>' . $_POST['name'] . '</p>';
$message .= '<p></p>';
$mailResult = false;
$mailResult = wp_mail( 'test#gmail.com',$subject,$message, $headers );
echo $mailResult;
}
add_action( 'wp_ajax_et_contact_modal', 'et_contact_modal' );
Sorry people's!!
The code was correct , it was just server issue !!
Thank you.

PHPmailer - After Form submitted the Inbox message displays HTML numeric code

The problem is with this PHP code that goes through an AJAX function and sends the content of a Form to a specific e-mail.
The essential part of the script is working, but some text appears to be converted in the Inbox when the e-mail arrives. Specifically, single-quotes (') are replaced with double-quotes (").
After searching the web and browsing SO, I couldn't find an answer. Any help would be deeply appreciated.
The code follows.
<?php
require '../config/squareconfig.php';
if($_POST)
{
//check if ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
//check $_POST vars are set, exit if any missingc
if(!isset($_POST["usercName"]) || !isset($_POST["usercEmail"]) || !isset($_POST["usercMessage"]))
{
die();
}
//Sanitize input data using PHP filter_var().
$user_cName = filter_var($_POST["usercName"], FILTER_SANITIZE_STRING);
$user_cEmail = filter_var($_POST["usercEmail"], FILTER_SANITIZE_EMAIL);
$user_cMessage = filter_var($_POST["usercMessage"], FILTER_SANITIZE_STRING);
if(strlen($user_cName)<4) // If length is less than 4 it will throw an HTTP error.
{
header('HTTP/1.1 500 Name is too short or empty!');
exit();
}
if(!filter_var($user_cEmail, FILTER_VALIDATE_EMAIL)) //email validation
{
header('HTTP/1.1 500 Please enter a valid email!');
exit();
}
if(strlen($user_cMessage)<5) //check emtpy message
{
header('HTTP/1.1 500 Too short message! Please enter something.');
exit();
}
//proceed with PHP email.
$headers = 'From: '.$user_cEmail.' ';
$templatemail = PHP_EOL . PHP_EOL . 'User E-mail:' . PHP_EOL . $user_cEmail;
#$sentMail = mail($squarecmail, $squarecsubject, $user_cMessage . PHP_EOL . $templatemail, $user_cName);
if(!$sentMail)
{
header('HTTP/1.1 500 Could not send mail! Sorry..');
exit();
}else{
echo $squarectymessage . '<br>' . '<div class="subagain">' . $squarecontagain . '</div>';
?>
<script type="text/javascript">
$('#contact_form').fadeOut();
</script>
<?php
}
}
I'm uncertain about the "problem", but a reasonable suggestion might be to send the e-mail as HTML, since filter_var() returns html-entities for non-alphanumeric characters. Specify the content type for the e-mail message.
$headers = "From: yomama#jokes.com\r\n";
$headers .= "Reply-To: arenot#always.funny\r\n";
$headers .= "CC: yomamasobig#whenshewearsayellowcoatppleyell.taxi\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$mail_sent = #mail($sender, $subject, $message, $headers);
The above suggestion is based on the fact that FILTER_SANITIZE_STRING causes html-unicode-encoding (or some such) on the mentioned characters.
$ php-shell
PHP-Shell - Version 0.3.1, with readline() support
(c) 2006, Jan Kneschke <jan#kneschke.de>
>> echo filter_var('\' -- "', FILTER_SANITIZE_STRING);
' -- "

PDF attachment shows up in Gmail but Outlook refuses to open it

I'm relatively new to php.
I wrote a script that sends an email with a pdf attachment to a user and a confirmation to a co-worker. Everything works fine in web-based email clients like gmail but the pdf attachment does not open when it is received in Outlook. It also adds another attachment called "ATT0159.txt" which is completely blank.
Here's the code:
<?php
$pdf = $_GET['pdf'];
$product = $_GET['product'];
$username = $_POST['Name']; #get name
$useraddress = $_POST['Email']; #get user email address
$subjectUser = $product . " PDF brochure from Specifile on-line";
$to = "specifile#gmail.com"; # recipient
$subject = "Confirmation: PDF Request - " . $product; #subject
$message = "The following person has requested the brochure - " . $product . "\n<br/>" . $username . " [ " . $useraddress . " ] "; #message
$attachment = $_SERVER['DOCUMENT_ROOT'] . "\\" . $pdf;
$attachment_type = "application/pdf";
$attachment_name = "brochure.pdf";
#open, read, then close the file
$fp = fopen( $attachment, 'rb');
$file = fread($fp, filesize($attachment));
fclose($fp);
#create boundary string
$num = md5(time());
$str = "==Multipart_Boundary_x{$num}x";
#encode data for safe transit
$file = chunk_split(base64_encode($file));
#define user header
$headers = "MIME-version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;\r\n";
$headers .= " boundary=\"{$str}\"\r\n";
$headers .= "From: Specifile on-line<specifile#infixion.co.za> \r\n";
#define confirmation header
$Confirmheaders = "MIME-version: 1.0\r\n";
$Confirmheaders .= "Content-type: text/html;";
$Confirmheaders .= " charset=\"UTF-8\"\r\n";
$Confirmheaders .= "From: Specifile on-line<specifile#infixion.co.za> \r\n";
#create message for user
$messageUser = "This is a multi-part message in MIME format\r\n";
$messageUser .= "--{$str}\r\n";
$messageUser .= "Content-type: text/html; charset=\"UTF-8\"\r\n";
$messageUser .= "Content-Transfer-Encoding: 8bit\r\n";
$messageUser .= "Hi " . $username . ", <p>The brochure you requested is attatched.</p> \r\n\n";
$messageUser .= "--{$str}\r\n";
#define non text attachment
$messageUser .= "Content-Type: {$attachment_type}; ";
$messageUser .= "name=\"{$attachment_name}\"\r\n";
$messageUser .= "Content-Disposition: attachment; ";
$messageUser .= "filename=\"{$attachment_name}\"\r\n";
$messageUser .= "Content-Transfer-Encoding: base64\r\n";
$messageUser .= "$file\r\n\n";
$messageUser .= "--{$str}";
mail($useraddress,$subjectUser,$messageUser,$headers);
mail($to,$subject,$message,$Confirmheaders);
?>
Not a real answer to your question, but have you considered using a ready-made mailing class like SwiftMailer? It does building the multipart message, and adding the attachments, for you.

Resources