PHPMailer + HTML2PDF: pdf invalid and pj - php

Good morning all ,
I would like to attach a PDF created following a form with HTML2PDF then send it with PHMailer.
Everything works, the email goes well, I managed to create the PDF by saving it on my hard drive.
But when I try to attach it to my email, the pdf is fine in pj but I can't open it.
enter image description here
It is the same size of my locally created pdf.
I followed the tutorial and wiki of the two libraries, well I think ^^
Here is my PHPMailer code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require './vendor/phpmailer/phpmailer/src/Exception.php';
require './vendor/phpmailer/phpmailer/src/PHPMailer.php';
require './vendor/phpmailer/phpmailer/src/SMTP.php';
require './vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = '******'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '*****'; // SMTP username
$mail->Password = '*****'; // SMTP password
//$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = ****; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('****#****.com', '****');
$mail->addAddress($to); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo($shop_email, 'Votre magasin');
//$mail->addCC('cc#example.com');
$mail->addBCC($shop_email);
// Attachments
$mail->addStringAttachment($pdf_done, 'myPdf.pdf'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = strip_tags($message);
$mail->send();
echo '';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
and here is the code for HTML2PDF:
ob_start();
// --> mon code HTML de creeation du PDF
$content = ob_get_clean();
require_once dirname(__FILE__).'/../vendor/autoload.php';
use Spipu\Html2Pdf\Html2Pdf;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Exception\ExceptionFormatter;
try{
$pdf = new \Spipu\Html2Pdf\Html2Pdf('P', 'A4', 'fr');
$pdf->writeHTML($content);
$pdf_done = $pdf->output('myPdf.pdf', 'S');
}catch(\Spipu\Html2Pdf\Exception\Html2PdfException $e){
die($e);
}
?>
I tried adding ", 'base64', 'application / pdf'" in the addStringAttachment but it doesn't change anything.
Do you have any idea of my mistake?
Thank you all

Debug one thing at a time. First download the PDF manually and make sure it looks correct – if the PDF is bad to start with, emailing it isn't going to improve things. Next check that it is readable from PHP – for example serve it using readfile(). Then when you try to attach it, check the return value of your call to addAttachment() like this:
if (!$mail->addAttachment('/path/to/myPdf.pdf')) {
die('could not read PDF');
}
Also note the difference between addAttachment and addStringAttachment; you're adding a file that's been saved to disk, so you want the first one, not the second.

Related

Hiding php codes from file_get_contents method

I am sending email with PHPMailer. My email design is available in the template.php file. I am pulling the contents of the template.php file with the file_get_contents method in my mail.php file. But I have such a problem, all my php codes in the template.php file are also visible. How can I hide them?
Mail.php
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require_once 'assets/standart/db.php';
require 'assets/class/all.php';
$connect = new baglan();
$settings = new ayarlar();
// SMTP settings connect
$connect->connect('settings_smtp', '', '', 0);
$smtp_settings = $connect->connect->fetch(PDO::FETCH_ASSOC);
$smtp_mail = $smtp_settings['mail'];
// /SMTP settings connect
$template_file = 'PHPMailer/template.php';
if(file_exists($template_file)){
$mail_template = htmlspecialchars(file_get_contents($template_file));
}else{
die("Unable to locate the template file");
}
//Load Composer's autoloader
// require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = $smtp_settings['host'];
$mail->SMTPAuth = true;
$mail->Username = $smtp_settings['mail'];
$mail->Password = $smtp_settings['password'];
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = $smtp_settings['port'];
//Recipients
$mail->setFrom("$smtp_mail", 'Mailer');
$mail->addAddress('example#gmail.com', '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);
$mail->CharSet = 'UTF-8';
$mail->Subject = 'Here is the subject';
$mail->Body = $mail_template;
$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}";
}
Template.php
<?php
echo $forexample = 'Some words here';
?>
Don’t use file_get_contents for that, use include with output buffering. That way it will run the PHP code and you’ll see the results of its execution instead of the code itself.
You have to run the template file, not simply read it.
You can do that like this:
ob_start();
include ( $template_file );
$mail_template = ob_get_clean();
ob_start() (output-buffer start) causes all php script output (from echo etc) to go into an output buffer. output_get_clean() retrieves that output.
With
$mail_template = htmlspecialchars(file_get_contents($template_file));
you will just get the contents of the file into the variable $mail_template
You could use the eval() function as described here: https://www.php.net/manual/en/function.eval.php
But be very careful using it!
Caution: The eval() language construct is very dangerous because it allows
execution of arbitrary PHP code. Its use thus is discouraged. If you
have carefully verified that there is no other option than to use this
construct, pay special attention not to pass any user provided data
into it without properly validating it beforehand.
You could use it like this:
$mail_template_php = htmlspecialchars(file_get_contents($template_file));
$mail_template = eval($mail_template_php);

