Can I use this part to send mails?
So if I add some php code in action.php, is this sufficient for a working email script (apart from the php code)?
This is the code, (and it's in a function don't worry this is not everything on the page)
And I know there is not a subject field, but I'll just enter the name (voornaam).
var voornaam = $('#voornaam').val();
var achternaam = $('#achternaam').val();
var telefoonnummer = $('#telefoonnummer').val();
var email = $('#email').val();
$.post('action.php',{action: "button", voornaam:voornaam, achternaam:achternaam, telefoonnummer:telefoonnummer, email:email},function(res){
$('#result').html(res);
});
document.getElementById('goed').innerHTML = 'Verstuurd!';
PHP support send mail function if the web server have correct setting,
try to research for this.
And you can use plugin like PHPmailer too.
The following is a simple example to send html email use mail function.
Just put the code into action.php will be OK.
<?php
$voornaam = $_POST['voornaam'];
$achternaam = $_POST['achternaam'];
$telefoonnummer = $_POST['telefoonnummer'];
$email = $_POST['email'];
$subject = 'Sample mail';
$headers = 'MIME-Version: 1.0' ."\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$content = '<html><header><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </header><body>';
$content .= $voornaam;
$content .= $achternaam;
$content .= $telefoonnummer;
$content .= '</body></html>';
mail($email, $subject, $content, $headers); //This method sends the mail.
echo "Your email was sent!"; // success message
?>
Related
I am working on a website where you can provide contact details and submit it to a PHP page. These contact details will be sent to an email address using the native PHP mail() function. However, everything seems fine and works with the following code:
<?php
$salutation = $_GET["salutation"];
$name = $_GET["name"];
$plan = $_GET["plan"];
$desc = $_GET["desc"];
$to = "example#email.com";
$subject = $desc;
$msg = "
<html>
<body>
<p>Test body</p>
</body>
</html>
";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $msg, $headers);
?>
When you submit the form, the end of the URL bar changes to:
page.php?salutation=dhr&name=Kees+van+Voorthuizen&plan=basic&desc=Test+Description
As I said, this works properly and no errors were thrown.
But when I add an <input name="email" id="email" type="email"> to the contact details page inside the <form> tag and I submit the form using my own email address, the server returns an HTTP error 503. PHP code using the email input field:
<?php
$salutation = $_GET["salutation"];
$name = $_GET["name"];
$email = $_GET["email"]; // <--- The only thing that has changed
$plan = $_GET["plan"];
$desc = $_GET["desc"];
$to = "example#email.com";
$subject = $desc;
$msg = "
<html>
<body>
<p>Test body</p>
</body>
</html>
";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $msg, $headers);
?>
The URL now ends in:
page.php?salutation=dhr&name=Kees+van+Voorthuizen&email=my%40email.com&plan=basic&desc=Test+Description
I suspect that the error is being thrown when I include a period ('.') inside the email field. I've tried manually changing the URL bar using my%40emailcom instead of my%40email.com and it worked. Notice that I removed the period between email and com.
Any ideas?
I am new to JQuery and havent used AJAX at all. I have searched the web for days with no complete answer.
Im trying to have a form be emailed via AJAX while JQuery hides the form and displays additional data. The JQuery portions works as the pages switches to display the proper data but the form information doesnt get emailed.
JQuery/AJAX
$(document).ready(function() {
//When the form is submitted...
$('form').on('submit',function(e) {
//Send the serialized data to mailer.php.
$.ajax({
url:'mailer.php',
data:$(this).serialize(),
type:'POST',
success:function(data){
console.log(data);
$("#success").show().fadeOut(5000); //=== Show Success Message==
},
error:function(data){
$("#error").show().fadeOut(5000); //===Show Error Message====
}
});
e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
//$.post("mailer.php");
//Take our response, and replace whatever is in the "form2"
//div with it.
$('#form1').hide();
$('#form2').show();
});
});
PHP Submit file mailer.php
<?php
//Grab Posted Data
$fname = strip_tags(htmlentities($_POST['fname']));
$lname = strip_tags(htmlentities($_POST['lname']));
$name = $fname." ".$lname;
$email = strip_tags($_POST['email']);
$phone = strip_tags(htmlentities($_POST['phone']));
$address = strip_tags(htmlentities($_POST['address']));
$city = strip_tags(htmlentities($_POST['city']));
$state = strip_tags(htmlentities($_POST['state']));
$zip = strip_tags(htmlentities($_POST['zip']));
$country = strip_tags(htmlentities($_POST['country']));
$message = strip_tags(htmlentities($_POST['goals']));
// PREPARE THE BODY OF THE MESSAGE
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>$name</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>$email</td></tr>";
$message .= "<tr><td><strong>Phone:</strong> </td><td>$phone</td></tr>";
$message .= "<tr><td><strong>Address:</strong> </td><td>$address</td></tr>";
$message .= "<tr><td> </td><td>$city, $state $zip $country</td></tr>";
$message .= "<tr><td><strong>Goals:</strong> </td><td>$message</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
// CHANGE THE BELOW VARIABLES TO YOUR NEEDS
$to = 'me#me.com';
$subject = 'Website Change Reqest';
$headers = "From: $email \r\n";
$headers .= "Reply-To: $email \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (mail($to, $subject, $message, $headers)) {
echo 'Your message has been sent.';
} else {
echo 'There was a problem sending the email.';
}
?>
PHP mail() uses sendmail system, and most of the problems happens when it is not set up properly. Sometimes webhosters disable it outright.
Sometimes, i solved this problem by using an external library, PhpMailer, for example, and SMTP with a real mail account.
On a side note - never ever do this:
$fname = strip_tags(htmlentities($_POST['fname']));
$lname = strip_tags(htmlentities($_POST['lname']));
$name = $fname." ".$lname;
$email = strip_tags($_POST['email']);
$phone = strip_tags(htmlentities($_POST['phone']));
$address = strip_tags(htmlentities($_POST['address']));
$city = strip_tags(htmlentities($_POST['city']));
$state = strip_tags(htmlentities($_POST['state']));
$zip = strip_tags(htmlentities($_POST['zip']));
$country = strip_tags(htmlentities($_POST['country']));
$message = strip_tags(htmlentities($_POST['goals']));
It can be easily replaced by something like:
extract(
array_map(
function($elem) {
return strip_tags(html_entities($elem));
}, $_POST)
);
I've already got a problem like this. The issue was not in javascript code, but in php's mail() function.
Happened that nowadays most mail servers do not support unauthenticated e-mail to be sent. See that in order to use mail() function you never provided an username or password to log in a mail server.
My issue was solved using the PHPMailer class (available at http://phpmailer.worxware.com), in witch I provide mailserver's address, account username and password. Thought it is quite easy to create an HTML e-mail with attachments without worry about writing long header codes.
Did you use Denwer for test this example?
Mail content:
<html><head></head><body><p> Message : 鏽嫌╒杜米土 杯屎</p>Share Link : Press here to enter <br><img src ='http://203.80.1.28/FlippingBook/development/demo/medium/Web081112_P070_medium.jpg' /></body></html>
PHP:
if (isset($_POST["data"])){
$info = explode("&", $_POST["data"]);
$headers = 'MIME-Version: 1.0\r\n';
$headers .= "Content-Type: text/html; charset = \"UTF-8\";\n";
$headers = "From: =?UTF-8?B?" . base64_encode(substr($info[0],strpos($info[0],'=')+1, strlen($info[0]))) . "?=";
$to = substr($info[1],strpos($info[1],'=')+1, strlen($info[1]));
$subject = "=?UTF-8?B?" . base64_encode('日報分享') . "?=";
$message = trim(substr($info[2],strpos($info[2],'=')+1, strlen($info[2])));
$message = '<html><head></head><body><p> Message : '.$message;
$url = substr($info[3],strpos($info[3],'=')+1, strlen($info[3]));
$message = $message. '</p>Share Link : Press here to enter ';
if (isset($info[4])){
$firstImg = substr($info[4],strpos($info[4],'=')+1, strlen($info[4]));
$message = $message."<br><img src ='".$firstImg."' />";
}
if (isset($info[5])){
$secondImg = substr($info[5],strpos($info[5],'=')+1, strlen($info[5]));
$message = $message."<br><img src ='".$secondImg."' />";
}
$message = $message.'</body></html>';
if (mail($to, $subject, $message, $headers))
die ('Mail sent');
else
die ('Fail');
}else{
die ('Fail');
}
I am writing a simple program to send email. However, my mail content is not english based so I used utf-8 to encode.
When I changed the encode method, it can not send the processed html code, instead the mail content is the raw html code shown above, how to fix this problem?
If using PEAR lib is not a problem than you can look for Pear Mail-Mime lib to send mail or HTML contents. More details you can get from here - http://pear.php.net/manual/en/package.mail.mail-mime.example.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);
I am trying to send my website visitors and email with some directions and tips before they show up to my studio via PHP form mailer. ( i am simplifying some of the form fields )
However the HTML formatting is not working correctly.... did i not declare the mime type of charset correctly?
<?php
if (isset($_POST['submit'])) {
//if (empty ($_POST['name']) || empty($_POST['email']))
//{
//echo"<div class='error'>Error<br />You did not fill in a required field, please review your form and correct the missing information. <a class='close' href='#'>close</a></div>";
//}
$name = $_POST['name'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
//A bunch of other fields are here
//Additional Headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//$yoursite = "My Site";
//$youremail = $email;
$subject = "Website Form";
$message = "
$name would like you to contact them about your service.
Contact PH: $phone
Email: $email
Legal Guardian: $legal
//more stuff here
";
$subject2 = "Directions and Information";
$message2 = "<html><head></head><body>
$message2 .= "<h1>Directions</h1>
<p>text</p>
<p><a href='http://example.com/schedules'>Click here</a>
<h2>How Do I find your Photo Studio?</h2>
<h2>What do I have to bring with me?</h2>
</p>";
$message2 .= "</body></html>";
$email3 = "me#mysite.com";
$email4 = "mysite#gmail.com";
//This email sends their details to me from the visitor
mail($email3, $subject, $message, "From: $email");
//This email sends directions to the visitor from me
mail($email, $subject2, $message2, "From: $email4");
echo"<div class='thankyou'>Thank you for contacting us,<br /> we will respond as soon as we can.</div>";
}
?>
There is a lot of random junk that needs to be done to an email to send correctly. I generally tend to outsource all that responsibility to a pre-packaged class that exists in the wild, something like http://swiftmailer.org/
Maybe someone else would have a better class to offer.
I swear by the PEAR Mail_Mime package. It's simple and powerful.
PEAR: Mail_Mime
//Basic mail headers
$headers['To'] = "test#domain.com";
$headers['From'] = "sender#domain.com";
$headers['Subject'] = "Test";
//Set up the mail module
$mime = new Mail_mime("\r\n");
$mime->setTXTBody("This is a test");
$mime->setHTMLBody("<p>This is a test.</p>");
$body = $mime->get();
$headers = $mime->headers($headers);
//Send the message via SMTP
$mail_obj =& Mail::factory('smtp', array('host' => 'mail.domain.com', 'port' => 25));
$mail_obj->send($headers['To'], $headers, $body);