Problems, need help? Have a tip or advice? Post it here.
10 posts Page 1 of 1
Hello,

I've been trying to use phpMailer to send me an email when someone adds a comment.

I've done the following:

Code: Select all
<cms:if k_is_commentable >
<div class="comment-form" >
    <cms:form method="post" class="k_form">

        <cms:if k_success >
      
            <cms:process_comment />

            <cms:if k_process_comment_success>
       
            <cms:php>
            require 'PHPMailer/PHPMailerAutoload.php';
            
            $mail = new PHPMailer;
            <cms:pages masterpage="contact.php">
            $mail->isSMTP();   // Set mailer to use SMTP
            $mail->Host = '<cms:show my_host />';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;    // Enable SMTP authentication
            $mail->Username = '<cms:show my_email />';  // SMTP username
            $mail->Password = '<cms:show my_password />';   // SMTP password
            $mail-> SMTPSecure = 'ssl';     // Enable TLS encryption, `ssl` also accepted
            $mail->Port = <cms:show my_port />;   // TCP port to connect to
            
            $mail->addAddress('<cms:show my_email />', '<cms:show my_email_name />');  // Add a recipient
            </cms:pages>
            $mail->addReplyTo('<cms:show k_comment_author_email />', '<cms:show k_comment_author />');
            
            $mail->WordWrap = 50;    // Set word wrap to 50 characters
            $mail->isHTML(true);     // Set email format to HTML
            
            $mail->From = '<cms:show k_comment_author_email />';
            $mail->FromName = '<cms:show frm_name />';
            $mail->Subject = 'Un mesaj de la <cms:show k_comment_author />';
            $mail->Body    = '
   
               You have a new comment from <cms:show k_comment_author /> on <cms:show k_page_title/><br /><br />
               
               <cms:show k_comment />
               
                        ';
               
            </cms:php>
                   
                <div class="k_successmessage">
                      ...


But this is not working as expected, it does add the comment and it does echo the success message but it doesn't send the email :(
Hi Alin!

Why not use Couch native sendmail?

Replace your <cms:php></cms:php> with :
Code: Select all
<cms:send_mail from='no-reply@mysite.com' to=<cms:show my_email /> subject='You Got New Comment' debug='1'>
   ...Email Body....
</cms:send_mail>


The debug set to '1', so you will get a log file on your web root, that cointan wether the mail sent or not.
As soon as possible!

Touch me up : abada[dot]zulma[at]gmail[dot]com
GoingMarryAsap wrote: Hi Alin!

Why not use Couch native sendmail?



Hello @GoingMarryAsap,

It's not that I don't wanna use it, I can't.
Server restrictions require SMTP :( Therefore I ended up using phpMailer for all my contact forms and so on :)
Alin wrote:
GoingMarryAsap wrote: Hi Alin!

Why not use Couch native sendmail?



Hello @GoingMarryAsap,

It's not that I don't wanna use it, I can't.
Server restrictions require SMTP :( Therefore I ended up using phpMailer for all my contact forms and so on :)


Then try this Alin : viewtopic.php?f=4&t=8675&p=16793&hilit=smtp+email#p16802
As soon as possible!

Touch me up : abada[dot]zulma[at]gmail[dot]com
GoingMarryAsap wrote: Then try this Alin : viewtopic.php?f=4&t=8675&p=16793&hilit=smtp+email#p16802


Hey there @GoingMarryAsap, if you take a look at my code, it's almost the same.
Note that I am already using this for my contact forms and it's working fine, now I'm trying to add it to the comments too. This is where it fails.

The difference is that i don't have the :
Code: Select all
            if(!$mail->send()) {
                echo 'Message could not be sent.';
                echo 'Mailer Error: ' . $mail->ErrorInfo;
            } else {
                echo 'Message has been sent';
            }


