I have file with name contact.php, inside I have form:
<form method="post" name="kontakt" action="send_mail.php">
<fieldset class="formularz_kontaktowy">
<legend>Formularz kontaktowy</legend>
<div><label id="lblStatus"></label></div>
<div><input type="text" name="txtName" title="Imię i nazwisko" id="txtName" class="text"></div>
<div><input type="text" name="txtEmail" title="Email" id="txtEmail" class="text"></div>
<div><input type="text" name="txtTitle" title="Tytuł" id="txtTitle" class="text"></div>
<div><textarea cols="30" rows="10" name="txtMessage" id="txtMessage" class="text" title="Treść wiadomości"></textarea></div>
<input type="submit" name="btnSendmail" value=Send">
</fieldset>
</form>
send_mail.php
<?php
require_once "Mail.php";
$from = "<mailaddress#gmail.com>";
$to = "<mail2#gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "<mailaddress#gmail.com>";
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
after click btnSendmail in browser I have URL, for example: localhost/contact.php?txtName=Mary+Smith&txtEmail=mailMail%40gmail.com&txtTitle=dfd&txtMessage=fgdf&btnSendmail=Send, but mail doesn'y sent and I have not message. Why?
Try changing
$username = "<mailaddress#gmail.com>";
to
$username = "mailaddress#gmail.com";
I found problem. I had two tags , one in template and second, nested in contact.php. Thanks a lot for help.
Related
I am using html ( bootstrap4) contact us page and trying to make it dynamic . server test ok with the php test script using pear mail.
But issue when trying to make dynamic and calling filed out put in email its not working
Can any one review below php script and tell me where is issue
HOW can I call here this part : $email_subject ? email body ? and email message ?
<form id="contact-form" method="post" action="test.php">
<div class="row">
<!-- Name -->
<div class="col-md-6 mb-2">
<input type="text" name="Full_Name" class="form-control" placeholder="Firstname *" required="required" data-error="Firstname is required.">
</div>
<!-- Email -->
<div class="col-md-6 mb-2">
<input type="email" name="email" class="form-control" placeholder="email *" required="required" data-error="Valid email is required.">
</div>
<!-- subject -->
<div class="col-md-12 mb-2">
<input type="text" name="subject" class="form-control" placeholder="subject *" required="required" data-error="Valid subject is required.">
</div>
<!-- Message -->
<div class="col-md-12 mb-2">
<textarea name="message" class="form-control" placeholder="Message *" rows="4" required="required" data-error="Please,leave us a message."></textarea>
</div>
<!-- Submit Button -->
<div class="col-12 text-right">
<input type="submit" class=" form-control btn btn-success btn-send" value="Send message">
</div>
</div>
</form>
<!-- Email -->
<div class="col-md-6 mb-2"> <input type="email" name="email" class="form-control" placeholder="email *" required="required" data-error="Valid email is required."> </div>
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
require_once "Mail/Mail-1.4.1/Mail.php";
$name = $_POST["Full_Name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$host = "*******"; <!--i will use my hosting server url-->
$username = "*******"; <!--i will use my email-->
$password = "*****"; <!--i will use here my email pss-->
$port = "2525";
$to = "******"; <!--i will use the email here-->
$email_from = "*****"; <!--how to call email from here by input that i defined above in php file->
$email_subject = "******" ; <!--how to call sujectl from here by input that i defined above in php file->
$email_body = "*****"; <!--how to call message from here by input that i defined above in php file->
$email_address = "*****";
$headers = array ('From' => $email, 'To' => $to, 'Subject' => $subject, 'Message' => $message, Reply-To' => $email_address);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $email_body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
I've done a slight revamp of the code which might help, also if you're using gmail to send the email please see the last bit of this article: https://help.dreamhost.com/hc/en-us/articles/216140597-How-do-I-send-PHP-mail-via-SMTP-
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
require_once "Mail/Mail-1.4.1/Mail.php";
$name = $_POST["Full_Name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$host = "*******"; <!--i will use my hosting server url-->
$username = "*******"; <!--i will use my email-->
$password = "*****"; <!--i will use here my email pss-->
$port = "2525";
$to = "******"; <!--i will use the email here-->
$reply_to = "*****";
// Please note the change to the headers
$headers = array ('From' => $email, 'To' => $to, 'Subject' => $subject, Reply-To' => $reply_to);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $message);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
After carefully reading the code and optimized slightly its working pefrectly and here i am pasting it.. might it be helpful for some one facing the same issue with html/php/pear
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
require_once "Mail/Mail-1.4.1/Mail.php";
$name = $_POST["Full_Name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$host = "mail.example.com";
$username = "xyz#example.com";
$password = "abc";
$port = "25";
$to = "xyz#example.com";
$reply_to = "xyz#example.com";
$headers = array ('From' => $email, 'To' => $to, 'Subject' => $subject, 'Reply-To' => $reply_to);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $message);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
This question already has answers here:
Blank PHP Emails
(2 answers)
Closed 5 years ago.
This script works, but it sends a blank email when it is visited. Then a real one when submitted. I want to get rid of the first blank one it sends. This is mostly edited code I found here and another place I pieced together to work for my needs.
<?php
ini_set("include_path", '/home/user/php:' . ini_get("include_path") );
require_once "Mail.php";
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$contact = $_POST['contact'];
$comments = $_POST['comments'];
$from = "Info <info#domain.net>";
$to = "Info <info#domain.net>";
$subject = "Customer Contact Form";
$body = "If this works it will be edited a bit \n" . "First Name: " . $first_name . "\n" . "Last Name: " . $last_name . "\n" . "Email: " . $email . "\n" . "Address: " . $address . "\n" . "Phone: " . $phone . "\n" . "Please contact me by: " . $contact . "\n" . "Other Comments: " . $comments;
$host = "mail.domain.net";
$username = "info#domain.net";
$password = "emailpassword";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
//if (PEAR::isError($mail)) {
// echo("<p>" . $mail->getMessage() . "</p>");
//} else {
// echo("<p>Message successfully sent!</p>");
//}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Address: <input type="text" name="address"><br>
Phone: <input type="text" name="phone"><br>
Perferred Form of Contact: <input type="text" name="contact"><br>
Message:<br><textarea rows="5" name="comments" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Because you don't check the form data!
Just use a simple if(isset($_POST['first_name'])) around your code for generating and sending the mail and it will just send an email if you've sent the form.
thanks for your help. I hope you can help me.
I have this contact form:
<form action="assets/php/sendmail.php" method="post" id="contactForm">
<p class="text_cont"><input id="name" input type="text" name="name" placeholder="<?php echo FORM_PLACEHOLDER_1 ?>" required></p>
<p class="text_cont"><input id="email" input type="text" name="email" placeholder="<?php echo FORM_PLACEHOLDER_2 ?>" required></p>
<p class="text_cont"><input id="subject" input type="text" name="subject" placeholder="<?php echo FORM_PLACEHOLDER_3 ?>"></p>
<p class="text_cont"><textarea id="message" textarea name="message" placeholder="<?php echo FORM_PLACEHOLDER_4 ?>"></textarea></p>
<div class="col-lg-4 col-md-3 col-sm-3"><p><input type="submit" id="send" class="btn btn-default" value="<?php echo SEND_PLACEHOLDER ?>" /></p></div>
sendmail.php has this code because the host company recommends it (windows servers), I have modified a little:
error_reporting( E_ALL & ~( E_NOTICE | E_STRICT | E_DEPRECATED ) );
require_once "Mail.php";
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$subject = htmlspecialchars($_POST['subject']);
$message = htmlspecialchars($_POST['message']);
$to = 'my#mail.com';
$from = $email;
$host = 'smtp.myhost.com';
$username = 'usernameformail';
$password = 'passwordformail';
$body =
utf8_decode("This mail has been written from your web: \r\n") .
utf8_decode("Name: " . $name . "\r\n") .
utf8_decode("Email: " . $email . "\r\n") .
utf8_decode("Subject: " . $subject . "\r\n") .
utf8_decode("Message: " . $message);
$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject,
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
};
$name = '';
$email = '';
$subject = '';
$message = '';
?>
So, all works fine. However, when I write code in "if (PEAR::isError($mail))" to redirect to other page to know if is an error or success, it doesn't works. For example:
if (PEAR::isError($mail)) {
header('Location: http://www.myweb.com/error.php');
} else {
header('Location: http://www.myweb.com/thanks.php');
};
If I write an script, it's the same:
if (PEAR::isError($mail)) {
echo "<script type='text/javascript'> redirect in javascript to error </script>";
} else {
echo "<script type='text/javascript'> redirect in javascript to thanks</script>";
};
Mail is always sent correctly to me, but someone who send the mail never know if it has been sent or not. It's like "if/else" doesn't work. I click on submit button and nothing happen (mail send correctly but people who write it don't know that).
Do you see where is my mistake?
Thanks a lot.
I'm trying to make php contact form work on webhost we are currently using. I found with google this trick to get it work when smtp is off:
http://www.top-answers.net/webhost/web-hosting.html
What I found it only gives me white screen on browser when I upload it to server but as offline it shows the form correctly.
Is there way to make this work or should I seek for another solution?
Thank you
<?php
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$company = trim($_POST['company']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$msg = $_POST['message'];
if (has_header_injection($name) || has_header_injection($company) ||has_header_injection($email) ||has_header_injection($phone)) {
die();
}
if ( !$name || !$company || !$email || !$phone || !$msg ) {
echo '<h4 class="error">Kaikki kentät ovat pakollisia.</h4>Palaa takaisin ja yritä uudelleen';
exit;
}
$to = "info#mydomain.com";
$subject = "$name lähetti viestin lomakkeen kautta";
$message = "Nimi: $name\r\n";
$message .= "Yritys: $company\r\n";
$message .= "Sähköposti: $email\r\n";
$message .= "Puhelin: $phone\r\n";
$message .= "Viesti:\r\n$msg";
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe') {
$message .= "\r\n\r\n$name tilasi uutiskirjeen. Muista lisätä sähköpostilistalle!\r\n";
}
$message = wordwrap($message, 72);
require_once "Mail.php";
$from = "info#mydomain.com"; \\the mail-iD under your domain
$to = "myemail#myemail.com"; \\the TO gmail ID
$subject = "PHP Enquiry form";
$body = $email_message;
$host = "mail.mydomain.com"; \\ your domain mail server
$username = "info#mydomain.com"; \\the mail-id under your domain
$password = "secretsecurepassword"; \\your password for yourid#mydomain.com
$headers = array ('From' => $from, 'To' => $to, 'Reply-To'=> $email_from, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $body);
?>
<h5>Kiitos yhteydenotosta!</h5>
<p>Vastaamme kahden arkipäivän kuluessa.</p>
<?php } else { ?>
<form method="post" action="" id="contact">
<label for="name">Nimi</label>
<input type="text" id="name" name="name">
<label for="company">Yritys</label>
<input type="text" id="company" name="company">
<label for="email">Sähköposti</label>
<input type="email" id="email" name="email">
<label for="phone">Puhelin</label>
<input type="text" id="phone" name="phone">
<label for="message">Viesti</label>
<textarea id="message" name="message"></textarea>
<input type="checkbox" id="subscribe" name="subscribe" value="Subscribe">
<label for="subscribe">Tilaa uutiskirje</label>
<button type="submit" class="button next" name="contact_submit">Lähetä</button>
</form>
<?php } ?>
i have this simple contact form
<form action="sokhna-golf-mail.php" method="post" onSubmit="alert('Thank you for submitting your inquiry, a representative will be in contact with you shortly.')">
<input type="hidden" name="source" id="source" value="EINBAY - Sokhna Golf Club">
<input type="text" placeholder="Enter your name *" name="Name" id="Name" required>
<input type="email" placeholder="Enter your email *" name="Email" id="Email" required>
<textarea spellcheck="true" id="Message" rows="3" cols="20" name="Message" class="fullwidth" placeholder="Enter your message *" style="width:100%" required></textarea>
<button>Submit</button>
</form>
and here is the code from sokhna-golf-mail.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
var fb_param = {};
fb_param.pixel_id = '6009625383124';
fb_param.value = '0.00';
fb_param.currency = 'USD';
(function(){
var fpw = document.createElement('script');
fpw.async = true;
fpw.src = '//connect.facebook.net/en_US/fp.js';
var ref = document.getElementsByTagName('script')[0];
ref.parentNode.insertBefore(fpw, ref);
})();
</script>
<noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/offsite_event.php? id=6009625383124&value=0¤cy=USD" /></noscript>
<?php
require_once "Mail.php";
$from = "mail#einbay.com";
$to = "user1#sokhnagolfclub.com,user2#einbay.com";
$subject = " " .$_POST["source"];
$body = "Name: ".$_POST["Name"]."\r\n";
$body .= "Email: ".$_POST["Email"]."\r\n";
$body .= "Message: ".$_POST["Message"]."\r\n";
$body .= "source: ".$_POST["source"]."\r\n";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "username#gmail.com";
$password = "secret";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password
));
$mail = $smtp->send($to, $headers, $body);
$file = 'WestHeightsLeads.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= $_POST["Name"].",__,".$_POST["Email"].",__,".$_POST["Subject"].",__,".$_POST["Message"].";__; \n";
// Write the contents back to the file
file_put_contents($file, $current);
if (PEAR::isError($mail)) {
} else {
}
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?msg=xJfDe');
?>
</body>
</html>
when i submit the form i get this error message (405 - http verb used to access this page is not allowed. the page you are looking for cannot be displayed because an invalid method (http verb) was used to attempt access.)
i searched the web for this issue and found some related topics even here on stackoverflow but none had a valid solution .. but most of the results on google saying this is probably caused by host provider not configured to send smtp mail.