-2

I don't use composer.

I have a simple contact form that I want to send the text through my site's email account to gmail.

http://elcomass.com/elcomass-contact.php

I've copied codes from multiple sites and tried atleast five but none worked. Current code is the latest I tried

I tried or viewed these links, didn't help me. This is my last resort, because I've also tried tutorials on sites.

phpmailer can't send mail

phpmailer not send mail

https://github.com/PHPMailer/PHPMailer#a-simple-example

Sending mail with PHPMailer (SMTP)

Failed opening required 'PHPMailer-master/PHPMailerAutoload.php' (include_path='.:/usr/share/pear:/usr/share/php')

https://codeforgeek.com/phpmailer-ultimate-tutorial/

https://www.cloudways.com/blog/send-emails-in-php-using-phpmailer/

https://alexwebdevelop.com/phpmailer-tutorial/

https://www.inmotionhosting.com/support/email/send-email-from-a-page/using-phpmailer-to-send-mail-through-php

Currently researching: https://www.google.com/search?client=firefox-b-d&q=PHP+Fatal+error%3A++require%28%29%3A+Failed+opening+required+%27class.PHPMailer.php%27

When you submit the form, all you get is a blank page. In the error log this is the most common problem:

PHP Fatal error: Class 'PHPMailer' not found in /home/elcomass/public_html/elcomass-sendmail.php on line 6

PHP Warning: require(class.PHPMailer.php): failed to open stream: No such file or directory in /home/elcomass/public_html/elcomass-sendmail.php on line 2

[13-Jul-2019 12:27:14 UTC] PHP Fatal error: require(): Failed opening required 'class.PHPMailer.php' (include_path='.:/opt/cpanel/ea-php56/root/usr/share/pear') in /home/elcomass/public_html/elcomass-sendmail.php on line 2

I've tried making folders, manually placing phpmailer items there. As it is now, its directly placed in my public_html without a folder and it still doesn't work. Same for Exception.php, STMP.php

<?php
// This is one way I tried
use PHPMailer;
require 'PHPMailer.php';
// #2
require 'class.PHPMailer.php';
// #3
require 'PHPMailer.php';
// #4
require 'test/class.PHPMailer.php';
// #5
require 'test\class.PHPMailer.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
$subject = $_POST['subject'];
$message = $_POST['message'];
$email = $_POST['email'];
$name = $_POST['name'];

try {
    //Server settings
    $mail->SMTPDebug = 2;                                       // Enable verbose debug output
    $mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->Host       = 'xxxx';  // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'xxxxxx@xxxxxx.com';                     // SMTP username
    $mail->Password   = 'xxxx';                               // SMTP password
    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = xxx;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom($email, $name);
    $mail->addAddress('xxx@gmail.com', 'elcomass');     // Add a recipient



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

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

i want the contact form to send email to a gmail address. I have the email address specifications for the site through cPanel.

David Dr.
  • 87
  • 9
  • 1
    You need to install phpmailer using composer – executable Jul 15 '19 at 09:57
  • 1
    For sending mail you have to first configure your SMTP server for email , have you done that? – Taha Jul 15 '19 at 09:57
  • In cpanel, only option for SMTP server is email accounts. That is the account I have settings for, is that what you mean? And yes I should learn to use it. – David Dr. Jul 15 '19 at 10:01
  • I think the problem here is , PHP Mailer class is not properly included, No such file or directory in /home/elcomass/public_html/elcomass-sendmail.php on line 2 – Taha Jul 15 '19 at 10:08
  • I tried changing that now with required_once, brackets, it still throws out an error: require_once(): Failed opening required 'class.phpmailer.php' – David Dr. Jul 15 '19 at 10:16
  • 'class.PHPMailer.php' and 'elcomass-sendmail.php' , both of them are on same directory? – Taha Jul 15 '19 at 10:26
  • Ignore most tutorials; go to the source are read the readme file. It tells you exactly what you need to do, and should have been the first place you looked. – Synchro Jul 15 '19 at 10:43
  • Ok i'll check it out. – David Dr. Jul 15 '19 at 10:57

2 Answers2

0

After much work with phpmailer, I managed to solve my issues. I was doing couple of things wrong, as errors showed, but for anyone interested be careful copying details about your domain e-mail and don't call SESSION in the same page where phpmailer code is.

David Dr.
  • 87
  • 9
-1

Use this code. its work for me.

<?php
    require_once('phpmailer/class.phpmailer.php');

    function send_email($mail_information=array())
    {
        $receiver_email = $mail_information['receiver_email']; 
        $receiver_name = $mail_information['receiver_name'];
        $reply_to_email = '***@gmail.com';
        $reply_to_name = $mail_information['reply_to_name'];
        $message = $mail_information['message'];
        $subject = $mail_information['subject'];

        $from_email = $reply_to_email; 
        $from_name = "TEST";  
        $mail             = new PHPMailer();
        $body             = $message;
        $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                                   // 1 = errors and messages
                                                   // 2 = messages only
        $mail->SMTPAuth   = false;                  // enable SMTP authentication
        $mail->Host       = "localhost"; // sets the SMTP server
        $mail->Username   = "***@test.com"; // SMTP account username
        $mail->Password   = "*****";        // SMTP account password
        $mail->Port = 25; // or 587
        $mail->SMTPSecure = 'No'; // secure transfer enabled REQUIRED for GMail
        $mail->IsSMTP(); 
        $mail->IsHTML(true);
        $mail->SetFrom($from_email, $from_name);

        if($reply_to_email != '' && $reply_to_name != '')
        {
            $mail->AddReplyTo($reply_to_email, $reply_to_name);
        }

        $mail->Subject    = $subject;

        $mail->AltBody    = "To view the message, please use an HTML compatible email viewer !"; // optional, comment out and test

        $mail->MsgHTML($body);

        $address = $receiver_email;
        $mail->AddAddress($address, $receiver_email);


        if(! $mail->Send()) 
        {
            echo "<br>Mailer Error: " . $mail->ErrorInfo;
            return false;
        } 
        else 
        {

            return true;
        }   
    }
    ?>
BInjal Patel
  • 321
  • 3
  • 16