I wrote code to send emails from PHP using HTML and JavaScript (jQuery). In js I defined the following strings:
var firstT = "It is " + $("#time").html() + " right now.";
var secondT = "And now, it is " + $("#othTime").html() + ".";
var sendT = firstT + "\r\n" + secondT;
Then I send this string to the PHP file:
var thTi = "folder/time.php?to=" + $("#perName").val() + "&message=" + sendT;
$.ajax({
url: thTi
});
PHP receives this strings and sends it with this code:
<?php
$to = $_GET['to'];
$subject = "The Subject";
$message = $_GET['message'];
$headers = 'From: Me?' . "\r\n" .
'Reply-To: no-reply#thesite.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
So, the problem is - when a person receives the message, it is all in one line. I've added line breaks \r\n, even tried with <br> and <br />. I found somewhere that inserting a dot . might solve the problem, but I've tried, and it doesn't.
To send an html email with mail(), you have to add the proper headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: Me?' . "\r\n";
$headers .= 'Reply-To: no-reply#thesite.com' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
To clean and add line-breaks to your html:
$message = strip_tags($_GET['message'];);
$message_html = preg_replace('/\n/' , '<br />' , $message);
Use the double quotes " instead of ' and then \r\n will work. So your new code must looks like:
$headers = "From: Me?" . "\r\n" .
"Reply-To: no-reply#thesite.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
Since you are using an html tages in your mail first thing that you need to do is to set an header type of that mail to html. Append the below lines to the code to the $header variable at the start, before adding the from and other details in this variable.
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Me?' . "\r\n" .
'Reply-To: no-reply#thesite.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
for more understanding of this you can see this link: http://www.w3schools.com/php/func_mail_mail.asp
I hope this helps you.
Related
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);
}
I want to redirect the user to another page only after displaying a success message. The code below redirects to the another page even if the page is refreshed without submitting any data.
Code Snippet
<?php
// We will store our status message later
$message = '';
// Let's check form submitted
if( isset( $_POST['action'] ) && $_POST['action'] == 'submit' ) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['number'];
$message_text = $_POST['address'];
if (isset($_POST['action']))
{
// Validate required fields
if( $name == '' || $email == '' || $message_text == '' ){
$message = "<p class=\"error\">Please fill out all required fields.</p>";
}
// send email after validation
else {
// Email will be sent at this email
$mailto = 'nepstarditsolutions#gmail.com';
// Let's create html email format
// (You can use html in this email format)
// Email headers
$headers = "From: " . strip_tags( $email ) . "\r\n";
$headers .= "Reply-To: ". strip_tags( $email ) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// Email body
$message = "<html><body>";
$message .= "<h3>Contact Info: </h3>";
$message .= "<p><strong>Name: </strong> ". strip_tags( $name ) . "</p>\r\n";
$message .= "<p><strong>Email: </strong> ". strip_tags( $email ) . "</p>\r\n";
$message .= "<p><strong>Contact Number: </strong> ". strip_tags( $phone ) . "</p>\r\n";
$message .= "<p><strong>Alternate Email: </strong> ". strip_tags( $_POST['altemail'] ) . "</p>\r\n";
$message .= "<p><strong>Hosting Package: </strong> ". strip_tags( $_POST['cradio'] ) . "</p>\r\n";
$message .= "<p><strong>Address: </strong>". strip_tags( $message_text ) . "</p>\r\n";
$message .= "</body></html>";
// Email subject
$subject = "Contact Info from ".$name;
// Send email
$mail_status = #mail( $mailto, $subject, $message, $headers );
if( $mail_status ){
$message = '<div class="alert alert-success">Quotation submitted Successfully ! </div>';
}
else {
$message = '<div class="alert alert-danger">Error in sending the Quotation ! </div>';
}
}
}
}
?>
I need help in 2 things
Success message should be displayed for 2 seconds (before the redirect).
How to redirect using JS in php?
Standard "vanilla" JavaScript way to redirect a page
You can use the code below to redirect the user after form submission code.
if( $mail_status ){
echo "
<script>
setTimeout(function() {
window.location = 'http://www.example.com/newlocation';
}, 2000);
</script>
";
}
MDN web docs on how setTimeout works
Hope it helps!
$id = $_POST['id'];
$to="codieboi#mail.com";
$msg1="done";
$email_subject = "View Cateloge Request";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'X-MSMail-Priority: High'. "\r\n";
$headers = 'From: '.$_POST['email']."\r\n".
'X-Mailer: PHP/' . phpversion();
$email_message .= "Name: ".$_POST['fname']."\n";
$email_message .= "Email: ".$_POST['email']."\n";
$email_message .= "Phone: ".$_POST['phone']."\n";
$email_message .= "Phone: ".$_POST['message']."\n";
#mail($to, $email_subject, $email_message, $headers);
$link = the_field('upload_file', $id);
echo '<script type="text/javascript">var url="_________";var win = window.open(url, "_blank");win.focus();</script>';
I store a url dynamically to the variable "link". I want to open up it in a new tab, once the mail is send. This is the code I have written. I need to place url in the _____________.
Please help.
If you store url to variable link, just use it:
echo "<script type='text/javascript'>var url='". $link."';var win = window.open(url, '_blank');win.focus();</script>";
On a Linux server, I have an ajax form that gets posted to a js script, which in turn posts to a php routine for sending a web site email contact form (Styleshout-Puremedia template). I know the post is occurring to js, and to the php page. I get an httpd server error on the php page that "$Error is undefined". However if I define it as NULL or '' it exists and therefore doesn't pass the test "if (!$Error)". If I create a hard-coded php script using the mail function, the e-mail works so php and sendmail are all configured properly. Can someone help as to why this php script doesn't work, or how to fix the "$Error" error?
PHP code:
<?php
// Replace this with your own email address
$siteOwnersEmail = 'info#mydomain.com';
if($_POST) {
$fname = trim(stripslashes($_POST['contactFname']));
$lname = trim(stripslashes($_POST['contactLname']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check First Name
if (strlen($fname) < 2) {
$error['fname'] = "Please enter your first name.";
}
// Check Last Name
if (strlen($lname) < 2) {
$error['lname'] = "Please enter your last name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Name
$name = $fname . " " . $lname;
// Set Message
$message = "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\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 (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['fname'])) ? $error['fname'] . "<br /> \n" : null;
$response .= (isset($error['lname'])) ? $error['lname'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
Thanks for your help!!
-Rob
You can always just initialize $error as
$error = [];
and then check for errors by
if(count($errors) === 0){
//no errors
}
Or, just use if(!isset($error)){//no errors}
I have PHP code that help me to other people can be in touch with me and send their opinion to me .
here's my PHP code :
<?php
$to = 'ticket.ritaweb#gmail.com';
$subject = 'Your decision/Opinion';
$name = htmlspecialchars($_REQUEST['name']);
$email = htmlspecialchars($_REQUEST['email']);
$site = htmlspecialchars($_REQUEST['site']);
$mobile = htmlspecialchars($_REQUEST['mobile']);
$nazar = htmlspecialchars($_REQUEST['nazar']);
$sqt = '2164854';
$sqtin = htmlspecialchars($_REQUEST['sqtin']);
if ($sqt === $sqtin) {
function clean_string($string) {
$bad = array("content-
type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "name : " . clean_string($name) . "\n";
$email_message .= "email : " . clean_string($email) . "\n";
$email_message .= "address : " . clean_string($site) . "\n";
$email_message .= "number : " . clean_string($mobile) . "\n";
$email_message .= "opinion : " . clean_string($nazar) . "\n";
$headers = 'From: ' . $mobile . "\r\n" . 'Reply-To: ' . $email . "\r\n" . 'X-Mailer:
PHP/' . phpversion();
mail($to, $subject, $email_message, $headers);
}
?>
and I called it with javascript ( ajax ) but I want a code in ajax that help me If my condition in PHP file was FALSE or each of the field was empty , show the message : "Your message was not send, be careful".
and here's my AJAX code :
<script type="text/javascript">
$(document).ready(function() {
$('#submit').click(function() {
$('#submit').attr('value', 'sending...');
$.post("themes/RitaWeb-v1/send.php", $(".rita_form").serialize(),
function(response) {
$('#submit').attr('value', 'sent.');
});
return false;
});
});
</script>
Use PHP to print messages on the page, like
{'status' : 'success'}
if the mail was sent and
{'status' : 'error'}
Then do something based on the output with js.
EDIT:
If you use jQuery.ajax instead of jQuery.post,you can also use the settings argument to let something happen if there ared errors during the request.
More information: http://api.jquery.com/jquery.ajax/
In your code, it would look like this.
PHP:
...
mail($to, $subject, $email_message, $headers);
print('{"status":"success"}');
}
else{
print('{"status":"error"}');
}
And your js:
$.ajax('themes/RitaWeb-v1/send.php').done(function(data){
status = $.parseJSON(data).status;
if(status == 'success'){
// do something
}
else if(ststus == 'error'){
// do something
}
else{
alert('unknown response');
}
});