Search code examples
phpemailphpmailer

How to successfully send mail with phpmailer


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.


Solution

  • 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.