1

How to install phpMailer in a shared hosting environment?

Seen this How to install phpMailer in a shared hosting environment? but I don't understand what does "include the main file with this line:" means and the next part too "After that, you will need an external SMTP account, like gmail.com for example. Here is a working example of PHPMailer with GMAIL:"

Thanks in advance Athlios

This is the send_form.php

<?php

$formid = $_POST['contactform'];

$email_to = "info@a-wd.eu";

$fullname = $_POST['fullname']; // required
$email_from = $_POST['email']; // required
$subject = $_POST['subject']; // required
//$subjectselect = $_POST('subject').value();
$message = $_POST['message']; // required

echo($email_from);

$email_message = "Submission details below.\n\n";
$email_message .= "Fullname: ".clean_string($fullname)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Whats this about: ".clean_string($subject)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_message = wordwrap($email_message, 70, "\r\n");

$headers = 'From: '.$email_from."\r\n".
            'Reply-To: '.$email_from."\r\n" .
            'X-Mailer: PHP/' . phpversion();

mail($email_to, $email_subject, $email_message,$headers) or die("Error!");

echo "Thank you for contacting us. We will be in touch with you very soon. \r\n";
?>

This is the page

<div class="container-fluid">
      <div class="row">
            <div class="col-12">
        <div class="container contact-form-container">
      <div class="row">
        <div class="col-md-12">
          <form action="send_form.php" id="contactform" name="contactform" method="post" data-parsley-validate>
            <div class="row">
            <!-- <span class="required-key">Fields marked with a <span class="label-required">*</span> are required.</span> -->
            <div class="form-group col-lg-12 col-12">
              <label for="fullname">Name <span>(Required)</span>:</label>
              <input type="text" name="fullname" data-parsley-trigger="focusin focusout" required data-parsley-required="true" data-parsley-errors-messages-disabled />
            </div>
            <div class="form-group col-lg-12 col-12">
              <label for="email">Email <span>(Required)</span>:</label>
              <input type="email" data-parsley-type="email" name="email" data-parsley-trigger="focusin focusout" required data-parsley-required="true" data-parsley-errors-messages-disabled />
            </div>
            <div class="form-group col-lg-12 col-12">
              <label for="subject">Subject <span>(Required)</span>:</label>
              <select name="subject" required data-parsley-required="true" data-parsley-errors-messages-disabled>
                <option value="question">General Question</option>
                <option value="quote">Request a Quote</option>
                <option value="sponsorship">Sponsorship</option>
                <option value="other">Other</option>
              </select>
            </div>
            <div class="form-group col-lg-12 col-12">
              <label for="message">Message <span>(Required)</span>:</label>
              <textarea name="message" data-parsley-trigger="focusin focusout" data-parsley-minlength="20" data-parsley-maxlength="1000" data-parsley-validation-threshold="10" data-parsley-minlength-message="Minimum message length is 20 characters" data-parsley-maxlength-message="Maximum message length is 1000 characters" data-parsley-required="true"></textarea>
            </div>
            <div class="form-group col-lg-12 col-12">
                <label for="message">Captcha <span>(Required)</span>:</label>
                <script src='https://www.google.com/recaptcha/api.js'></script>
                <div class="g-recaptcha" data-sitekey="6LeAAUkUAAAAAJeW7fjroLKFkYtETHvXGgflK49u"></div>
            </div>
            <div class="form-group col-lg-12 col-12">
              <button class="btn-contact" name="send" type="submit">Send Message <i class="fa fa-paper-plane" aria-hidden="true"></i></button>
            </div>
          </form>
          </div>
        </div>
      </div>
        </div>
             </div>
          </div>
        </div>

1 Answers1

4

Go to the PHPMailer github, click the green "Clone or Download" button, and click "Download ZIP". On your local computer, unarchive the ZIP file and upload the PHPMailer-master folder to your shared server's public_html directory.

Now, wherever you want to use PHPMailer, include the parts you need:

<?php
require '/path/to/PHPMailer-master/src/PHPMailer.php'; // Only file you REALLY need
require '/path/to/PHPMailer-master/src/Exception.php'; // If you want to debug
?>

You don't need an external SMTP account for this to work, as noted in the PHPMailer documentation. So in a script where you're sending out an email, your code should look something like this:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer-master/src/PHPMailer.php'; // Only file you REALLY need
require 'PHPMailer-master/src/Exception.php'; // If you want to debug


