jQuery/PHP Send email on mousedown link event - php

Here my code. I could not display errors message. Simply mail is not sent. What's wrong with this approach ?
<a id="target" href="http://www.mylink.wav">mylink</a>
<script type="text/javascript">
$(function() {
$("#target").mousedown(function() {
$.post(
"sendemail.php",
{ name: "John" }
);
});
});
</script>
<?php
if($_POST)
{
$mail = "myemail.email.com"
$name = $_POST['name'];
$subject = 'My subject';
$to = "myemail#email.com";
$message = "My message: ".$name."<br />";
$headers = "From: $mail \n";
$headers .= "Reply-To: $mail \n";
$headers .= "MIME-Version: 1.0 \n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \n";
}
?>
Thank you for your suggestions to solve this issue.

Firstly check if you actually are calling your script inside jQuery block.
Second, you need to include php mail() function (if enabled on server):
mail($to, $subject, $message, $headers);
http://php.net/manual/en/function.mail.php

Related

Jquery Ajax Email Sending File Instead Of Message

I have a simple contact form that I want to submit to an email.
Everything is working fine except that instead of the input values being sent in the body of the email it's sending a file.
I tried changing the content-type of my php file but the only thing that changes is the format of the file that's sent, .html or .txt.
It's my first time working with ajax or PHP, so what am I doing wrong here?
I tried inserting the html tags on the message $body and tried without them as well, same thing happens.
Here's my jquery
$("#enviar").click(function(){
$.ajax({
dataType:'html',
url:"./scripts/email.php",
type:"POST",
data:({nome:$("input[name='name']").val(),email:$("input[name='email']").val(),telefone:$("input[name='phone']").val()}),
beforeSend: function(data){
}, success:function(data){
alert("Dados Enviados");
}, complete: function(data){}
});
});
And here's the PHP
<?php
if(isset($_POST['email']) && !empty($_POST['email'])){
$nome = strip_tags($_POST['nome']);
$email = strip_tags($_POST['email']);
$telefone = strip_tags($_POST['telefone']);
$headers = "MIME-Version: 1.1\n";
$headers .= "Content-type: text/html: charset=UTF-8\n";
$headers .= "From: mexample#example.com\n";
$headers .= "Return-Path: example#example.com\n";
$headers .= "Reply-To: ".$email."\n";
$emailsender = "example#example.com";
$to = "receiver email";
$subject = "Formulario - Contato";
$body = "<html>
<head>
<title> Formulario </title>
</head>
<body>".
"<p>". "Nome: " .strip_tags($nome). "</p>".
"<p>". "Email: " .strip_tags($email). "</p>".
"<p>"."Telefone: " .strip_tags($telefone). "</p>".
"</body>
</html>";
if(!mail($to, $subject, $body, $headers ,"-r".$emailsender)){
$headers .="Return-Path: " . $emailsender . "\n";
mail($to, $subject, $body, $headers);
}
}
?>

Php contact form keeps going to spam and has an html attachement

I experiencing a problem with a php contact form that I normal use on our products sites.
<?php
$to = "martin#puritech.co.za";
$subject = "Reverse Osmosis";
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['tel'];
$mail = $_POST['message'];
$headers = "From: $name \r\n";
$headers .= "Email: $email \r\n";
$headers .= "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html charset=iso-8859-1 \r\n";
$message = "<html><body style='font-family: Arial;'>";
$message .= "<h1>Hello Guys! You have a request from ".$name."</h1>";
$message .= "<p>".$mail."</p>";
$message .= "<p><span style='font-weight:bold;'>From:</span> ".$name."</p>";
$message .= "<span style='font-weight:bold;'>Email:</span> ".$email."</p>";
$message .= "<span style='font-weight:bold;'>Contact Number:</span> ".$number."</p>";
$message .= "</body></html>";
$send = mail ($to, $subject, $message, $headers);
if ($send==true) {
header('Location: ../index.html');
exit;
}else {?>
<script type="text/javascript">
alert('Please check your details and resend. Thank you.');
window.location = '../index.html';
</script>
<?php
}
?>
When I click on the send button, the mail goes straight to spam and has an html doc attachment.
How do I fix this? really draining me and its starting to annoy me.

sending mail from php when link in message body

i have a php script
<?php
$to = 'somebody#somedomain.com';
$subject = 'Test mail';
$message = 'mysitedomain.com';
$from = 'support#mysitedomain.com';
$headers = 'From:' . $from;
mail($to,$subject,$message,$headers);
echo 'Mail Sent.';
?>
When i run this code mail not send. If i change message to mysitedomaincom (without dot before com) the mail send succesfull.
Anybody have a solution for this?
This codes that tells the mailer and the recipient that the email contains well formed HTML that it will need to interpret
If you want you can change content of $message, now with this codes you can send HTML content mail.
<?PHP
$to = 'somebody#somedomain.com';
$subject = 'Test mail';
$headers = "From: Support <support#mysitedomain.com>" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h1>mysitedomain.com</h1>';
$message .= '</body></html>';
mail($to, $subject, $message, $headers);
?>

