I have created a quiz but would like to know how to start when making a user input to email.
Basically I would like the user to enter their name before they start my quiz and then once they have finished the quiz the results get sent to me by email using Javascript.
Is this possible?
You can't send email with plain JavaScript as far as I know, you will need to have a service, backend or a php file..
for example this php file:
<?php
header('Content-type: application/json');
$errors = '';
if(empty($errors))
{
$from_name = $_POST['name'];
$from_email = $_POST['email'];
$message = $_POST['message'];
$to_email = 'your#email.com';
$to_email_cc = $from_email;
$contact = "<p><strong>Name:</strong> $from_name</p>
<p><strong>Email:</strong> $from_email</p>";
$content = "<p>$message</p>";
$email_subject = "Neue Nachricht von $from_name erhalten";
$email_subject_cc = "Deine Nachricht wurde gesendet";
$email_body = '<html><body>';
$email_body .= "$contact $content";
$email_body .= '</body></html>';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $my_email\n";
$headers .= "Reply-To: $from_email";
mail($to_email,$email_subject,$email_body,$headers);
mail($to_email_cc,$email_subject_cc,$email_body,$headers);
$response_array['status'] = 'success';
echo json_encode($response_array);
} else {
$response_array['status'] = 'error';
echo json_encode($response_array);
}
?>
and then you could do a post on that with for example angular, jquery or other..
something like this:
postEmail(newMail: Email): Observable<string>{
let body = `name=${newMail.name}&email=${newMail.email}&message=${newMail.message}`;
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });
return this._http.post(this._contactUrl, body, options)
//.map(res => <string> res.json())
.catch(this.handleError)
}
this is some old code from angular2 i wrote once and is not tested, but it should give you an idea of how to do it.
Related
i got a few variables on my site that i push on GTM they work perfect, now i need to get variables results from gtm and send it via ajax to a php page where afterwards i'll use it to an email with the results.
<script>
$(document).ready( function() {
var msg = google_tag_manager["GTM-xxxxx"].dataLayer.get('pageName');
var msg1 = google_tag_manager["GTM-xx"].dataLayer.get('MembershipLvl');
var msg2 = google_tag_manager["GTM-xx"].dataLayer.get('CountryRes');
var msg3 = google_tag_manager["GTM-xxxx"].dataLayer.get('pageLanguage');
var getall = [msg, msg1, msg2, msg3,];
// var me = 'just seend'; this do work
console.log(getall);
$.ajax({
type: "GET",
url: "url.cart.php", // some php
data:{info: getall},
datatype: 'html',
success: function(data) {}
});
});
and here is my php
<?php
/*
*
*/
send_mail();
function send_mail(){
if( $_GET["info"] ) {
$mail = $_GET["info"];
$header ="From: no-reply#test.com" . "\r\n";
$for = 'end#emailadaress.com';
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$title = 'Mailing list Newsletter';
$message = '<html><body>';
$message .= '<br/> <p>tou got email from</p><br/>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="2">';
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($mail)."</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
if(mail($for, $title, $message, $header)){
die();
}else{
//echo "false";
}
}else{
echo "false";
}
}
?>
the issue is that im not getting GTM variables sent, i do see them in console log, if i change variable like var = test, i do get it, but i just cant get the GTM variable sent
So the problem is that $_GET["info"] contains an array which strip_tags cannot handle. If you look at the response from your call it should have a PHP error. Somewhere along the lines of : Warning: strip_tags() expects parameter 1 to be string, array given. If you want to get the values out of the array you can either build a loop or reference the particular index.
LOOP
foreach ($mail as $gtm) {
$message .= $gtm;
}
INDEX
$message .= $mail[0] // google_tag_manager["GTM-xxxxx"].dataLayer.get('pageName');
$message .= $mail[1] // google_tag_manager["GTM-xx"].dataLayer.get('MembershipLvl');
etc...
PHP strip tags
Foreach loop
How to view a ajax call in chrome
i am having a problem to do a webhook with the facebook api on a lead ads form.
Last day it was working like a charm. On the 'Lead Ads Testing Tool' i received a status = success. Now i received a status = failure, error code = 102, error message = server failure
image of the error 102
Anyone know this issue ?
here my webhook.php :
if (isset($_GET['hub_verify_token'])) {
if ($_GET['hub_verify_token'] === 'your verify token') {
echo $_GET['hub_challenge'];
return;
} else {
echo 'Invalid Verify Token';
return;
}
}
$input = json_decode(file_get_contents('php://input'), true);
$emailfrom = "test#test.com";
$emailto = "my#email.com";
$subject = "You've got a new submission Test";
$headers = "From: " . $emailfrom . "\r";
$headers .= "Reply-To: ". $emailfrom . "\r";
$headers .= "MIME-Version: 1.0\r";
$headers .= "Content-Type: text/html; charset=utf-8\r";
$body = 'hello';
mail($emailto, $subject, $body, $headers);
and the callback_url is good = "success": true image graph api subscription to webhook
Thanks
I've encountered a problem. I wanted to send a mail with PHPMailer but I passed the values from the form via jQuery AJAX. But when the mail is sent I get an email without the info that I've passed via AJAX.
Here is my JS code:
$.ajax({
url: post_url,
type: request,
data: {
ime: ucenikOdabrao.ime,
email: ucenikOdabrao.email,
broj: ucenikOdabrao.broj,
grad: ucenikOdabrao.grad,
pol: ucenikOdabrao.pol,
studij: ucenikOdabrao.studij,
faks: ucenikOdabrao.faks,
univerziteti: ucenikOdabrao.univerziteti,
dokumenti: ucenikOdabrao.dokumenti
},
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false,
dataType: "json"
}).done(function(res){
if(res.type == "error"){
alert(res.text);
}
if(res.type == "done"){
alert(res.text);
}
})
I've printed out the object ucenikOdabrao and it printed out in my console perfectly with all the data being there.
Here is my PHP code:
require "PHPMailer-master/PHPMailerAutoload.php";
header('Content-Type: application/json');
$ime = $_POST["ime"];
$email = $_POST["email"];
$broj = $_POST["broj"];
$grad = $_POST["grad"];
$pol = $_POST["pol"];
$studij = $_POST["studij"];
$faks = $_POST["faks"];
$univerziteti = $_POST["univerziteti"];
$dokumenti = $_FILES["dokumenti"];
$dokumenti_count = count($dokumenti['name']);
$subject = "Aplikacija za faks - ".$ime;
$body = "Prijava za faks - ".$ime."\n";
$body .= "Email: ".$email."\n";
$body .= "Broj: ".$broj."\n";
$body .= "Grad: ".$grad."\n";
$body .= "Pol: ".$pol."\n";
$body .= "Studij: ".$studij."\n";
$body .= "Faks na koji aplicira: ".$faks."\n";
$body .= "Univerziteti na koje aplicira: ".$univerziteti."\n";
for($x = 0; $x < $dokumenti_count; $x++){
if(!empty($dokumenti['name'][$x])){
if($dokumenti['error'][$x]>0){
print json_encode("Došlo je do greške! Molimo pokušajte ponovo!");
exit;
}
$file_name = $dokumenti['name'][$x];
$file_size = $dokumenti['size'][$x];
$file_type = $dokumenti['type'][$x];
// Čitamo fajl bajo moj
$handle = fopen($dokumenti['tmp_name'][$x], "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
}
}
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->isHTML(true);
$mail->Username = "cccc#gmail.com";
$mail->Password = "passs";
$mail->SetFrom("cccc#gmail.com");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress("dddd#gmail.com");
if(!$mail->Send()){
print json_encode(array('type'=>'error', 'text' => 'Došlo je do greške. Molimo pokušajte kasnije!'));
exit;
} else{
print json_encode(array('type'=>'done', 'text' => 'Vaša aplikacija je uspješno poslata!'));
exit;
}
I also have a problem that I can't get anything as a response in jQuery AJAX function. I just can't see where the problem is :/
I am attempting to send the collected value's of a html form as an email to myself via PHP & Ajax. For some reason, I am able to update the UI with a success alert, however there's no actual email sent when I check my inbox. I am under the impression that my PHP script may be ill-structured, because when I log the results of my js function, all of the form values have been correctly captured.
Here is the JS:
function _(id){ return document.getElementById(id); };
function submitForm(){
var formdata = new FormData();
formdata.append( "first-name", _("first-name").value );
formdata.append( "last-name", _("last-name").value );
formdata.append( "email", _("email").value );
formdata.append( "subject", _("subject").value );
formdata.append( "message", _("message").value );
var ajax = new XMLHttpRequest();
ajax.open( "POST", "email_me.php" );
ajax.onreadystatechange = function() {
if(ajax.readyState == 4 && ajax.status == 200) {
if(ajax.responseText == "success"){
alert("Hey! It Worked!!");
} else {
// display error
}
}
}
ajax.send( formdata );
// Display the key/value pairs
for (var pair of formdata.entries()) {
console.log(pair[0]+ ', ' + pair[1]);
}
}
And Here is the php script (email_me.php file)
<?php
if(isset($_POST['first-name'], $_POST['last-name'], $_POST['email'], $_POST['subject'], $_POST['message'])){
$name = $_POST['first-name'];
$email = $_POST['email'];
$m = nl2br($_POST['message']);
$to = "me#gmail.com";
$from = $email;
$subject = $_POST['subject'];
$message = '<p>'.$m.'</p>';
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
if( mail($to, $subject, $message, $headers) ){
echo "success";
} else {
echo "The server failed to send the message. Please try again later.";
}
}
?>
What seem's to be the issue? Im running the current version of Apache with a localhost, using MAMP Pro btw.
Here are the server logs:
Marker - Aug 23, 2016, 12:34:32 PM
Aug 23 12:35:24 MacBookAir postfix/master[7884]: daemon started -- version 2.11.0, configuration /etc/postfix
Aug 23 12:36:24 MacBookAir postfix/master[7884]: master exit time has arrived
Aug 23 12:36:24 MacBookAir postfix/master[7885]: daemon started -- version 2.11.0, configuration /etc/postfix
Aug 23 12:37:24 MacBookAir postfix/master[7885]: master exit time has arrived
Aug 23 12:37:24 MacBookAir r postfix/master[7886]: daemon started -- version 2.11.0, configuration /etc/postfix
update you php code and check it will work
if(isset($_POST['first-name'], $_POST['last-name'], $_POST['email'], $_POST['subject'], $_POST['message'])){
$name = $_POST['first-name'];
$email = $_POST['email'];
$m = nl2br($_POST['message']);
$message = '<p>Name => '.$name.' <br/> Email =>'.$email.'<br /> Message =>'.$m.'</p>';
$to = "me#gmail.com";
$subject = $_POST['subject'];
$headers = "From: noreply#something.com\r\n"; // use \r\n
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; // use \r\n
$headers.= "X-Priority: 1\r\n";
if( mail($to, $subject, $message, $headers) ){
echo "success";
} else {
echo "The server failed to send the message. Please try again later.";
}
}
I have been struggling to send uploaded file to email using php. My problem is whatever i give the email uploaded file send to that email. Here is my code it is not working.
<?php
if (isset($_POST['submit'])) {
$headers = "From: abc#example.com mailer \r\n";
echo $message .= $_POST['message'];
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
if (file_exists($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
$to_user = "bnarendra6036#gmail.com";
$flgchk = mail("$to_user", "$subject", "$message", "$headers");
if ($flgchk) {
echo "Done, <br>";
}
else
{
echo "Failed to send email, <br>";
echo "Redirecting you to your Inbox";
}
}
?>
<form name="form1" method="post" action="" enctype="multipart/form-data">
<table>
<tr>
<td width='20'><b>Select File:</b> </td>
<td width='20'><input type="file" name="attachment"></td>
</tr>
<p><td>
<input type="submit" name="Submit" value="Send" class="Submit" /></td></tr></table>
</p>
</form>
So can you please adjust this code or send me any another working code.
I have created demo using PHPMailer. First load phpmailer.class.php file.
You can download from here : http://github.com/PHPMailer/PHPMailer
require_once('/class.phpmailer.php'); // Your full path of phpmailer.php file
$email = new PHPMailer();
$email->From = 'From email address'; //From email address
$email->FromName = 'Your name'; //your name
$email->Subject = 'Message'; //Message
$email->Body = 'Body Text Message'; //Body message
$email->AddAddress( 'To email address' ); //To email address
$attach_file = 'Your file path'; //Attached file path
$email->AddAttachment( $attach_file, 'file_name.doc' );
return $email->Send();