$mail = new PHPMailer(true);                              // Passing `true` enables exceptions

try {

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>

This should be all you need to get started using PHPMailer on a shared server. Check out the project's README and look at some examples for a better understanding of all the awesomeness you get with this library.

Update for OP's code

Put the include code at the top of send_post.php:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer-master/src/PHPMailer.php'; // Only file you REALLY need
require 'PHPMailer-master/src/Exception.php'; // If you want to debug

// Form details
$formid = $_POST['contactform'];

$email_to = "info@a-wd.eu";

$fullname = $_POST['fullname']; // required
$email_from = $_POST['email']; // required
$subject = $_POST['subject']; // required
$message = $_POST['message']; // required

$email_message = "Submission details below.\n\n";
$email_message .= "Fullname: ".clean_string($fullname)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Whats this about: ".clean_string($subject)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_message = wordwrap($email_message, 70, "\r\n");

// No need to set headers here

// Replace the mail() function with PHPMailer

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions

try {

    //Recipients
    $mail->setFrom($email_from, 'From Name');
    $mail->addAddress($email_to, $fullname);     // Add the recipient

    //Content
    $mail->isHTML(true);                         // Set email format to HTML
    $mail->Subject = $subject;
    $mail->Body    = $email_message;

    $mail->send();
    echo "Thank you for contacting us. We will be in touch with you very soon. \r\n";
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
  • Nice answer! I’m glad *someone* is paying attention! – Synchro Mar 08 '18 at 01:58
  • Thank you for your answer. I'm still trying though. You say "in your code include". I don't understand, in my code where? – Andreas Stavropoulos Mar 09 '18 at 11:05
  • @AndreasStavropoulos No problem. Basically, wherever you're going to use PHPMailer (to send an email), you just need to include/require the files. If you give an example of your code in you question, we can be more specifically helpful. – John-Michael L'Allier Mar 09 '18 at 14:14
  • I added what I have so far – Andreas Stavropoulos Mar 09 '18 at 19:32
  • @John-MichaelL'Allier Thank you for the help. I've updated the send_form.php, sending the form does point there, but it only shows a blank white screen and doesn't send an email. – Andreas Stavropoulos Mar 10 '18 at 14:41
  • @AndreasStavropoulos Did you upload the PHPMailer files and include the right directories? put “ini_set(‘display_errors’, true);” on the first line of send_form.php to see the errors. – John-Michael L'Allier Mar 10 '18 at 16:46
  • @John-MichaelL'Allier 1 Fatal error: Uncaught Error: Call to undefined function clean_string() in /var/www/vhosts/gordian-knot.eu/a-wd.eu/wp-content/themes/bizplan/send_form.php:27 Stack trace: #0 {main} thrown in /var/www/vhosts/gordian-knot.eu/a-wd.eu/wp-content/themes/bizplan/send_form.php on line 27 – Andreas Stavropoulos Mar 10 '18 at 22:40
  • @John-MichaelL'Allier This is line 27 $email_message .= "Fullname: ".clean_string($fullname)."\n"; – Andreas Stavropoulos Mar 10 '18 at 22:41
  • @AndreasStavropoulos it looks like you don’t have a “clean_string()” function – John-Michael L'Allier Mar 10 '18 at 23:03
  • @John-MichaelL'Allier Yea. I got this code from a post while I was looking how to create the sendform.php from a guide regarding that on phpmailer search. – Andreas Stavropoulos Mar 12 '18 at 00:38
  • @AndreasStavropoulos gotcha. Remove that and it should work. If it does, please mark my answer as the accepted answer. – John-Michael L'Allier Mar 12 '18 at 01:27
  • @John-MichaelL'Allier I did, but as I don't have rep it wont record it. I still have some minor issues regarding sending the content of the form, but I think I have to dig a bit on my own as I'm currently learning. Need to practice some too. Thank you very much for the help. – Andreas Stavropoulos Mar 12 '18 at 23:47
  • 1
    Thanks for pointing me in the right direction. I needed to add `use PHPMailer\PHPMailer\SMTP; require 'PHPMailer-master/src/SMTP.php';` before it worked for me. – Velojet May 07 '19 at 08:25