Sending email (gmail) using HTML forms and PHP

时间:2015-09-01 22:10:22

标签: php html email

I tried the code here and changed it a little

  1. Wrote the php code in a seperate file
  2. Added password and other credentials that I think are needed for Gmail but the code doesn't work (I received this message: "Error")

Could you please kindly help me with that? I tried to add my question on this page but it seems that only one question could be asked per topic. Really appreciate your help. I know it might seems repetitive but I really have no experience in PHP and HTML and cannot figrure out wh

HTML code:

<div class="col-lg-8 col-md-8 col-sm-8">
                <form id="contact-form" action="bat/gmil.php" method="post">
                      <div class="contact-form-loader"></div>
                      <fieldset>
                        <label class="name form-div-1">
                          <input type="text" id="name" name="name" placeholder="Name:" value="" data-constraints="@Required @JustLetters"  />
                          <span class="empty-message">*This field is required.</span>
                          <span class="error-message">*This is not a valid name.</span>
                        </label>
                        <label class="email form-div-2">
                          <input type="text" id="email" name="email" placeholder="E-mail:" value="" data-constraints="@Required @Email" />
                          <span class="empty-message">*This field is required.</span>
                          <span class="error-message">*This is not a valid email.</span>
                        </label>
                        <label class="phone form-div-3">
                          <input type="text" id="phone" name="phone" placeholder="Phone:" value="" data-constraints="@JustNumbers" />
                          <span class="empty-message">*This field is required.</span>
                          <span class="error-message">*This is not a valid phone.</span>
                        </label>
                        <label class="message form-div-4">
                          <textarea id="message" name="message" placeholder="Message:" data-constraints='@Required @Length(min=20,max=1000)'></textarea>
                          <span class="empty-message">*This field is required.</span>
                          <span class="error-message">*The message is too short.</span>
                        </label>
                        <!-- <label class="recaptcha"><span class="empty-message">*This field is required.</span></label> -->
                        <div class="btns">
                            <input href="#" id="submit" name="submit" type="submit" value="Send" class="btn-default btn3"></input>
                        </div>
                      </fieldset> 
                      <div class="modal fade response-message">
                        <div class="modal-dialog">
                          <div class="modal-content">
                            <div class="modal-header">
                              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                              <h4 class="modal-title">Modal title</h4>
                            </div>
                            <div class="modal-body">
                              You message has been sent! We will be in touch soon.
                            </div>      
                          </div>
                        </div>
                      </div>
                </form>
            </div>

Php code (original password and email address has been removed):

require_once 'swift/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
    ->setUsername('test@gmail.com')
    ->setPassword('*******');

$to = "test@gmail.com";
$from = $_POST['email'];

$msg = "NAME: "  .$_POST['name']    ."<br>\n";
$msg .= "EMAIL: "  .$_POST['email']    ."<br>\n";
$msg .= "PHONE: "  .$_POST['phone']    ."<br>\n";
$msg .= "MESSAGE: "  .$_POST['message']    ."<br>\n";

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance('Visitor Email')
    ->setFrom(array($from))
    ->setTo(array($to))
    ->setBody($msg);

$result = $mailer->send($message);

If you even share a working (and separate) and simple php and html code for sending email it would be so helpful. I tested many simple php amd html codes that I found on the web on different hosts but surprisingly none of them worked! I really have no idea what is the problem!

0 个答案:

没有答案