SMTP error on external form

Permalink
Hi,

I've been asked to help out on a Concrete5 website that has a few errors. I am a total novice with Concrete5! The site seems to be running version 5.5.2.1. It hasnt been updated for a long while.

They have an external form which keeps producing this error when submitted: Mailer Error: SMTP Error: Could not authenticate. They have some normal forms which work fine.

I've checked in settings and the SMTP method was set to Default. I changed it to external and used our gmail details, but I still get the same error.

Can someone help?

Thank you!

 
hutman replied on at Permalink Reply
hutman
Can you share the code for the External Form controller?
whitelabel replied on at Permalink Reply
Hi,

No problem. Here it is:

<?php
defined('C5_EXECUTE') or die("Access Denied.");
class ApplyNowFormExternalFormBlockController extends BlockController {

/*
public function action_apply_now_search() {

$req = Request::get();
$this->set('response', t('Thanks!'));
return true;

}
*/
public function action_apply_now() {
$captcha = Loader::helper('validation/captcha');
$r = $captcha->check('ccmCaptchaCode');

if(!$r) {
$this->set('response', t('The CAPTCHA wasn\'t entered correctly. Please try again.!'));
return true;
}

$data = array();
foreach($_POST as $k=>$v) {
if( is_array($v) ) {
$data[$k] = array_values($v);
} else {
$data[$k] = trim($v);
}
}

// validations
$valid = array();

$section = 'Personal Details';
$i = 0;
$valid[$i]['description'] = $section;
if(!empty($data['family_name'])) {
$valid[$i]['vals']['family_name']['txt'] = $data['family_name'];
$valid[$i]['vals']['family_name']['qst'] = "Family Name";
}
if(!empty($data['title'])) {
$valid[$i]['vals']['title']['txt'] = $data['title'];
$valid[$i]['vals']['title']['qst'] = "Title";
}
if(!empty($data['first_name'])) {
$valid[$i]['vals']['first_name']['txt'] = $data['first_name'];
$valid[$i]['vals']['first_name']['qst'] = "First Name";
}
if(!empty($data['gender'])) {
$valid[$i]['vals']['gender']['txt'] = $data['gender'];
$valid[$i]['vals']['gender']['qst'] = "Gender";
}
if(!empty($data['date_of_birth'])) {
$valid[$i]['vals']['date_of_birth']['txt'] = $data['date_of_birth'];
$valid[$i]['vals']['date_of_birth']['qst'] = "Date of Birth";
}
if(!empty($data['address'])) {
$valid[$i]['vals']['address']['txt'] = $data['address'];
$valid[$i]['vals']['address']['qst'] = "Address";
}
if(!empty($data['postcode'])) {
$valid[$i]['vals']['postcode']['txt'] = $data['postcode'];
$valid[$i]['vals']['postcode']['qst'] = "Postcode";
}
if(!empty($data['town_county'])) {
$valid[$i]['vals']['town_county']['txt'] = $data['town_county'];
$valid[$i]['vals']['town_county']['qst'] = "Town / County";
}
if(isset($data['country_of_birth'])) {
$countries = array();
while(!count($countries))
$countries = file("country.csv");

//echo "<pre>"; print_r($countries); echo "</pre>"."count=|".count($countries)."|";die;

$valid[$i]['vals']['country_of_birth']['txt'] = $countries[$data['country_of_birth']];
$valid[$i]['vals']['country_of_birth']['qst'] = "Country of Birth";
unset($countries);
}
if(!empty($data['email'])) {
$valid[$i]['vals']['email']['txt'] = $data['email'];
$valid[$i]['vals']['email']['qst'] = "E-mail";
}
if(!empty($data['home_tel_no'])) {
$valid[$i]['vals']['home_tel_no']['txt'] = $data['home_tel_no'];
$valid[$i]['vals']['home_tel_no']['qst'] = "Home Tel No";
}
if(!empty($data['mobile_no'])) {
$valid[$i]['vals']['mobile_no']['txt'] = $data['mobile_no'];
$valid[$i]['vals']['mobile_no']['qst'] = "Mobile No";
}
if(!empty($data['national_insurance_no'])) {
$valid[$i]['vals']['national_insurance_no']['txt'] = $data['national_insurance_no'];
$valid[$i]['vals']['national_insurance_no']['qst'] = "National Insurance No";
}
if(!empty($data['how_did_you_hear_about_jace'])) {
$valid[$i]['vals']['how_did_you_hear_about_jace']['qst'] = "How did you hear about JACE?";

$text = "";
switch($data['how_did_you_hear_about_jace']) {
case 'word_of_mouth': $text = "Word of mouth";break;
case 'saab': $text = "Career event";break;
case 'advertising': $text = "Advertising";break;
case 'job_centre': $text = "Job Centre";break;
case 'school': $text = "School";break;
case 'friend': $text = "Friend";break;
case 'search_engine': $text = "Search engine";break;
case 'other': $text = "Other";break;
}
$valid[$i]['vals']['how_did_you_hear_about_jace']['txt'] = $text;
unset($text);
}

if(!empty($data['other_source'])) {
$valid[$i]['vals']['other_source']['qst'] = "How did you hear about JACE - Other";
$valid[$i]['vals']['other_source']['txt'] = nl2br($data['other_source']);
unset($text);
}

$section = 'Education & Training';
$i++;
$valid[$i]['description'] = $section;
if(!empty($data['name_of_school'])) {
$valid[$i]['vals']['name_of_school']['txt'] = $data['name_of_school'];
$valid[$i]['vals']['name_of_school']['qst'] = "Name of last secondary school";
}
if(!empty($data['name_of_college'])) {
$valid[$i]['vals']['name_of_college']['txt'] = $data['name_of_college'];
$valid[$i]['vals']['name_of_college']['qst'] = "Name of last college";
}
if(!empty($data['examinations'])) {
$valid[$i]['vals']['examinations']['txt'] = nl2br($data['examinations']);
$valid[$i]['vals']['examinations']['qst'] = "Examinations / Qualifications gained at school";
}

$section = 'Type of Qualifications';
$i++;
$valid[$i]['description'] = $section;
if(!empty($data['qualifications'])) {
$valid[$i]['vals']['qualifications']['txt'] = $data['qualifications'];
$valid[$i]['vals']['qualifications']['qst'] = "Qualifications";
}
if(!empty($data['other_qualifications'])) {
$valid[$i]['vals']['other_qualifications']['txt'] = $data['other_qualifications'];
$valid[$i]['vals']['other_qualifications']['qst'] = "Other Qualifications";
}

$section = 'Employment Details & Any Special Needs';
$i++;
$valid[$i]['description'] = $section;
if(isset($data['currently_employed'])) {
$valid[$i]['vals']['currently_employed']['txt'] = $data['currently_employed'] ? "yes" : "no" ;
$valid[$i]['vals']['currently_employed']['qst'] = "Are you currently employed?";
}
if(!empty($data['part_full_time'])) {
$valid[$i]['vals']['part_full_time']['qst'] = "Is your job";

$text = "";
switch($data['part_full_time']) {
case 'full_time': $text = "Full time";break;
case 'part_time': $text = "Part time";break;
case 'voluntary': $text = "Voluntary";break;
case 'work_experience': $text = "Work Experience";break;
}
$valid[$i]['vals']['part_full_time']['txt'] = $text;
unset($text);
}
if(!empty($data['employment'])) {
$valid[$i]['vals']['employment']['txt'] = $data['employment'];
$valid[$i]['vals']['employment']['qst'] = "Name and address of employment";
}

$section = 'Disclosure and Barring Service (DBS) - formerly CRB';
$i++;
$valid[$i]['description'] = $section;
if(!empty($data['criminal_records'])) {
$text = "";
switch($data['criminal_records']) {
case 'yes': $text = "Yes";break;
case 'no': $text = "No";break;
case 'pending': $text = "Pending";break;
case 'not_sure': $text = "Not Sure";break;
}
$valid[$i]['vals']['criminal_records']['txt'] = $text;
$valid[$i]['vals']['criminal_records']['qst'] = "Do you have a current Disclosure and Barring Service record (DBS)?";
}
if(!empty($data['criminal_records_date'])) {
$valid[$i]['vals']['criminal_records_date']['txt'] = $data['criminal_records_date'];
$valid[$i]['vals']['criminal_records_date']['qst'] = "What is the date?";
}

$section = 'Residency Status';
$i++;
$valid[$i]['description'] = $section;
if(isset($data['living_in_EU_state'])) {
$valid[$i]['vals']['living_in_EU_state']['txt'] = $data['living_in_EU_state'] ? "yes" : "no" ;
$valid[$i]['vals']['living_in_EU_state']['qst'] = "Have you been living in the UK or other member state of the European Union for the last 3 years?";
}

$section = 'Passport Details';
$i++;
$valid[$i]['description'] = $section;
if(!empty($data['pass_nationality'])) {
$valid[$i]['vals']['pass_nationality']['txt'] = $data['pass_nationality'];
$valid[$i]['vals']['pass_nationality']['qst'] = "Nationality";
}
if(!empty($data['pass_number'])) {
$valid[$i]['vals']['pass_number']['txt'] = $data['pass_number'];
$valid[$i]['vals']['pass_number']['qst'] = "Passport No";
}
if(!empty($data['pass_country_of_issue'])) {
$valid[$i]['vals']['pass_country_of_issue']['txt'] = $data['pass_country_of_issue'];
$valid[$i]['vals']['pass_country_of_issue']['qst'] = "Country of Issue";
}
if(!empty($data['pass_expiry_date'])) {
$valid[$i]['vals']['pass_expiry_date']['txt'] = $data['pass_expiry_date'];
$valid[$i]['vals']['pass_expiry_date']['qst'] = "Expiry date";
}
if(!empty($data['birth_certificate_number'])) {
$valid[$i]['vals']['birth_certificate_number']['txt'] = $data['birth_certificate_number'];
$valid[$i]['vals']['birth_certificate_number']['qst'] = "Birth Certificate number";
}
if(!empty($data['name_in_full'])) {
$valid[$i]['vals']['name_in_full']['txt'] = $data['name_in_full'];
$valid[$i]['vals']['name_in_full']['qst'] = "Name in full";
}

$section = 'Type of Qualifications';
$i++;
$valid[$i]['description'] = $section;
if(!empty($data['programmes'])) {
$valid[$i]['vals']['programmes']['txt'] = $data['programmes'];
$valid[$i]['vals']['programmes']['qst'] = "Programmes are you interested";
}
include_once 'blocks/external_form/forms/vocational_levels.php';
if(!empty($data['level1'])) {
$tmp = array();
foreach( $data['level1'] as $key ){
$tmp[] = $programmes['level1'][$key];
}
$valid[$i]['vals']['programmes_level1']['txt'] = implode(', ', $tmp);
$valid[$i]['vals']['programmes_level1']['qst'] = "Vocational Area's Level 1";
}
if(!empty($data['level2'])) {
$tmp = array();
foreach( $data['level2'] as $key ){
$tmp[] = $programmes['level2'][$key];
}
$valid[$i]['vals']['programmes_level2']['txt'] = implode(', ', $tmp);
$valid[$i]['vals']['programmes_level2']['qst'] = "Vocational Areas Level 2 or 3";
}
if(!empty($data['start_date_required'])) {
$valid[$i]['vals']['start_date_required']['txt'] = $data['start_date_required'];
$valid[$i]['vals']['start_date_required']['qst'] = "Approx start date required";
}
if(!empty($data['special_needs'])) {
$valid[$i]['vals']['special_needs']['txt'] = nl2br($data['special_needs']);
$valid[$i]['vals']['special_needs']['qst'] = "Do you have any special or specific needs (e.g. learning, health, physical, carer responsibilities)?";
}

if(!empty($data['contact_method'])) {
$valid[$i]['vals']['contact_method']['txt'] = $data['contact_method'];
$valid[$i]['vals']['contact_method']['qst'] = "Contact method";
}

// Upload file
if ( isset($_FILES['cvToUpload']['name']) && !empty($_FILES['cvToUpload']['name']) ) {

$uploadOk = false;
$target_dir = getcwd() . "/files/cv/";
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
$file_name = uniqid() . '_' . basename($_FILES["cvToUpload"]["name"]);
$file_url = 'http://'.$_SERVER['HTTP_HOST'].'/files/cv/'.$file_name;
$target_file = $target_dir . $file_name;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if ($imageFileType != "pdf" && $imageFileType != "doc" && $imageFileType != "docx" && $imageFileType != "jpg" && $imageFileType != "jpeg" && $imageFileType != "png") {
$this->set('response', t('Sorry, only PDF, DOC, DOCX, PNG & JPG files are allowed.'));
return true;
}


if (move_uploaded_file($_FILES["cvToUpload"]["tmp_name"], $target_file)) {
$uploadOk = true;
} else {
$uploadOk = false;
}

if (!$uploadOk) {
$this->set('response', t('Sorry, there was an error uploading your file.'));
return true;
}
}
// END OF validations

// keep in db
$db = Loader::db();
$chq = $db->query("INSERT INTO tbl_apply_now (`details`,`date`) VALUES ('".mysql_real_escape_string(serialize($valid))."',NOW())");
$id = $db->Insert_ID();
// END OF keep in db

// prepare the text
$str = "<html><head></head><body><h3>Form submitted on ".date('jS F Y, l')."</h3>";
$str .= "<h3>Application No: AP-".$id."</h3>";
$str .= "<table cellspacing='0' cellpadding='5' border='1' width='600px'>";
foreach($valid as $id=>$vals) {
if(isset($vals['vals']) && !empty($vals['vals'])) {
$str .= '<tr><td colspan="2"><h2 style="margin-bottom: 5px;">'.htmlentities($vals['description']).'</h2></td></tr>';
foreach($vals['vals'] as $item) {
$str .= '<tr><td width="40%" style="font-weight:bold;">'.htmlentities($item['qst']).'</td><td>'.htmlentities($item['txt']).'</td></tr>';
}
}
}
$str .= '<tr><td colspan="2"><h2 style="margin-bottom: 5px;">Uploads</h2></td></tr>';
$str .= '<tr><td width="40%" style="font-weight:bold;">CV</td><td><a target="_blank" href="'.$file_url.'">'.$file_name.'</a></td></tr>';
$str .= "</table></body></html>";

// END OF prepare the text

require_once( dirname(__FILE__) . '/../../../../libraries/phpmailer/class.phpmailer.php' );

$mail = new PHPMailer();

$params = array(
/* Email SMTP settings */
'smtp' => array(
/* if enabled => false, you can leave the rest empty */
'enabled' => true,
'host' => 'smtp.websolved.co.uk',
'port' => 25,
'username' => 'jace.training.web.application@websolved.co.uk',
'password' => '4KfiSHtL',
'ssl' => false,
'debug' => false
),
'send_to' => array(
/* Accepts multiple recepients */
/*'Jeffrey Murdock' => 'u.goran@gmail.com',
'Spela' => 'spela@websolve.co.uk',*/
'Client' => EMAIL_APPLY_NOW
),
'send_from' => array(
'name' => 'Web Application Form ' . $_SERVER['HTTP_HOST'],
'email' => 'jace.training.web.application@websolved.co.uk',
),
'subject' => "Application Form",
/* Alt message in case user looks in non html reader */
'alt_message' => "To view the message, please use an HTML compatible email viewer!",
'ok_message' => "Thank you for submitting your application!",
'error_message' => "Mailer Error: ",
/* set to true to spit out mailer data */
'debug' => false
);

if( $params['smtp']['enabled'] === true ){
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = $params['smtp']['host'];
$mail->Port = $params['smtp']['port'];
$mail->Username = $params['smtp']['username'];
$mail->Password = $params['smtp']['password'];
if( $params['smtp']['ssl'] === true ){
$mail->SMTPSecure = "ssl";
}
if( $params['smtp']['debug'] === true ){
//1 = errors and messages
//2 = messages only
$mail->SMTPDebug = 1;
}
}

$mail->Subject = $params['subject'];
$mail->AltBody = $params['alt_message'];

$mail->SetFrom(
$params['send_from']['email'],
$params['send_from']['name']
);

$mail->MsgHTML($str);

foreach( $params['send_to'] as $name => $address )
{
$mail->AddAddress($address, $name);
}
#$mail->AddAddress('tt3@websolved.co.uk', 'Websolve Backup');

if(!$mail->Send())
{
//echo $mail->ErrorInfo;
$this->set('response', $params['error_message']. $mail->ErrorInfo);
unset($_POST);
return false;
}
else
{
// send confirmation email
$mail->ClearAllRecipients();
$mail->Subject = 'Copy of Application Form';
$mail->AddAddress($data['email'], $data['name']);

@$mail->Send();

$this->set('response', $params['ok_message']);
unset($_POST);
return true;
}



/*
// send mail
$mh = Loader::helper('mail');

//Get Zend Mailer Object
$zmo = $mh->getMailerObject();
$zm = $zmo['mail'];
$zm->setSubject("Application Form");
$zm->setBodyHtml($str);
$zm->addTo(EMAIL_APPLY_NOW);

//These settings are in config/base.php
$zm->setFrom($data['email'], $data['name']);
$zm->send();
// END OF send mail
*/
/*
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

// Additional headers
$headers .= 'To: '.EMAIL_APPLY_NOW . "\r\n";
$headers .= "From: webapplication@jace-training.co.uk\r\n";

$out = mail(EMAIL_APPLY_NOW, "Application Form", $str, $headers);
####TEST###$out = mail('Goran <u.goran@gmail.com>', "Application Form", $str, $headers);

// send confirmation email:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
// Additional headers
$headers .= 'To: '.$data['name'].' <'.$data['email'] . ">\r\n";
$headers .= 'From: Jace Training '.EMAIL_APPLY_NOW . "\r\n";
mail($data['name'].' <'.$data['email'] . '>', "Copy of Application Form", $str, $headers);

$this->set('response', t('Thank you for submitting your application!'));
unset($_POST);
return true;
*/

}

}
hutman replied on at Permalink Reply
hutman
Try changing your mail stuff to use this

