Problems, need help? Have a tip or advice? Post it here.
28 posts Page 1 of 3
Hi KK,

I got a hosting for a client which does not allow the use of mail().

I know how to send mail with SMTP through php mailer, no problem.

But I want to use the Couch script to check the mail for error in the form, I think it's great feature : I noticed the config.php 15 to check for "alternative method to send emails".

How to I use that feature? I mean what does it do?

Regards,
Paolo E.
Hi Paolo,

If the "alternative method to send emails" setting is '1' in config file, the cms:send_mail tag tries to use an alternative script that ships with Couch (instead of the default mail() PHP function).

It is somewhat akin to using phpmailer instead of mail() but the script used is not that complex. Please try making the change to config file and continue using the cms:send_mail tag. See if the mail goes through.

If this does not work, and since you can work with phpmailer library, you can try substituting the cms:send_mail block in your form with <cms:php..</cms:php> and use phpmailer in there.

Do let me know if you require any assistance with the process.

Thanks.
Hello again,

Ok,
I tried the alternative method, it doesn't work.

If I use the <cms:php> tag, should I let the config .15 to 1 or turn it back to 0?

I'm trying now with:

<cms:if k_success>
<cms:php>
require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'hostcloud2.vdconline.vn'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '####@palmlogvn.com'; // SMTP username
$mail->Password = '########'; // SMTP password
$mail-> SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to


$mail->addAddress('vietnamsales@asia.com', 'Palm Logistic Website'); // Add a recipient
$mail->addReplyTo($_POST['email'], $_POST['name']);

$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML

$mail->From = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->Subject = $_POST['subject'];
$mail->Body = $_POST['message'];


if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
</cms:php>

</cms:if>

regular stuff, but no success so far...
(the SMTP script works well otherwise, if I call it with action" " on a separate page)

Any idea?
Thanks,
Paolo
Hi,

The config setting comes into play only with cms:send_mail tag. Since your code is no longer using that it does not matter what value it is set to.

As for debugging swiftmailer, Paolo I suggest we do it piece-meal.
1. Place the cms:php block outside of the k_success condition so that it executes everytime the template is executed (will be easier to test) and instead of $_POST variables, use hardcoded values for all parameters.
2. The above should ideally work because it is not dependent on any other factor now.
3. If it does, try moving the entire block back to the k_success condition.
4. Submit form and it should work as before.
5. If it does, begin substituting the hardcoded values with '<cms:show frm_email />' stuff (i.e. use variables set by Couch upon successful form submission.

Please let me know how it goes.
If you require my assistance, you may PM me the FTP+Couch creds.
Hi KK,

it's ok, everything works well now. I let the code here, just in case somebody who wants to send SMTP mail needs it - saves time...

1) Upload PHPMailer for Github
2) I made it works with Gmail, otherwise coding is the same
Code: Select all
<cms:form id="contactForm" action="" method="POST">
<div class="row">
    <div class="form-group">
        <div class="col-md-6">
            <label>Your name *</label>
            <cms:input type="text" value="" maxlength="100" class="form-control" name="name" id="name" required='1' />
            <cms:if k_error_name>
            <p style="display:block" class="alert alert-danger">Please enter your name.</p>
            </cms:if>
        </div>
        <div class="col-md-6">
            <label>Your email address *</label>
            <cms:input type="text" value="" maxlength="100" class="form-control" name="email" id="email" required='1' validator='email' />
            <cms:if k_error_email>
            <p style="display:block" class="alert alert-danger">Please enter a valid email address.</p>
            </cms:if>
        </div>
    </div>
</div>
<div class="row">
    <div class="form-group">
        <div class="col-md-12">
            <label>Subject *</label>
            <cms:input type="text" value="" maxlength="100" class="form-control" name="subject" id="subject" required='1' />
            <cms:if k_error_subject>
            <p style="display:block" class="alert alert-danger">Please enter a subject.</p>
            </cms:if>
        </div>
    </div>
