Can I send a php variable as an attachment - php

i'm Using livedoc to create a pdf. I've suceeded in doing so without saving the file to the server, and thats the objective here.
Can I attach the file I've created which is on a variable via phpmailer and email it.
require("php_mailer/phpmailer.inc.php");
$mail = new phpmailer;
$mail->From = "noreply#credireto.com";
$mail->FromName = "Crédireto";
$mail->Host = "smtp1.site.com;smtp2.site.com"; // specify main and backup server
$mail->AddAddress("$email"); // name is optional
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("$data","Simulação.pdf"); // add attachments
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Simulação Crédireto";
$mail->Body = filegetcontents("emailagradecimento");
$mail->Send(); // send message
The variable $data contains the file I want to send.

I Solved It. Created a temp file and sent the attachment. Let me know if you need any help with this.

Related

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!

EasyPeasyICS is not sending ics file with my email

In codeigniter, working with phpMailer, I am trying to use EasyPeasyICS to help build an email with an ics file and this is a portion of my code:
$invite = new EasyPeasyICS();
$invite->addEvent(strtotime($appointment_data['start_datetime']),strtotime($appointment_data['end_datetime']),
$service_data['name'],$message." ".$appointment_link,$appointment_link);
$mail = new PHPMailer();
$mail->From = $company_settings['company_email'];
$mail->FromName = $company_settings['company_name'];
$mail->AddAddress($receiver_address); // "Name" argument crushes the phpmailer class.
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Subject = $title;
$mail->Body = $email_html;
$mail->Ical = $invite->render(false);
if (!$mail->Send()) {
throw new Exception('Email could not been sent. Mailer Error (Line '
. __LINE__ . '): ' . $mail->ErrorInfo);
}
return TRUE;
}
So, what I get is my HTML email and no ICS file. If I modify $mail->Ical = $invite->render(true) then the ics file downloads to me and I see the ICS file wrapped around my HTML as it is supposed to be and it looks great. Why is it not sending out with the email?
Even with the latest version (5.2.22), you must set AltBody if you want to parse Ical. Because alternative (alt) message type is required to Ical to be loaded.
But if you specify Ical, your email WILL BE an event, and will be considered as it by email clients.

using phpmailer class to send attachment in email: Could not access file

I have this function below "sendMail", to send emails using phpmailer class.
And its working fine, Im already sending emails with sucess.
But now Im trying to add one attachment to my function, so I added this to my code:
$mail->AddAttachment(BASE.'/'.'images/2014/05/image4.jpg','image 4');
And Im having this error:
Could not access file: localhost/project/images/2014/05/image4.jpg
But this directory is exactly the correct directory to my image, so I dont understand why this error is happening.
Somebody there knows if Im missing something here?
My function to send email:
function sendMail($subject,$message,$emissor,$emissorName,$receptor,$receptorName, $reply = NULL, $replyName = NULL){
require_once('mail/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->IsHTML(true);
$mail->SMTPSecure = "tls";
$mail->Host = MAILHOST;
$mail->Port = MAILPORT;
$mail->Username = MAILUSER;
$mail->Password = MAILPASS;
$mail->From = utf8_decode($emissor);
$mail->FromName = utf8_decode($emissorName);
$mail->Subject = utf8_decode($subject);
$mail->Body = utf8_decode($message);
$mail->AddAddress(utf8_decode($receptor),utf8_decode($receptorName));
$mail->AddAttachment(BASE.'/'.'images/2014/05/image4.jpg','image 4');
if($reply != NULL){
$mail->AddReplyTo(utf8_decode($reply),utf8_decode($replyNome));
}
if($mail->Send()){
return true;
}
else{
return false;
}
}
And my server acess file, when I acess this url: htttp://localhost/projeto/banner-imagens/2014/05/bib_idh.jpg, I get my image!
Your path is incorrect. Remove BASE and replace it with the actual file path to the project folder. Put in the full path or relative path based on the script.
Add an attachment from a path on the filesystem.
http://phpmailer.github.io/PHPMailer/classes/PHPMailer.html#method_addAttachment
IF that doesn't fix it also make sure the file is readable. Meaning the file permissions are set so that web server can access the file.

PHPMailer doesn't work on CentOS

I got a contact us page using PHP. Like all contact us, after someone clicking send, it will send a warning to my email.
The problem is it doesn't work. The script works perfectly in my localhost, it doesn't work only on the server and it doesn't show any error.
$default_path = get_include_path();
set_include_path(dirname(__FILE__)."/../");
require_once("extensions/PHPMailer/class.phpmailer.php");
set_include_path($default_path);
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->SMTPSecure="ssl";
$mail->Host="smtp.gmail.com";
$mail->SMTPDebug =0;
$mail->Port=465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = Yii::app()->params['sender_email']; // SMTP username
$mail->Password = Yii::app()->params['sender_password']; // SMTP password
$webmaster_email = Yii::app()->params['webmaster_email']; //Reply to this email ID
$mail->From = $email_address;
$mail->FromName = "Webmaster";
$mail->AddAddress($email_address,"");
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 70; // set word wrap
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = $mailcontent->subject;
$mail->Body = $mailcontent->body;
//$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
$mail->Send();
I think it's because of server misconfiguration(this is my first time settings a server), but i didn't know what i did wrong.
The firewall already set to allow every traffic, so it's not firewall problem.
No error and no results makes me very confused.
Okay, after I checked everything, it turns out google block my email access from the server because of suspicious activity.
To unblock this, you need to visit this page : https://accounts.google.com/DisplayUnlockCaptcha
and then run the script again.

How to save mail body as html

I am looking for a way to save my template file which I use as a certificate snf sttsch it to my my.
Below is the code to replace fields in my template with one I have queried.
require("class.phpmailer.php");
//placeholders array
$pholders = array('replace_name', 'replace_number', 'replace_title');
//Certificate replace values array
$failedValues = array($FullName, $TestNo, $Title);
$mail_body = file_get_contents("failed.html");
$mail_body = str_replace($pholders,$failedValues,$mail_body);
//mailer
$mail = new phpmailer;
$mail->IsSMTP(); // set mailer to use SMTP
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Results";
$mail->Body = $mail_body;
$mail->AddAttachment("cpd.html");
I have omitted code for the smtp setting. The file cpd.html has to be generated and attached. I am looking for a way on how to generate it and save it for attaching it to the mail.

Categories