I'm not sure how to send form over email [duplicate] - javascript

This question already has answers here:
How can I send an email using PHP?
(20 answers)
Closed 7 years ago.
At the bottom of my site there is a "send message" button. I want it to take the message and contact information(email, name) and send it to my email address. How could I possibly do this? I'm new at this website stuff by the way.

You can use PHP mail function.
<?php
if (!empty($_POST)) {
$name= $_POST['name'];
$mail_id= $_POST['email_id'];
$email= '';//email address on which you want to receive website details
$message_field = $_POST['message'];
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Return-Path: \r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header= "From: TriaaHousing";
$message="Name:".$name."\r\n";
$message .=" ";
$message .= "EmailId:".$mail_id."\r\n";
$message .=" ";
$message .= "Message:".$message_field;
if(mail($email, "Subject", $message, $header)){
echo 1;
}else{
echo 0;
}
}
?>

Use can use default PHP's mail() functionality OR you can use PHPMailer (Mail send helper). Both are safe and correct. but if you need some other stuff then use PHPMailer.
1. Using PHP's mail() function it's possible. Remember mail function will not work in Local server.
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: from#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
NOTE: You need to config SMTP on your local server if you are using SMTP. Take a look at this similar post.
2. You could also use PHPMailer class at https://github.com/PHPMailer/PHPMailer .
It allows you to use the mail function or use an smtp server transparently. It also handles HTML based emails and attachments so you don't have to write your own implementation.
Here is an example from the page above:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('webmaster#example.com', 'Webmaster User'); // Add a recipient
$mail->addAddress('webmaster#example.com'); // Name is optional example
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Use addReplyTo, addCC, addBCC if require.
Hope this help you well!

Related

Contact form does not work for me with special characters inlcuding UTF-8 [duplicate]

When I send out the email, the email does not show characters other than english.
It does show like below:
余生ä»ä»
May know actually what cause this?
Even I tried to added Content-type and charset in the script, it still show the same.
I used Mail::Factory("mail");
You can add header "Content-Type: text/html; charset=UTF-8" to your message body.
$headers = "Content-Type: text/html; charset=UTF-8";
If you use native mail() function $headers array will be the 4th parameter
mail($to, $subject, $message, $headers)
If you user PEAR Mail::factory() code will be:
$smtp = Mail::factory('smtp', $params);
$mail = $smtp->send($to, $headers, $body);
I'm using rather specified charset (ISO-8859-2) because not every mail system (for example: http://10minutemail.com) can read UTF-8 mails. If you need this:
function utf8_to_latin2($str)
{
return iconv ( 'utf-8', 'ISO-8859-2' , $str );
}
function my_mail($to,$s,$text,$form, $reply)
{
mail($to,utf8_to_latin2($s),utf8_to_latin2($text),
"From: $form\r\n".
"Reply-To: $reply\r\n".
"X-Mailer: PHP/" . phpversion());
}
I have made another mailer function, because apple device could not read well the previous version.
function utf8mail($to,$s,$body,$from_name="x",$from_a = "info#x.com", $reply="info#x.com")
{
$s= "=?utf-8?b?".base64_encode($s)."?=";
$headers = "MIME-Version: 1.0\r\n";
$headers.= "From: =?utf-8?b?".base64_encode($from_name)."?= <".$from_a.">\r\n";
$headers.= "Content-Type: text/plain;charset=utf-8\r\n";
$headers.= "Reply-To: $reply\r\n";
$headers.= "X-Mailer: PHP/" . phpversion();
mail($to, $s, $body, $headers);
}
If not HTML, then UTF-8 is not recommended. koi8-r and windows-1251 only without problems. So use html mail.
$headers['Content-Type']='text/html; charset=UTF-8';
$body='<html><head><meta charset="UTF-8"><title>ESP Notufy - ESP Сообщения</title></head><body>'.$text.'</body></html>';
$mail_object=& Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail_object->send($recipents, $headers, $body);
}

Facebook javascript api - webhook

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

user enters name before quiz starts using javascript

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.

sending mail in php is not working [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am trying to send an email from php. The mail sends successfully but is never delivered. The code is below..
if(isset($_POST["Referred"])) {
if(isset($_POST["RequestedEmail"])) {
$messagebody="You are invited to join Fasttask. please click on the link below \n http://www.fasttask.net/EmailRequestRedirect.php?User_Id=".$_SESSION['user_id'];
$To=$_POST["RequestedEmail"];
if(mail($To,"Join Fasttask", $messagebody)) {
$query="Update monthlygiveaway set TotalReferrals=TotalReferrals+1 where UserId=?";
$stmt=$mysqli->stmt_init();
$stmt->prepare($query);
$stmt->bind_param('i', $UserId);
$UserId=$_SESSION['user_id'];
$stmt->execute();
echo"Successfully sent mail:".$To;
} else {
echo "Failed to send mail";echo $_SESSION['user_id'];
}
}
}
Everything goes fine.. and the echo message is delivered to callin php function. I checked so the $To variable does have the address of the recipient. So please help me to trace the issue. :)
Have you try header parameter? may be it works.
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
For more detail http://php.net/manual/en/function.mail.php

form not redirecting or sending email

Having an issue with my mailer script. When an email is sent it doesn't redirect to my chosen page it sits on mail.php which os the logic file. Having some issues understanding what the top section is asking me for too.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<?php
require 'c:\php\includes\PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.website.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'my#website.com'; // SMTP username
$mail->Password = '******'; // Enable encryption, 'ssl' also accepted
$mail->Port = 25;
$mail->From = 'http://iamdanbarrett.com';
$mail->FromName = 'Contact Form';
$mail->addAddress('iamdanbarrett.com'); // Name is optional
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'General Enquiries';
$mail->Body = "<html><body>Name: " . $_POST["Name"] . "<br><br>Email: " . $_POST["Email"] . "<br><br>Message: " . $_POST["Message"];
// $mail->Body = 'Details below:<br><hr><br><br><b>Shortlist:</b><br>' . $_POST["strShortlist"] . '<br><br><b>Company:</b> ' . $_POST["strCompany"] . '<br><br><b>Full Name:</b> ' . $_POST["strFullname"] . '<br><br><b>Email:</b> ' . $_POST["strEmail"] . '<br><br><b>Contact No:</b> ' . $_POST["strContactNo"] . '<br><br><b>No of People:</b> ' . $_POST["strNoOfPeople"] . '<br><br><b>Start Date:</b> ' . $_POST["strDate"] . '<br><br><b>Requirements:</b> ' . $_POST["strRequirements"] . '<br><br><b>Other:</b> ' . $_POST["strOther"] . '<br><br><b>Salesperson:</b> ' . $_POST["strSalesperson"];
if(!$mail->send()) {
echo '';
echo '';
exit;
} else {
}
?>
</head>
<body>
<script>
$( document ).ready(function() {
window.location.href = "http://iamdanbarrett.com/;
});
</script>
Why do you have to bring Javascript in here ? Use the PHP's header() instead.
if(!$mail->send()) {
die("Mail was not sent!");
} else {
header("location:http://iamdanbarrett.com/");
exit;
}
You can use php header function. Put below code in your else part.
header("Location:http://iamdanbarrett.com/");exit;

Categories