I have PHPmailer library. This is a code which is a password reminder, when you add your email to input, it will send your forgotten password to your email. This code only sends through php mail, but since my host does not allow it, only option would be to use SMTP, right? So this is the php code with form:
<div class="tab-pane fade <?php
if ($tab == 'ForgotPass') {
echo 'active in';
}
?>" id="forgotpass">
<form role="form2" action="index?tab=ForgotPass" method="POST">
<fieldset>
<?php
#$_SESSION['email'] = $_POST['email'];
?>
<hr class="colorgraph">
<div class="form-group">
<input type="email" name="email" maxlength="64" id="email" class="form-control input-lg" placeholder="El. paštas" value="<?= $_SESSION['email'] ?>" required>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<input type="submit" name="forgot" class="btn btn-lg btn-success btn-block" value="send">
</div>
</div>
</fieldset>
</form>
<?php
if (isset($_POST['forgot'])) {
$email = $_POST['email'];
if (empty($email)) {
echo errorbox("Please enter email.");
} else {
$check = mysql_query("SELECT * FROM users WHERE email='$email'");
$row2 = mysql_fetch_assoc($check);
if (mysql_num_rows($check) > 0) {
$query = mysql_query("SELECT * FROM settings");
$row = mysql_fetch_assoc($query);
$subject = 'Forgot Password - ' . $row['title'] . '';
$message = '
<center>
<a href="' . $row['url'] . '" title="Visit ' . $row['url'] . '" target="_blank">
<h1><img src="' . $row['url'] . '' . $row['logo'] . '" title="' . $row['title'] . '"> ' . $row['title'] . '</h1>
</a><br />
<b>Registration details:</b><br />
Username: ' . $row2['username'] . '<b></b><br />
Password: ' . $row2['password'] . '<b></b><br />
</center>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'To: ' . $email . ' <' . $email . '>' . "\r\n";
$headers .= 'From: ' . $row['email'] . ' <' . $row['email'] . '>' . "\r\n";
#mail($to, $subject, $message, $headers);
echo okbox("We have just sent you your forgotten data from your account on the entered E-Mail Address");
} else {
echo errorbox("There is no player with such email.");
}
}
}
?>
AND i also have this SMTP configured code which just sends a message to test email and I already tested, it works, it does send the message, now I have to somehow make it so that it would send the password to user email:
<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.website.com";
$mail->SMTPAuth = true;
//$mail->SMTPSecure = "ssl";
$mail->Port = 25;
$mail->Username = "support#website.com";
$mail->Password = "emailpass";
$mail->From = "support#website.com";
$mail->FromName = "Test User";
$mail->AddAddress("emailthatgetsmessage#gmail.com");
//$mail->AddReplyTo("mail#mail.com");
$mail->IsHTML(true);
$mail->Subject = "Test message from server via SMTP Authentication";
$mail->Body = "Test Mail sent via SMTP Authentication";
//$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
Related
I am having trouble getting form to send attachments. It will either hang up or not send them and the error file shows nothing. Site is on a shared hosting server. The form works without attachments.
I am not a coder but i have searched this site and most of the information i have found refers to editing files created by composer which i am not using. Do I need to need to create a temp folder for the files before they are attached and if I do where do I define the path? The php code below is what I found for multiple attachments without composer and have been trying to adapt without success. Below is the code for attaching the file in the html form (I can post the entire html form if needed but since it works i did not see the need) and the php mail file any help would be appreciated.
<div class="form-group">
<label class="control-label col-sm-2" for="lname">Attach File:</label>
<div class="col-sm-10">
<input type="file" class="form-control" id="attachFile" name="attachFile[]" multiple="multiple">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="comment">Message:</label>
<div class="col-sm-10">
<textarea class="form-control" rows="5" name="message" id="message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" name="sendEmail">Send Email</button>
-------------------------------------------------------------------------------------------------------------------
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once 'PHPMailer/PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/PHPMailer/src/SMTP.php';
$msg = ''; // start with a blank message
$msg .= 'Email: ' . $_POST['email'] . "\n";
$msg .= 'City: ' . $_POST['city'] . "\n";
$msg .= 'State: ' . $_POST['state'] . "\n";
$msg .= 'Telephone: ' . $_POST['phone'] . "\n";
$msg .= 'Message: ' . $_POST['message'] . "\n";
if (array_key_exists('attachFile', $_FILES)) {
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = 'mail.domain.com'; // SMTP server
$mail->Username = 'form#domain.com';
$mail->Password = 'password';
$mail->setFrom ('form#domain.com');
$mail->AddAddress ('user#domain.com');
$mail->Subject = "Info Request";
$mail->Body = $msg;
$mail->WordWrap = 50;
# $mail->Port = 465;
for ($ct = 0, $ctMax = count($_FILES['attachFile']['tmp_name']); $ct < $ctMax; $ct++) {
//Extract an extension from the provided filename
$ext = PHPMailer::mb_pathinfo($_FILES['attachFile']['name'][$ct], PATHINFO_EXTENSION);
//Define a safe location to move the uploaded file to, preserving the extension
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['attachFile']['name'][$ct])) . '.' . $ext;
$filename = $_FILES['attachFile']['name'][$ct];
if (move_uploaded_file($_FILES['attachFile']['tmp_name'][$ct], $uploadfile)) {
if (!$mail->addAttachment($uploadfile, $filename)) {
$msg .= 'Failed to attach file ' . $filename;
}
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
}
}
if (!$mail->send()) {
$msg .= 'Mailer Error: ' . $mail->ErrorInfo;
} else {
$msg .= 'Message sent!';
}
}
# $return = $mail->Send();
?>
<html>
<head>
//<meta HTTP-EQUIV="refresh" Content="0;url=thankyou.html">
</head>
<body bgcolor="#366A7F">
</body>
</html>
I have a problem that after I fill out the contact form on my HTML website I receive over 50 same E-mails. Its a HTML form connected to contact.php file which code is shown bellow. I have set everything but maybe there is a problem in my code or somewhere else.
My code is over here
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+ (ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$verify = $_POST['verify'];
if(trim($name) == '') {
echo '<div class="error_message">Vyplnte meno.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="error_message">Vyplnte email.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">Zadali ste nesprávny e-mail, skúste to znovu.</div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="error_message">Vyplnte text správy.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe#yourdomain.com";
$address = "noreply#marcelaskolenia.sk";
$address = "lubosmasura#gmail.com";
$toCustomer = $email;
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'Mate novu spravu od ' . $name . '.';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "Mate novu spravu od $name." . PHP_EOL . PHP_EOL;
$e_content = "\"$subject\"" . "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "Kontaktujte $name cez email, $email alebo cez mobil $phone";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers .= 'To: Test <noreply#marcelaskolenia.sk>' . "\r\n";
$headers .= 'From: Testk <noreply#marcelaskolenia.sk>' . "\r\n";
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h3 class'mark'>Sprava bola odoslana.</h3>";
echo "<p>Dakujeme <strong>$name</strong>, Vasa sprava nam bude dorucena.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
HTML code
<div class="contact_form">
<div id="message"></div>
<form id="contactform" class="row" action="contact.php" name="contactform" method="post">
<div class="col-md-12">
<input type="text" name="name" id="name" class="form-control" placeholder="Meno">
<input type="text" name="email" id="email" class="form-control" placeholder="Email">
<input type="text" name="phone" id="phone" class="form-control" placeholder="Telefónne číslo">
<input type="text" name="subject" id="subject" class="form-control" placeholder="Predmet">
<textarea class="form-control" name="comments" id="comments" rows="6" placeholder="Text správy"></textarea>
<button type="submit" value="SEND" id="submit" class="btn btn-primary"> ODOSLAŤ</button>
</div>
</form>
</div>
</div><!-- end col -->
Any Ideas why is this happening?
Thank you.
I purchased a template from Template Monster and I can't figure out how to make the form work.
Here's the form code:
<form id="form">
<div class="success_wrapper">
<div class="success-message">Το μήνυμά σας εστάλη.</div>
</div>
<label class="name">
<input type="text" placeholder="Όνομα*:" data-constraints="#Required #JustLetters" />
<span class="empty-message">*Το πεδίο είναι υποχρεωτικό.</span>
<span class="error-message">*Το όνομα δεν είναι έγκυρο.</span>
</label>
<label class="email">
<input type="text" placeholder="Email*:" data-constraints="#Required #Email" />
<span class="empty-message">*Το πεδίο είναι υποχρεωτικό.</span>
<span class="error-message">*Το email δεν είναι έγκυρο.</span>
</label>
<label class="phone">
<input type="text" placeholder="Τηλέφωνο:" data-constraints=" #JustNumbers"/>
<span class="empty-message">*Το πεδίο είναι υποχρεωτικό.</span>
<span class="error-message">*Το τηλέφωνο δεν είναι έγκυρο.</span>
</label>
<label class="message">
<textarea placeholder="Μήνυμα:" data-constraints=' #Length(min=20,max=999999)'></textarea>
<span class="empty-message">*Το πεδίο είναι υποχρεωτικό.</span>
<span class="error-message">*Το μήνυμα είναι πολύ μικρό.</span>
</label>
<div>
<div class="clear"></div>
<div class="btns">
Καθαρισμος
Αποστολη</div>
</div>
</form>
There is also a MailHandler.php in a folder named "bat" with the following code:
<?php
//SMTP server settings
$host = "smtp.host.com";
$port = "587";
$username = "";
$password = "";
$messageBody = "";
if($_POST['name']!='false'){
$messageBody .= '<p>Visitor: ' . $_POST["name"] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['email']!='false'){
$messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}else{
$headers = '';
}
if($_POST['state']!='false'){
$messageBody .= '<p>State: ' . $_POST['state'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['phone']!='false'){
$messageBody .= '<p>Phone Number: ' . $_POST['phone'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['fax']!='false'){
$messageBody .= '<p>Fax Number: ' . $_POST['fax'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['message']!='false'){
$messageBody .= '<p>Message: ' . $_POST['message'] . '</p>' . "\n";
}
if($_POST["stripHTML"] == 'true'){
$messageBody = strip_tags($messageBody);
}
if($host=="" or $username=="" or $password==""){
$owner_email = $_POST["owner_email"];
$headers = 'From:' . $_POST["email"] . "\r\n" . 'Content-Type: text/plain; charset=UTF-8' . "\r\n";
$subject = 'A message from your site visitor ' . $_POST["name"];
try{
if(!mail($owner_email, $subject, $messageBody, $headers)){
throw new Exception('mail failed');
}else{
echo 'mail sent';
}
}catch(Exception $e){
echo $e->getMessage() ."\n";
}
}else{
require_once 'Mail.php';
$to = $_POST["owner_email"];
$subject = 'A message from your site visitor ' . $_POST["name"];
$headers = array (
'From' => 'From:' . $_POST["email"] . "\r\n" . 'Content-Type: text/plain; charset=UTF-8' . "\r\n",
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory(
'smtp',
array (
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $messageBody);
try{
if(PEAR::isError($mail)){
echo $mail->getMessage();
}else{
echo 'mail sent';
}
}catch(Exception $mail){
echo $mail->getMessage() ."\n";
}
}
?>
Any help will be truly appreciated!
Ok I figured this one out... You have to get into the forms javascript file it will look like TMForms.js or something like that in the file named JS, under the very top portion of the code that looks like this
okClass:'ok'
,emptyClass:'empty'
,invalidClass:'invalid'
,successClass:'success'
,onceVerifiedClass:'once-verified'
,mailHandlerURL:'http://www.yourwebsitename.com/bat/MailHandler.php
,successShowDelay:'4000'
,stripHTML:true
MAKE SURE TO PUT THE FULL URL PATH TO THE BAT FOLDER LIKE I HAVE in bold, this will fix your issue.
Replace yourwebsite name with your actual URL and you are good to go...
You must contact the support from where your purchased the template, they can help you
I have a web form which emails the form content back to me using the phpmailer function. I'm trying to add an AddAttachment feature but I seems to have an issue in the php.
This is my html snippet:
<td>
<div align="right">Add attachment :</div>
</td>
<td colspan="2">
<input type="file" name="uploaded_file" id="uploaded_file" />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
</td>
And this is my php;
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mailer.********.local"; // SMTP server
$mail->From = $_POST['email'];
$mail->AddAddress("frank********#gmail.com");
$mail->Subject = "Request for Contract Registration for " . $_POST['name'];
$mail->Body = "Supplier number : " . $_POST['suppno'] . "\r\n";
$mail->Body .= "Payee name : " . $_POST['name'] . "\r\n";
$mail->Body .= "Address : " . $_POST['add'] . "\r\n";
$mail->Body .= " : " . $_POST['add2'] . "\r\n";
$mail->Body .= " : " . $_POST['add3'] . "\r\n";
$mail->Body .= "Nature of business : " . $_POST['nob'] . "\r\n";
$mail->Body .= "Tax Ref : " . $_POST['rctref'] . "\r\n";
$mail->Body .= "Description of works : " . $_POST['descofwks'] . "\r\n";
$mail->Body .= "Start date of contract : " . $_POST['stdte'] . "\r\n";
$mail->Body .= "End date of contract : " . $_POST['enddte'] . "\r\n";
$mail->Body .= "Location of contract : " . $_POST['location'] . "\r\n";
$mail->Body .= "Estimated value of contract : " . $_POST['contractval'] . "\r\n";
$mail->Body .= "Confirm contract : " . $_POST['confirm'] . "\r\n";
$mail->Body .= "Declaration : " . $_POST['declaration'] . "\r\n";
$mail->Body .= "Department : " . $_POST['dept'] . "\r\n";
$mail->AddAttachment($_POST['uploaded_file']);
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
header('Location: confirm.htm');
}
?>
Is there an issue with my path??? It's probally something simple that I'm missing but if anyone can help me I'd greatly appreciate it!
Thanking you in advance,
Frank.
Instead of using
$mail->AddAttachment($_POST['uploaded_file']); // WRONG
try this
if (isset($_FILES['uploaded_file']) &&
$_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['uploaded_file']['tmp_name'],
$_FILES['uploaded_file']['name']);
}
Uploaded files are stored in the temp folder.
You should add the attachment from the filesystem, it is a mistake to use $_POST for this.
Once I submit the form it will aks me this "We weren't able to send your message. Please contact site#sitenamexample.com It will not send to my WebMail. I can't figureout the problem. I really need your help guys.
$domain = "http://sitenamexample.com" . $_SERVER["HTTP_HOST"];
$siteName ="My Website name";
$siteEmail = "site#sitenamexample.com";
$er = "";
if(isset($_POST["contactEmail"])){
global $subject, $message;
$contactName = htmlentities(substr($_POST["contactName"], 0, 100), ENT_QUOTES);
$contactEmail = htmlentities(substr($_POST["contactEmail"], 0, 100), ENT_QUOTES);
$messageSubject = htmlentities(substr($_POST["messageSubject"], 0, 100), ENT_QUOTES);
$messageContent = htmlentities(substr($_POST["messageContent"], 0, 100), ENT_QUOTES);
if (!preg_match('/' . '^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]{2,})+$' . '/'
, $contactEmail))
{
$er .= 'Please enter a valid e-mail address.<br>';
}
if ($contactName == "" || $contactEmail == "" || $messageSubject == "" ||
$messageContent == ""){
$er .= 'Your Name, E-mail, Message Subject, and Message Content cannot be left
blank.<br />';
}
if($er == ''){
$subject = $messageSubject;
$message =
'<html>
<head>
<title>' . $siteName . ': A Contact Message </title>
.<body>
' . wordwrap($messageContent, 100) . '
</body>
</html>';
}
$to = $siteName . ' Contact Form <' . $siteEmail . '>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=is0-8859-1' . "\r\n";
$headers .= 'From: ' . $contactName . ' <' . $contactEmail . '>' ."\r\n";
$headers .= 'Reply-To: ' . $contactName . ' <' . $contactEmail . '>' . "\r\n";
$headers .= 'Return-Path: ' . $contactName . ' <' . $contactEmail . '>' . "\r\n";
$headers .= 'X-Mailer: ' . $siteName . "\r\n";
if (mail($to, $subject, $message, $headers)){
echo '<div>Thank you for contacting ' . $siteName . '. We will read your message and
contact you if necessary.</div>';
}
else {
$er .= 'We weren\'t able to send your message. Please contact ' . $siteEmail .
'.<br />';
}
}
else {
showContactForm();
}
if ($er != '' && isset($_POST["contactEmail"])){
showContactForm($contactName, $contactEmail, $messageSubject, $messageContent, $er);
}
else if($er != '' && !isset($_POST["contactEmail"])){
showContactForm('', '', '', $er);
}
function showContactForm($contactName = "", $contactEmail = "", $messageSubject = "",
$messageContent = "", $er = ''){
echo '<div style="font-weight:bold; margin: 5px 0; "> ' . $er . '</div>
<div id="email_form">
<h1> Contact Form</h1>
<form method="post" name="contact" action="#contactus">
<label for="name">Name:</label>
<input type="text" id="author" name="contactName" class="input_field" />
<div class="cleaner h10"></div>
<label for="email" class="adjust">Email:</label>
<input type="text" id="email" name="contactEmail" class="input_field" />
<div class="cleaner h10"></div>
<label for="messageSubject" class="adjust">Subject:</label>
<input type="text" id="messageSubject" name="messageSubject" class="input_field" />
<div class="cleaner h10"></div>
<label for="text" class="adjust">Comments:</label>
<textarea id="text" name="messageContent" rows="0" cols="0"></textarea>
<div class="cleaner h10"></div>
<input type="submit" class="submit_btn float_l" name="submitButton" id="submit"
value="Send Message" />
<input type="reset" class="submit_btn float_r" name="reset" id="reset" value="Reset" />
</form>
<div class="clearfix"></div>
</div>';
}
?>
<div class="clearfix"></div>
<?php include ("includes/footer.php"); ?>
You're code is generally correct (but remember - $to should only be the email address without any < or >). I think the reason of your problem is disabled Sendmail PHP Library - you can enable it in server settings or if you have not access to it - read about phpmailer.