Skip to content
Advertisement

unable to use PHPMAILER to send email

I have just downloaded PHPMAILER from github to send emails to users . My problem is that when I try to run the php file with PHPMAILER from localhost I get the error

Warning: require(PHPMailerAutoload.php): failed to open stream: No such file or directory in C:xampphtdocsPROJECTHTML_FILESsendmail.php on line 3

Fatal error: require(): Failed opening required 'PHPMailerAutoload.php' (include_path='C:xamppphpPEAR') in C:xampphtdocsPROJECTHTML_FILESsendmail.php on line 3

where sendmail.php is the file with the phpmailer sample code to send an email . I believe that there’s a problem with the location of the phpmailer repo I downloaded so I will show you how my project folder is set up with the repo inside :

enter image description here

PHPMailer-5.2-... is the zip file and PHPMAILER_FOLDER is the unzipped one . I have just renamed it . The PROJECT FOLDER is the one with the long name on top and is located in xampp/htdocs My sendmail.php code (the sample code without my credentials):

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$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');

$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 = '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';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

I have also added "phpmailer/phpmailer": "~5.2" to my composer.json with the same result I would appreciate your help . Thank you in advance .

Advertisement

Answer

If your sendmail.php is in the “root folder” of your project, but PHPMailerAutoload.php is in “PHPMAILER_FOLDER” then you need to include it like this:

require 'PHPMAILER_FOLDER' . DIRECTORY_SEPARATOR . 'PHPMailerAutoload.php';

But do you even have such a file? From what I can see PHPMailer does not ship one, have you created one yourself?

You need to point to the file.

If you prefer using autoloader generated by composer then usually it creates a folder called vendor and then instead of autoloading using the autoloader from phpmailer include the one generated by composer and add the required use statements:

include 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement