0

I'm trying to set up SMTP for my custom contact form.

I have placed the latest PHPMailer folder in my theme folder.

I created a page template for Contact Form and added the code for the contact form in it and below it I added the SMTP code like below,

<?php
                require("PHPMailer/class.PHPMailer.php");
                require 'PHPMailer/PHPMailerAutoload.php';
                $mail = new PHPMailer;
                //$mail->SMTPDebug = 3; // Enable verbose debug output
                $mail->isSMTP(); // Set mailer to use SMTP
                $mail->Host = 'localhost'; // Specify main and backup SMTP servers
                $mail->SMTPAuth = false; // Enable SMTP authentication
                //$mail->Username = ''; // SMTP username
                //$mail->Password = ''; // SMTP password
                //$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
                $mail->Port = 25; // TCP port to connect to
                $mail->setFrom($email, $sendername);
                $mail->addAddress('test@gmail.com', 'Joe User'); // Add a recipient
                //$mail->addAddress('ellen@example.com'); // Name is optional
                $mail->addReplyTo($email, $sendername);
                $mail->addCC('test@testmail.com');
                //$mail->addBCC('bcc@example.com');
                //$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
                //$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
                $mail->isHTML(true); // Set email format to HTML
                $mail->Subject = 'Test Mail';
                $comment = $comment . "\r\n<br/>" . "<br/>\r\nIP Address: " . $id . "<br/>\r\nUser Agent: " . $browser . "<br/>\r\nUser Agent: " . "<br/>\r\nReferrer: " . $referrer . "<br/>\r\nCity: " . $city . "<br/>\r\nRegion: " . $region . "<br/>\r\nCountry: " . $country . "<br/>\r\nLatitude: " . $latitude . "<br/>\r\nLongitude: " . $longitude;
                $mail->Body    = $comment;
                $mail->AltBody = $comment;
                if($sendernameErrFlag == 0 && $emailErrFlag == 0 && $subjectErrFlag == 0 && $commentErrFlag == 0) {
                    $mail->send();
                    echo "hi";
                    $pagelink = the_permalink();
                    echo '<div id="popup1" class="overlay">
                        <div class="popup">
                            <a class="close" href="' . $pagelink . '">&times;</a>
                            <div class="content">
                                Message sent successfully!!!
                            </div>
                        </div>
                    </div>';
                    //echo '<script>alert("Message sent successfully!!!");</script>';
                } else {
                    $sendernameErrFlag = $emailErrFlag = $subjectErrFlag = $commentErrFlag = 2;
                }
            ?>

But when I submit the contact form, wordpress stops executing code when it reaches the line with the require() is written. Any and all code writting below it is not executed.

If I place the require() above the form then the form also doesn't display in the page. If I place it in the header the whole page doesn't display.

What seems to be the problem here? Why is WordPress not accepting and executing the require()?

0

I noticed that require(), require_once(), include() and include_once() methods were somehow not working. The files that were called using these methods were not being called and the code stopped executing forward from this point.

After a lot of Googling and R&D I didn't find any solution to making the above methods work.

So I decided to look into the architecture of the tweentysixteen theme and found out that there was a built in function of WordPress that it used to include other PHP files.

So I thought of trying that out to include the class.PHPMailer.php and PHPMailerAutoload.php files. If changed the lines

require("PHPMailer/class.PHPMailer.php");
require 'PHPMailer/PHPMailerAutoload.php';

in my code to,

get_template_part('PHPMailer/class.PHPMailer');
get_template_part('PPHPMailer/PHPMailerAutoload');

and finally I was able to fix the issue and the contact form started working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.