Could not access file PHPMailer in Laravel - php

I am creating a project using Laravel. I want to send an email with an attachment; however, this is not working. I know there are many questions regarding this, but unfortunately, I could not find the answer.
$file1 = $_FILES['file']['tmp_name'];
$path = base_path();
$path .= '/public/uploads/'.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $path);
DB::statement("UPDATE orders SET pdf = '".$fileData."' WHERE id = '".$id."'");
$mail = new PHPMailer\PHPMailer(); // create a n
$mail->isSMTP();
$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 = 'xxxxx#gmail.com';
$mail->Password = 'xxxxxx';
$mail->SetFrom('xxxxx#gmail.com', 'xxxxx.com');
$mail->Subject = 'xxxxx Ready';
$mail->Body = $text;
$mail->AddAddress("xxxxxx.com");
$mail->AddAttachment($pdfLink, $fileData, $encoding = 'base64', $type = 'application/pdf');
$mail->Send();
The generated PDF link is accessible, and when I open the PDF link in a new tab, it works. However, I don't know why PHPMailer told me that I could not access.

I got the solution, i am just getting the file from server. This is the reason this is not working.The file should upload from actual path.

login to your gmail account, go to Enable access
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=ENTER_YOUR_GMAIL_USERNAME
MAIL_PASSWORD=ENTER_YOUR_GMAIL_PASSWORD
MAIL_ENCRYPTION=ssl
and all must be working fine )

Related

HTML2PDF page stores file but is not sent in e-mail

i have a problem with my code, it's supposed to store the pdf file in the server and then send it as an attachment in an e-mail to the client automatically, here is the code :
ob_end_clean();
$fileName='CW00'.$commande.'.pdf';
$file = $html2pdf>Output('/home/itscoma/public_html/components/'.$fileName,'F');
try{
$mail = new PHPMailer();
$mail->IsHTML(true);
$mail->IsSMTP();
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->CharSet = 'UTF-8';
$mail->Host= "smtp.gmail.com";
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 465;
$mail->Username = "***#***.com";
$mail->Password = "******";
$mail->From='a***#gmail.com';
$mail->AddAddress('*****#gmail.com');
$mail->AddReplyTo('*****#gmail.com');
$mail->Subject='sujet';
$mail->Body='message';
$mail->AddAttachment($file);
$mail->Send();
unset($mail);
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Several things wrong here. This combination will not work:
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 465;
Change Port to 587 to use that encryption mode, or change encryption mode to PHPMailer::ENCRYPTION_SMTPS to work with port 465.
This line is meaningless:
$file = $html2pdf>Output('/home/itscoma/public_html/components/'.$fileName,'F');
because you're missing a -, it should be $html2pdf->Output.
Next, what does that method return? What do you get in $file? If it's not the path to the saved file (which you already know anyway), then passing it to addAttachment is not going to do anything useful. It would be a good idea to check that this call actually works – at the moment you're just assuming it does.
You would know this if you checked the return value from addAttachment(), but you're not so you don't know that it's failing.
I suggest trying this:
if (!$mail->addAttachment('/home/itscoma/public_html/components/' . $fileName)) {
echo 'Attachment failed';
}

How to send attachment from Laravel storage app to email via PHPmailer

I have tried to send attachments by accessing files from the storage app that I am uploading from the form and send it to email using phpMailer but in vain.
After the files being uploaded from the form, it is stored in storage/app/docs
The body content is sending but the attachment is not, Kindly help
controller function
$mail = new PHPMailer\PHPMailer(); // create a n
$mail->SMTPDebug = false; // debugging: 1 = errors and messages, 2 = messages only
$mail->IsSMTP();
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "server1.web-hosting.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "support#example.com";
$mail->Password = "xxxxx";
$mail->SetFrom("support#example.com", $receiver_name);
$file = $request->file('filenames');
$filename = $file->getClientOriginalExtension();
$filename_string = time().'.'.$filename;
$path = $file->storeAs('docs', $filename_string);
$mail->AddAttachment($path,$filename_string);
$mail->Subject = 'Order No #'.$paw.'- Premium Article Writers
';
$mail->IsHTML(true);
$mail->Body = $text;
$mail->AddAddress($email,$name);
$mail->Send();
}
else
{
echo "No data Was posted nor sent to database, Contact administrator for help- info#premiumarticlewriters.com";
}

Yii phpmailer not working for gmail

I'm using Yii phpmailer extension to send mail using gmail but it showing SMTP Error: Could not authenticate. instead of sending mail. My code what I used
Yii::import('application.extensions.phpmailer.JPhpMailer');
$mail = new JPhpMailer;
$mail->IsSMTP();
//$mail->Host = 'smpt.163.com';
$mail->Host = 'smtp.googlemail.com:465';
//$mail->Host = 'smtp.gmail.com:587';
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = $email;
$mail->Password = $pass;
$mail->SetFrom($email, SiteConfig::SITE_TITLE);
$mail->Subject = 'PHPMailer Test Subject via smtp, basic with authentication';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML('<h1>JUST A TEST!</h1>');
$mail->AddAddress($email, 'My Name');
$mail->Send();
I followed this link.
How can I solve this problem and send mail using gmail? I’d appreciate any light you can shed on this!!
Try adding the following line:
$mail->Port = 465;
I am unsure if adding :port works for the Host field as you've done - I can confirm that setting the Port field manually does work, as I have a large system whose code looks very similar to yours which is working as expected.
We also use $mail->Host = 'smtp.gmail.com' but I am almost certain that the smtp.googlemail.com domain should also work.
You can debug and get error report
for 1 error and message
for 2 message only
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
Be sure to set the following attributes:
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = "your username";
$mail->Password = "your password";
It should work with those attributes set.
Hope it helps.

PHP Mailer change FROM NAME

I have managed to send a mail to myself using this code.
$mail = new PHPMailer;
//$mail->SMTPDebug = 1;
//Email server info
$mail->isSMTP();
$mail->Host = 'smtp-mail.outlook.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
//User authentication info
$mail->SMTPAuth = true;
$mail->Username = '******#hotmail.com';
$mail->Password = '********';
//Email info
$mail->From = 'noreply#ledii.net';
$mail->FromName = 'LediiNet';
$mail->addAddress($mail->Username);
$mail->Subject = 'LediiNet Feedback';
$mail->Body = 'Here are some thoughts...';
Is it possible to make it so that the mail will show up with my website name as sender insteath of my name and email?
I would preferrably want it to seem like the mail is sent from noreply#ledii.net and with the sender name of LediiNet.
I feel like I probably have to actually create that email somehow and connect to my host servers email server info?
I figured out how to do it, but I had to change to using the mail() function and modify my local files for it to work locally.

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.

Categories