</div>
<div class="row">
    <div class="form-group">
        <div class="col-md-12">
            <label>Message *</label>
            <cms:input type="textarea" maxlength="5000" rows="10" class="form-control" name="message" id="message" required='1'  ></cms:input>
            <cms:if k_error_message>   
                <p style="display:block" class="alert alert-danger">Please enter a message.</p>
            </cms:if>   
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        <cms:if k_success>
        <cms:php>
            require 'PHPMailer/PHPMailerAutoload.php';
           
            $mail = new PHPMailer;
         
            $mail->isSMTP();   // Set mailer to use SMTP
            $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;    // Enable SMTP authentication
            $mail->Username = 'youraddress@gmail.com';  // SMTP username
            $mail->Password = 'yourpassword';   // SMTP password
            $mail->SMTPSecure = 'ssl';     // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 465;   // TCP port to connect to
           
           
            $mail->addAddress('address_to_send_to@website.com', 'First Last');  // Add a recipient
            $mail->addReplyTo('<cms:show frm_email />', '<cms:show frm_name />');
           
            $mail->WordWrap = 50;    // Set word wrap to 50 characters
            $mail->isHTML(true);     // Set email format to HTML
           
            $mail->From = '<cms:show frm_email />';
            $mail->FromName = '<cms:show frm_name />';
            $mail->Subject = '<cms:show frm_subject />';
            $mail->Body    = '<cms:show frm_message />';
           
           
            if(!$mail->send()) {
                echo 'Message could not be sent.';
                echo 'Mailer Error: ' . $mail->ErrorInfo;
            } else {
                echo 'Message has been sent';
            }
            </cms:php>
        </cms:if>
        <br />
        <input type="submit" value="Send Message" class="btn btn-primary btn-lg" data-loading-text="Loading..." />
      </div>
</div>
</cms:form>

Thanks for your help,
Cheers,
Paolo

====================
Addendum by KK -

Following thread describes how to send attachments with cms:send_mail tag - File attachments in Contact form (http://www.couchcms.com/forum/viewtopic.php?f=8&t=6959).

To send attachments with Paolo's method above, please see the following -
http://www.couchcms.com/forum/viewtopic ... 060#p17060
Thanks for sharing the code, Paolo.
I am sure it'll help many.
I have attempted to use pretty much identical code, but do not seem to get anything - not even error or success messages!
Can you see from a quick glance what I might have done wrong?

Thanks

Code: Select all
<cms:form action='' method='post' id='contact_form'>
   <ul id="contactPageFormList">
      <li>
         <label for="name"><p class="contact_labels">Name:</p></label>
         <cms:input type='text' name='name' id='contact_name' required='1' />
         <cms:if k_error_name>
         <p class='contact_error'>Please provide a name.</p>
         </cms:if>   
      </li>
      <li>
         <label for="email"><p class="contact_labels">Email Address:</p></label>
         <cms:input type='text' name='email' id='contact_email' required='1' validator='email' />
         <cms:if k_error_email>
         <p class='contact_error'>Please provide a valid Email.</p>
         </cms:if>
      </li>
      <li>
         <label for="message"><p class="contact_labels">Message:</p></label>
         <cms:input type='textarea' name='message' id='contact_message' required='1' rows="20" cols="30"></cms:input>
         <cms:if k_error_message>
         <p class='contact_error'>Please enter a message.</p>
         </cms:if>
      </li>
      <li>
      <!--PHPMailer -->
         <cms:if k_success>
              <cms:php>
                  require 'PHPMailer/PHPMailerAutoload.php';
                  $mail = new PHPMailer;
                           
             $mail->isSMTP();   // Set mailer to use SMTP
             $mail->Host = 'localhost';  // Specify main and backup SMTP servers
             $mail->SMTPAuth = true;    // Enable SMTP authentication
             $mail->Username = 'admin@website.com';  // SMTP username
             $mail->Password = 'removed';   // SMTP password
             $mail-> SMTPSecure = 'tls';     // Enable TLS encryption, `ssl` also accepted
             $mail->Port = 25;   // TCP port to connect to
         
            $mail->addAddress('admin@website.com', 'Contact Form');  // Add a recipient
            $mail->addReplyTo('<cms:show frm_email />', '<cms:show frm_name />');
                             
            $mail->WordWrap = 50;    // Set word wrap to 50 characters
            $mail->isHTML(true);     // Set email format to HTML
                    
            $mail->From = '<cms:show frm_email />';
            $mail->FromName = '<cms:show frm_name />';
            $mail->Subject = 'Message from Contact Form';
            $mail->Body    = '<cms:show frm_message />';
                             
            if(!$mail->send()) {
                echo 'Message could not be sent.';
                echo 'Mailer Error: ' . $mail->ErrorInfo;
            } else {
                echo 'Message has been sent';
            }
         </cms:php>
         </cms:if>
                                 
                  
      <div id="contactSubmitWrapper">
         <input type='submit' id='send_message' class="button" value='Submit' />
      </div>
      </li>
   </ul>
</cms:form>
@ticalex, I outlined a way to debug this method -
viewtopic.php?p=16799#p16799

Have you tried doing so using incremental changes?
I just tried putting it outside of the <cms:if k_success> tag and it executed (Obviously with an error) - so what would be my next step?
From within the <cms:if> tag seems to do nothing, it won't even print an error.

Edit: As for hard-coded variables - are they hard coded to be frm_variablename - or am I getting the wrong end of the stick there?

Thanks!
Please PM me the FTP+Couch access creds for your site.
I think you could do with a helping hand :)
28 posts Page 1 of 3
cron