Is PHPmailer from github only used for your local host?

My PHPmailer from Github does work on my local host but not on my Firebase hosted website. When I submit on my website it downloads the file. Now how to solve it? Do I have to put some extra code in? Someone experienced with Firebase?
<?php
// get variables from the form
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Load Composer's autoloader
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try{
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example#gmail.com'; // SMTP username
$mail->Password = '°°°°°°°'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
// Sender
$mail->setFrom($email, $name);
// Recipients
$mail->addAddress('example#gmail.com', 'Luk Ramon'); // Add a recipient
// Body content
$body = "<p>You received an email from your website <br>name:<strong>".$name." </strong><br>subject: <strong>".$subject."</strong><br>message:<br><i>".$message."</i></p> Contact back on ".$email;
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Company-name message from '.$name;
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Firebase hosting does not support PHP which is why the file is being downloaded.
You'd need to write this in Javascript to use with Firebase - https://firebase.google.com/docs/hosting/functions

I have converted HTML to PDF but struggling to send the PDF with PHPmailer

I have managed to generate pdf from my HTML page but unable to send the pdf as an attachment using phpmailer. Please, see my code below. What am I missing?
Key points:
The html(tractpage2.php) renders well to PDF
PHPmailer works but it only sends the $message without the attachment.
Problem is that the pdf does not attach the mail
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/Exception.php';
//reference the dompdf namescape
use Dompdf\Dompdf;
//initialise dompdf class
// get/setup the htmlpage
ob_start();
require("tractpage2.php"); // The html document
$page = ob_get_contents();
ob_end_clean();
// convert to pdf
$document = new Dompdf();
$document->set_option('defaultFont', 'Courier');
$document->load_html($page);
// set paper orientation
$document->set_paper('A4', 'portrait');
// Render the HTML as PDF
$document->render();
// Output the generated PDF to Browser
$document->stream("contract.pdf", array("Attachment"=>0));
//1 = download
//0= preview
$fileupload = $document->output();
// setup email
$message = "Am new to programming and loving it";
$mail = new PHPMailer;
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxx#gmail.com'; // SMTP username
$mail->Password = 'xxxxxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('xxxxx#gmail.com', 'james');
$mail->addAddress('xxxx#aol.com', 'name of receiver'); // Add a recipient
$mail->addAddress('xxxxx#yahoo.com'); // Name is optional
//Attachments
$mail->addAttachment($fileupload); // Add attachments
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = $message;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if ($mail->send()){
echo 'Message has been sent';}
else { echo 'Message could not be sent.';}
?>
You would want to create your file on server and save and then pass absolute path of pdf file to addAttachment or alternatively you can also directly attach the DomPDF output without creating a file, like so:
$mail->addAttachment($fileupload,'application/pdf','output.pdf', false);

PHP - Dompdf and PHPMailer unexpected behavior

I am working on a web application using PHP as a server-side and a jQuery framework as a client-side.
One of the scenarios of the application is to send an email with pdf file attached I am doing this using PHPMailer and Dompdf libraries.
I had created a function named sendMail in a file name send.php accept 4 params (receiver email,subject,data,email number) the last param is because I have 5 different emails may be sent depending on the situation and data param is the data will be rendered in the html email body.
The problem is when I call the function from send.php it work as expected the email sent and pdf file created and attached to the email .
but when I require send.php in any other file and call sendMail function I get the email only without any pdf file and the file not even generated or saved on the server.
send.php
<?php
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
require 'PHPMailerAutoload.php';
$body = "test message";
sendMail('peter#w34.co','MLS mail',$body,5);
function sendMail($email,$subject,$body,$index){
$mail = new PHPMailer;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // SMTP authentication
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 587; // SMTP Port
$mail->Username = "peterwilson.intake34#gmail.com"; // SMTP account username
$mail->Password = "Password"; // SMTP account password // TCP port to connect to
$mail->From = 'peter#example.com';
$mail->FromName = 'Wilson';
$mail->addAddress($email); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body=$body;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($body);
// Render the HTML as PDF
$dompdf->render();
$output = $dompdf->output();
$file_to_save= "../work-orderes/file.pdf";
file_put_contents($file_to_save, $output);
$mail->addAttachment('../work-orderes/file.pdf');
if(!$mail->send()) {
// echo $mail->ErrorInfo;
} else {
return 1;
}
}
?>
save.php
<?php
require("sendSmtp/send.php");
session_start();
$body = "Hello World!";
sendMail('peter#w34.co','MLS mail',$body,5);
?>
Any suggestions about why Dompdf not works when calling it from any other file ??
What I have tried
removing dompdf and reinstall the latest stable version from
here
cleaning all code and just leave sendMail function and calling it
try to write the code from the beginnig
I solved it finally!
after debugging I found that everything going fine till $output = $dompdf->output(); after that I get an error on save the file using $file_to_save= "../work-orderes/file.pdf";
this work properly when I call the function sendMail() from send.php but when I call it from another file I get an error regarding accessing the path.
The Answer here that I should use the absolute path not the relative one like below
$file_to_save= $_SERVER['DOCUMENT_ROOT']."\work-orderes/file.pdf";
now the file saved successfully and attached to the email like expected!

Fatal error: Class 'PHPMailer' not found

I tried :include_once('C:\Inetpub\wwwroot\php\PHPMailer\PHPMailerAutoload.php');
Fatal error: Class 'PHPMailer' not found in C:\Inetpub\wwwroot\php\index.php on line 151
I place the PHPMailerAutoload.php in the same directory as my script.
Can someone help me with this ?
all answers are outdated now. Most current version (as of Feb 2018) does not have autoload anymore, and PHPMailer should be initialized as follows:
<?php
require("/home/site/libs/PHPMailer-master/src/PHPMailer.php");
require("/home/site/libs/PHPMailer-master/src/SMTP.php");
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxx";
$mail->Password = "xxxx";
$mail->SetFrom("xxxxxx#xxxxx.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("xxxxxx#xxxxx.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
This answers in an extension to what avs099 has given above, for those who are still having problems:
1.Makesure that you have php_openssl.dll installed(else find it online and install it);
2.Go to your php.ini; find extension=php_openssl.dll enable it/uncomment
3.Go to github and downland the latetest version :6.0 at this time.
4.Extract the master copy into the path that works better for you(I recommend the same directory as the calling file)
Now copy this code into your foo-mailer.php and render it with your gmail stmp authentications.
require("/PHPMailer-master/src/PHPMailer.php");
require("/PHPMailer-master/src/SMTP.php");
require("/PHPMailer-master/src/Exception.php");
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 1;
$mail->Port = 465 ; //465 or 587
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->IsHTML(true);
//Authentication
$mail->Username = "foo#gmail.com";
$mail->Password = "*******";
//Set Params
$mail->SetFrom("foo#gmail.com");
$mail->AddAddress("bar#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
Disclaimer:The original owner of the code above is avs099 with just my little input.
Take note of the additional:
a) (PHPMailer\PHPMailer) namespace:needed for name conflict resolution.
b) The (require("/PHPMailer-master/src/Exception.php");):It was missing in avs099's code thus the problem encountered by aProgger,you need that line to tell the mailer class where the Exception class is located.
Doesn't sound like all the files needed to use that class are present. I would start over:
Download the package from https://github.com/PHPMailer/PHPMailer by clicking on the "Download ZIP" button on the far lower right of the page.
extract the zip file
upload the language folder, class.phpmailer.php, class.pop3.php, class.smtp.php, and PHPMailerAutoload.php all into the same directory on your server, I like to create a directory on the server called phpmailer to place all of these into.
Include the class in your PHP project: require_once('phpmailer/PHPMailerAutoload.php');
As of 2021-07, and PHPMailer 6.5.0, without composer you need to do your includes in the following order:
$rdir = str_replace("\\", "/", __DIR__); //Root Dir
require $rdir.'/PHPMailer/src/Exception.php';
require $rdir.'/PHPMailer/src/PHPMailer.php';
require $rdir.'/PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
You may need to tweak based on your directory structure.
The rest of the code works as expected.
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.yourserver.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'no-reply#example.com'; //SMTP username
$mail->Password = 'password'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('mail#example.com');
$mail->addAddress('mail#example.com', 'Joe User'); //Add a recipient
$mail->addAddress('mail#example.com', 'Joe Doe'); //Name is optional
$mail->addAddress('mail#example.com', 'Optional Name'); //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 is just namespacing. Look at the examples for reference - you need to either use the namespaced class or reference it absolutely, for example:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
I suggest you look into getting composer. https://getcomposer.org
Composer makes getting third-party libraries a LOT easier and using a single autoloader for all of them. It also standardizes on where all your dependencies are located, along with some automatization capabilities.
Download https://getcomposer.org/composer.phar to C:\Inetpub\wwwroot\php
Delete your C:\Inetpub\wwwroot\php\PHPMailer\ directory.
Use composer.phar to get the phpmailer package using the command line to execute
cd C:\Inetpub\wwwroot\php
php composer.phar require phpmailer/phpmailer
After it is finished it will create a C:\Inetpub\wwwroot\php\vendor directory along with all of the phpmailer files and generate an autoloader.
Next in your main project configuration file you need to include the autoload file.
require_once 'C:\Inetpub\wwwroot\php\vendor\autoload.php';
The vendor\autoload.php will include the information for you to use $mail = new \PHPMailer;
Additional information on the PHPMailer package can be found at https://packagist.org/packages/phpmailer/phpmailer
I was with the same problem except with a slight difference, the version of PHPMailer 6.0, by the good friend avs099 I know that the new version of PHPMailer since February 2018 does not support the autoload, and had a serious problem to instantiate the libraries with the namespace in MVC, I leave the code for those who need it.
//Controller
protected function getLibraryWNS($libreria) {
$rutaLibreria = ROOT . 'libs' . DS . $libreria . '.php';
if(is_readable($rutaLibreria)){
require_once $rutaLibreria;
echo $rutaLibreria . '<br/>';
}
else{
throw new Exception('Error de libreria');
}
}
//loginController
public function enviarEmail($email, $nombre, $asunto, $cuerpo){
//Import the PHPMailer class into the global namespace
$this->getLibraryWNS('PHPMailer');
$this->getLibraryWNS('SMTP');
//Create a new PHPMailer instance
$mail = new \PHPMailer\PHPMailer\PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// $mail->SMTPDebug = 0; // 0 = off (for production use), 1 = client messages, 2 = client and server messages Godaddy POR CONFIRMAR
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
//Whether to use SMTP authentication
$mail->SMTPAuth = true; // authentication enabled
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl'; //Seguridad Correo Gmail
//Set the hostname of the mail server
$mail->Host = "smtp.gmail.com"; //Host Correo Gmail
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465; //587;
//Verifica si el servidor acepta envios en HTML
$mail->IsHTML(true);
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = 'tumail#gmail.com';
//Password to use for SMTP authentication
$mail->Password = 'tucontraseña';
//Set who the message is to be sent from
$mail->setFrom('tumail#gmail.com','Creador de Páginas Web');
$mail->Subject = $asunto;
$mail->Body = $cuerpo;
//Set who the message is to be sent to
$mail->addAddress($email, $nombre);
//Send the message, check for errors
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
return false;
} else {
echo "Message has been sent";
return true;
}
Just from reading what you have written, you will need to add the file class.phpmailer.php to your directory as well.
PHPMailerAutoload needs to be in the same folder as class.phpmailer.php
This is the PHPMailerAutoload code that I assume this:
$filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'class.'.strtolower($classname).'.php';
Just download composer and install phpMailler autoloader.php
https://github.com/PHPMailer/PHPMailer/blob/master/composer.json
once composer is loaded use below code:
require_once("phpMailer/class.phpmailer.php");
require_once("phpMailer/PHPMailerAutoload.php");
$mail = new PHPMailer(true);
$mail->SMTPDebug = true;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'youremail id';
$mail->Password = 'youremail password';
$mail_from = "youremail id";
$subject = "Your Subject";
$body = "email body";
$mail_to = "receiver_email";
$mail->IsSMTP();
try {
$mail->Host= "smtp.your.com";
$mail->Port = "Your SMTP Port No";// ssl port :465,
$mail->Debugoutput = 'html';
$mail->AddAddress($mail_to, "receiver_name");
$mail->SetFrom($mail_from,'AmpleChat Team');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$mail->Send();
$emailreturn = 200;
} catch (phpmailerException $e) {
$emailreturn = $e->errorMessage();
} catch (Exception $e) {
$emailreturn = $e->getMessage();
}
echo $emailreturn;
Hope this will work.
I resolved error copying the files class.phpmailer.php , class.smtp.php to the folder where the file is PHPMailerAutoload.php, of course there should be the file that we will use to send the email.
I had a number of errors similar to this. Make sure your setFrom email address is valid in $mail->setFrom()

Categories