I want to send my attached PDF with message to the user.
I have my website files in my server in Public_htm folder. There I have created folder with name "wp-regpdf". in this folder when user registers themselves a PDF is created. Now I want to send the PDF to their email as an attachment. I tried to attach it but unable to get, only body message is displaying no attachment is coming.
<?php
session_start();
if ($_SESSION['email'] == '' || $_SESSION['Uniqueid'] == '') {
session_destroy();
echo '<script>window.location.href="wp-destroy.php"</script>';
}
?>
<?php
$Uniqueid =$_SESSION['Uniqueid']; //this the file name iam getting from session
$path = 'wp-regpdf/' . $_SESSION['Uniqueid'] . '.pdf'; // this is the complete path with file name i need
$email = $_SESSION['email'];
include ('smtp/PHPMailerAutoload.php');
echo smtp_mailer($email, 'Testing Mail', 'Please find your payslip attached');
function smtp_mailer($to, $subject, $msg) {
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtppro.zoho.in";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
//$mail->SMTPDebug = 2;
$mail->Username = "";
$mail->Password = "";
$mail->SetFrom("support@team.tech");
$mail->addCC('info@team.com');
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->AddAttachment('' . $path . ''); //need help here?
$mail->AddAddress($to);
$mail->SMTPOptions = array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => false));
if ($mail->Send()) {
echo '<script>window.location.href="thankyou.php"</script>';
} else {
echo $mail->ErrorInfo;
}
}
?>
The most likely problem is that the absolute path to the file is wrong. You are setting the path like this:
$path = 'wp-regpdf/' . $_SESSION['Uniqueid'] . '.pdf';
but this is a relative path, and where it is relative to might not be where you think. It's better to resolve it to an absolute path, for example by making it relative to where the script itself resides:
$path = __DIR__ . '/wp-regpdf/' . $_SESSION['Uniqueid'] . '.pdf';
When you are debugging things like this always question your assumptions, so add an echo $path;
to confirm that it is the correct path. Now you can try to attach it:
if (!$mail->addAttachment($path)) {
die('Attaching file failed');
}
This might fail with the correct path if the user the script is running as does not have ownership or permission to read the file.