sendmail - PHP Mail Sending Blank Message On Page-Load -
i having issue php mailer. sending blank email every time page loaded.
i'm sure it's simple (maybe missing condition if submit button hit) fix this.
their documentation doesn't seem have 1 though , script first worked when first used started sending emails on page load after first few times. thanks!
<form method="post" action=""> <div class="form-group"> <input name="name" type="text" class="form-control" placeholder="enter name"> </div> <div class="form-group"> <input name="email" type="email" class="form-control" placeholder="enter email"> </div> <div class="form-group"> <input name="subject" type="text" class="form-control" placeholder="enter subject"> </div> <div class="form-group"> <textarea name="message" class="form-control" rows="5" placeholder="enter message"></textarea> </div> <button name="submit" type="submit" value="submit" class="btn btn-submit">submit</button> </form> <?php require '/phpmailer/phpmailerautoload.php'; require_once('/phpmailer/class.phpmailer.php'); include("/phpmailer/class.smtp.php"); $emailaddress = 'info@newpointdigital.com'; $message= 'name: '.$_post['name'].'<br /> email: '.$_post['email'].'<br /> subject: '.$_post['subject'].'<br /> ip: '.$_server['remote_addr'].'<br /><br /> message:<br /><br /> '.nl2br($_post['message']).' '; $mail = new phpmailer(); $mail->issmtp(); // telling class use smtp $mail->host = "smtp.gmail.com"; // smtp server //$mail->smtpdebug = 2; // 1 = errors , messages,2 = messages $mail->smtpauth = true; // enable smtp authentication $mail->host = "smtp.gmail.com"; // sets smtp server $mail->port = 465; // set smtp port gmail server $mail->username = "info@newpointdigital.com"; // smtp account username (the email account created) $mail->password = "newpoint!@#$"; // smtp account password (the password above email account) $mail->smtpsecure = 'ssl'; // enable encryption, 'ssl' accepted $mail->charset = 'utf-8'; // interprets foreign characters $mail->setfrom($_post['email']); $mail->addreplyto($_post['email']); $mail->subject = "contact form ".$_post['name']." "; $mail->msghtml($message); $mail->addaddress($emailaddress); if(!$mail->send()) { echo 'message not sent.'; echo 'mailer error: ' . $mail->errorinfo; } else { echo 'message has been sent'; } ?>
you don't check see if form submission occurred. call email code when page loads. there several ways decide if form submission occurred. 1 way check see if form action post:
if ($_server['request_method'] === 'post') { // email code goes here }
you can check see if submit button pressed:
if (isset($_post['submit'])) { // email code goes here }
other things know:
- you don't data validation still possible submit nothing resulting in blank email being sent
- you don't protect against header injections makes form vulnerable sending spam
Comments
Post a Comment