..part cause I am already handling the success/error on comment submit.
@Alin, adding to what @GoingMarryAsap suggested - please try debugging the problem incrementally (e.g. as shown here -http://www.couchcms.com/forum/viewtopic.php?p=16799#p16799).

Instead of fetching values from 'contact.php' to set phpMailer's parameters, hard-code them to begin with. Try to send the mail from outside the k_success block.

In short, get to a point where you can get the mail across and then start replacing the hardcoded values with variables. This way you'll know exactly when the process fails and why.

Hope it helps.
Hi i use this script and working perfectly


<cms:php>
if($_GET['message']=='send') {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$question = $_POST['message'];

$message = "<html><body>";
$message .= "<h3><cms:show page_title /></h3>";

$message .= "<table rules='all' style='border-color: #666 !important;' cellpadding='10'>";

$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>$name</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>$email</td></tr>";
$message .= "<tr><td><strong>Subject:</strong> </td><td>$subject</td></tr>";
$message .= "<tr><td><strong>Message:</strong> </td><td>$question</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
$message .= "</body></html>";

//send item
require_once('class.phpmailer.php');
include("class.smtp.php");

/* Host */
$mailHost = "mail.yourdomain.com";
$mailAcc = "info@yourdomain.com";
$mailPass = "info2015";

/* Sender */
$clientMail = $email;
$replayto = $clientMail;
$subject = "Web Contact";
$body = $message;

/* Script */
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = $mailHost;
$mail->SMTPAuth = true;
$mail->Host = $mailHost;
$mail->Port = 587;
$mail->Username = $mailAcc;
$mail->Password = $mailPass;

$mail->SetFrom("$clientMail" , "<$email>");
$mail->Subject = $subject;
$mail->MsgHTML($body);
$mail->AddAddress($mailAcc, "<Bali Property Rentals>");
$mail->AddCC($clientMail, $name);


if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<b>Thanks for your time, your message has been sent, we will get in touch with you soon.</b><hr>";
}
}
</cms:php>
<script src="<cms:show k_site_link />js/jquery.validate.min.js"></script>
<script>
$(document).ready(function() {
$("#contactform").validate();
});
</script>

<form action="<cms:show k_site_link />contact.php?message=send" method="post" enctype="multipart/form-data" id="contactform" role="form">
<div class="form-group">
<label for="name">Name*</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter your name" required>
</div>
<div class="form-group">
<label for="email">Email address*</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" required>
</div>
<div class="form-group">
<label for="subject">Subject*</label>
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" name="message" placeholder="Your message" rows='10'></textarea>
</div>
<div class="checkbox custom-checkbox"><label><input type="checkbox" name="spam" required><span class="fa fa-check"></span> Check it to prevent spam</label></div>
<button class="btn btn-green isThemeBtn">SEND MESSAGE</button>
</form>

Attachments

KK wrote: @Alin, adding to what @GoingMarryAsap suggested - please try debugging the problem incrementally (e.g. as shown here -http://www.couchcms.com/forum/viewtopic.php?p=16799#p16799).

Instead of fetching values from 'contact.php' to set phpMailer's parameters, hard-code them to begin with. Try to send the mail from outside the k_success block.

In short, get to a point where you can get the mail across and then start replacing the hardcoded values with variables. This way you'll know exactly when the process fails and why.

Hope it helps.


@KK, apparently the problem lies within the "<cms:show k_comment_author_email />". I've narrowed it down to only this, I can send it from the k_success block and while fetching values from 'contact.php' but only if I manually add the email, it won't take the k_comment_author_email.

Is there a reason for that? I am also using this with a mailchimp code to add them to the mailing list if they comment and it's the same thing, only if I add the email manually. The email doesn't get passed while using <cms:show k_comment_author_email />.

Here's the Mailchimp code cause it's shorter:
Code: Select all
<cms:if k_is_commentable >
<div class="comment-form" >
    <cms:form method="post" class="k_form">

        <cms:if k_success >
      
            <cms:process_comment />
         
            <cms:if k_process_comment_success>
         
            <cms:php>
               <cms:pages masterpage='newsletter.php'>
                  $MailChimp = new \Drewm\MailChimp('<cms:show mc_api_key/>');
               </cms:pages>
                  $result = $MailChimp->call('lists/subscribe', array(
                  <cms:pages masterpage='newsletter.php'>
                     'id'    => '<cms:show mc_list_id/>',
                  </cms:pages>
                     'email' => array('email' => 'Manually written email')
//Doesn't work with <cms:show k_comment_author_email />
                  ));
            </cms:php>
            
                <div class="k_successmessage">
@Alin,

What seems to be happening is that the variable you are trying to use (namely 'k_comment_author_email') is simply not available at point of use (this variable gets set when you use cms:comments to loop through existing comments).

What is available at that point are the values submitted through the form.
A typical comment form would atleast have the following fields -
Code: Select all
<cms:input type="text" name="k_author" required="1"/>

<cms:input type="text" name="k_email" validator="email" required="1"/>

So, using the convention in all Couch forms, you can access the submitted values by prefixing 'frm_' to the names. I.e., frm_k_email would be the variable to use.

An easy way of knowing which variables are available at any place is to place a <cms:dump /> or <cms:dump_all /> to get a list of all variables. Please do that in place of send_mail and you'll spot the variables to use in the mail.

Hope it helps.
KK wrote: So, using the convention in all Couch forms, you can access the submitted values by prefixing 'frm_' to the names. I.e., frm_k_email would be the variable to use.


At some point I have tried using frm_email :| for a contact form it did work but I forgot about the k since the input name is k_email. Thank you !
10 posts Page 1 of 1