PHP Mail function not working (jQuery/php)

I have a little problem with the following code. When I click "Send" it says "PHP Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:***.com**\mail.php on line 21"
This is the jQuery:
$(document).ready(function(){
$("#send").click(function(){
var valid = '';
var isr = ' requested.</h6>';
var name = $("#name").val();
var mail = $("#email").val();
var messaggio = $("#message").val();
if (name.length<1) {
valid += '<h6>A valid name is'+isr;
}
if (!mail.match(/^([a-z0-9._-]+#[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
valid += '<h6>A valid e-mail address is'+isr;
}
if (valid!='') {
$("#re").fadeIn("slow");
$("#re").html("<h6><b>Error:</b></h6>"+valid);
$("#re").css("background-color","#ffc0c0");
}
else {
var datastr ='name=' + name + '&mail=' + mail + '&messaggio=' + encodeURIComponent(messaggio);
$("#re").css("display", "block");
$("#re").css("background-color","#FFFFA0");
$("#re").html("<h6>Sending..</h6>");
$("#re").fadeIn("slow");
setTimeout("send('"+datastr+"')",2000);
}
return false;
});
});
function send(datastr){
$.ajax({
type: "POST",
url: "mail.php",
data: datastr,
cache: false,
success: function(html){
$("#re").fadeIn("slow");
$("#re").html(html);
$("#re").css("background-color","#e1ffc0");
setTimeout('$("#re").fadeOut("slow")',2000);
}
});
}
And the PHP
<?php
$mail = $_POST['mail'];
$name = $_POST['name'];
$text = $_POST['messaggio'];
$ip = $_SERVER['REMOTE_ADDR'];
$to = "admin#****.com";
$message = "E-mail received from: ".$name.", ".$mail.".<br />";
$message .= "Messaggio: <br />".$text."<br /><br />";
$message .= "IP: ".$ip."<br />";
$headers = "From: $mail \n";
$headers .= "Reply-To: $mail \n";
$headers .= "MIME-Version: 1.0 \n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \n";
if(mail($to, $message, $headers)){
echo "<h6>Message sent.</h6>";
}
else{
echo "<h6>Ops! We've got a problem! :(</h6>";
}
?>
The mentioned line is "if(mail($to, $message, $headers)){" but I don't understand the error.
I am using the same code on another website (another domain also) and it runs without problems.
Headers need to be terminated with \r\n not \n.
$headers = "From: $mail \r\n";
$headers .= "Reply-To: $mail \r\n";
$headers .= "MIME-Version: 1.0 \r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \r\n";
Also there is a Header Injection vulnerability in your code. You should validate all of the post data that you put into email headers.
Have you checked the SMTP Settings in PHP.INI?
Set this in your code or in your php.ini:
<?php
ini_set("SMTP","smtp-server-address" );
ini_set('sendmail_from', 'from-email-address');
?>
And if you are sending through a different port than 25, you can also override the default:
ini_set('smtp_port', '2525');
You might try replacing your FROM headers with:
$headers = "From: $name <$mail>\r\n";
designating the email address within lt/gt brackets.
One other thought is to be sure the $_POST['mail'] value is indeed being passed.
I think you might have a carriage return coming from your field. You need to scrub your data input in the $_POST like:
$mail = trim($_POST['mail']);
You might also want to use mysql_real_escape_string or something similar in regexp if inputting into a DB.
Also for consistantcy in your code:
$headers = "From: ".$mail." \r\n";
$headers .= "Reply-To: ".$mail." \r\n";
Also I think your missing the correct mail parameters. According to the mail documentation your missing a subject parameter:
mail($to, $subject, $message, $headers);

PHP Contact Form not Sending?

Ive got a contact form that isnt sending but is outputting that the message is sent? Can anybody see a problem?
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "myemail#email.co.uk";
//begin of HTML message
$message = "
From : $name,
Email: $email,
Subject: $subject,
Message: $message ";
//end of message
// To send the HTML mail we need to set the Content-type header.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Website Enquiry";
if (isset($_POST['name'])) {
// now lets send the email.
mail($to, $subject, $message, $headers);
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=Thankyou, we will be in touch shortly.');
} else {
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=There was an error sending your message, Please try again.');
}
?>
The "From" header should have a syntactically correct email address. You also need to check the return value of the "mail" function.
$header .= "From: Website Enquiry <enquiry#website.com>";
PS: Please improve your code formatting.
Try to enter an email at From: in $headers.
Like $headers .= "From: youremail#provider.com" or
$headers .= "From: Website Enquiry <youremail#provider.com>"
And you should change it to
if(mail(...)) {
//success
}
else {
//email failed
}

Categories