Loader::helper('mail');
$mh = new MailHelper();
$mh->from('jace.training.web.application@websolved.co.uk', t('Web Application Form'));
foreach( $params['send_to'] as $name => $address ){
   $mh->to($address, $name);
}
$mh->setSubject('Application Form');
$mh->setBodyHTML($str);
$mh->sendMail();
$mh2 = new MailHelper();
$mh2->from($data['email'], $data['name']);
$mh2->to(EMAIL_APPLY_NOW);
$mh2->setSubject('Application Form');
$mh2->setBodyHTML($str);
$mh2->sendMail();

That will use the built in C5 mail helper, which the standard forms use
whitelabel replied on at Permalink Reply
Hi,

Thanks for your reply. I've replaced the mail section in the controller php file for this form, but no joy. I still get the SMTP error.

I think it might need a Concrete5 expert to take a look at it!
hutman replied on at Permalink Reply
hutman
Can you show your code after the update you made?
whitelabel replied on at Permalink Reply
Sure! I noticed that the mail section in the original php was commented out? On this one I removed the comments lines.

<?php 
defined('C5_EXECUTE') or die("Access Denied.");
class ApplyNowFormExternalFormBlockController extends BlockController {
/*
    public function action_apply_now_search() {
        $req = Request::get();
        $this->set('response', t('Thanks!'));
        return true;
    }
*/    
    public function action_apply_now() {
        $captcha = Loader::helper('validation/captcha');
        $r = $captcha->check('ccmCaptchaCode');
        if(!$r) {
            $this->set('response', t('The CAPTCHA wasn\'t entered correctly. Please try again.!'));
hutman replied on at Permalink Reply
hutman
Ok, not everything that needed to be removed got removed, it should look like this

<?php 
defined('C5_EXECUTE') or die("Access Denied.");
class ApplyNowFormExternalFormBlockController extends BlockController {
/*
    public function action_apply_now_search() {
        $req = Request::get();
        $this->set('response', t('Thanks!'));
        return true;
    }
*/    
    public function action_apply_now() {
        $captcha = Loader::helper('validation/captcha');
        $r = $captcha->check('ccmCaptchaCode');
        if(!$r) {
            $this->set('response', t('The CAPTCHA wasn\'t entered correctly. Please try again.!'));
whitelabel replied on at Permalink Reply
Hi,

Thanks, I've replaced the file with the version and the error has gone. The page refreshes once I click submit. I have just asked the client if they are receiving the email or not. I cant seem to find the form under dashboard > form results?

Thanks for your help so far! Really appreciate it!
hutman replied on at Permalink Reply
hutman
You won't see the results of this form there, that is only for forms created using the "form" block, not the "external form" block. You can look under Dashboard -> Reports -> Logs and see if the email sent there (it won't show the content of the email).
whitelabel replied on at Permalink Reply
Oh I see. I've checked there and there isnt anything. Just logs from the main contact form. Nothing today and I've tried twice.
hutman replied on at Permalink Reply
hutman
And you are 100% sure that this controller is being hit?
hutman replied on at Permalink Reply
hutman
I moved this code to an editor and I see a couple things I didn't get right, these lines
foreach( $params['send_to'] as $name => $address ){
    $mh->to($address, $name);
 }

Need to be changed to
$mh->to(EMAIL_APPLY_NOW, 'Client');

Because we removed $params as that was part of the block throwing errors

And
$this->set('response', $params['ok_message']);

Should be
$this->set('response', 'Thank you for submitting your application!');

And comment back in the second mail block that I commented out, that does need to be there
whitelabel replied on at Permalink Reply
Hi,

A think you've cracked it! Just made those changes. I've got the confirmation message on the form and have checked the logs and there is a record for the submission! I'll get the client to confirm but it seems to be working.

Thank you so much! Really appreciate the help and time you have put in to solving this.