multiple attachments going with single mail from phpmailer - php

I am sending payslip mails with payslips as attachment with phpmailer class. the problem is the first mail is going with one attachment but the sedonf mail is going with the first and the second attachments together.
For example:
mail for employee name : A is going with A.pdf
mail for employee name : B is going with A.pdf and B.pdf
need some help. my project completion date is tomorrow and I am stuck in this last problem.
this is my code:
<?php
require_once 'mailerClass/PHPMailerAutoload.php';
require_once '../connect.php';
$mail = new PHPMailer;
//$mail->isSMTP();
$sql = "SELECT * FROM mail ORDER BY Id";
$query = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($query, MYSQL_ASSOC)){
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = false;
$mail->Username ='riteshrc13#gmail.com';
$mail->Password = "password";
$mail->setFrom('64mediakraft#gmail.com', 'Mediakraft');
$mail->addAddress($row['Email'], $row['Name']);
$mail->Subject = "Payslip of " . $row['Name'];
$mail->Body = "payslip email";
$mail->AltBody = 'Payslip Email for the month. Please find the payslip attached.';
$mail->isHTML(true);
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$pdf = "C:/Reports/" . $row['Name']. ".pdf";
$mail->addAttachment($pdf);
if ($mail->send()) {
echo "<script>alert('Mail Sent success');</script>";
// header("Location:index.php");
}
else {
echo "<script>alert('Mailer Error: ' $mail->ErrorInfo);</script>";
// header("Location: index.php");
}
$pdf = "";
} //endwhile
?>

Creating a new instance inside the loop will work, but it's very inefficient and means you can't use keepalive, which makes a huge difference to throughput.
Base your code on the mailing list example provided with PHPMailer which shows how to send most efficiently, and read the docs on sending to lists. To paraphrase that example, it should go roughly like this:
$mail = new PHPMailer;
//Set properties that are common to all messages...
$mail->isSMTP();
$mail->SMTPKeepAlive = true;
$mail->Host = 'mail.example.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Subject = 'Hello';
$mail->From = 'user#example.com';
//etc
//Loop over whatever resource gives you your recipients
foreach ($result as $target) {
//Set properties that are specific to this message
$this->addAddress($target['email']);
$this->addAttachment($target['file']);
//Send the message
$this->send();
//All done, so clear recipients and attachments for next time around
$mail->clearAddresses();
$mail->clearAttachments();
}
Don't forget to add some error checking in there, and I can also see that you're using an old version of PHPMailer - so get the latest, and base your code on the mailing list example.

$mail = new PHPMailer; // this should be inside of while, I think...

Thanks to #jonStirling and #toor for the help.
complete working code for other help seekers:
<?php
require_once 'mailerClass/PHPMailerAutoload.php';
require_once '../connect.php';
//$mail->isSMTP();
$counter = 1;
$sql = "SELECT * FROM mail ORDER BY Id";
$query = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($query, MYSQL_ASSOC)){
$mail = new PHPMailer;
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = false;
$mail->Username ='riteshrc13#gmail.com';
$mail->Password = "password";
$mail->setFrom('64mediakraft#gmail.com', 'Mediakraft');
$mail->addAddress($row['Email'], $row['Name']);
$mail->Subject = "Payslip of " . $row['Name'];
$mail->Body = "payslip email";
$mail->AltBody = 'Payslip Email for the month. Please find the payslip attached.';
$mail->isHTML(true);
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$pdf = "C:/Reports/" . $row['Name']. ".pdf";
$mail->addAttachment($pdf);
if ($mail->send()) {
echo "<script>alert('Mail Sent success');</script>";
// header("Location:index.php");
}
else {
echo "<script>alert('Mailer Error: ' $mail->ErrorInfo);</script>";
// header("Location: index.php");
}
$pdf = "";
$mail->clearAttachments();
} //endwhile
?>

Related

How to put pdf file into specific folder after sending it through email

Here's my code for generating pdf and send it through email
$dompdf->set_paper($customPaper);
$dompdf->render();
ob_end_clean();
$file = $dompdf->output();
file_put_contents($pdfname,$file);
$name = 'sample';
$subject = 'sample subject';
$body = 'Hello '.$empname.' , here\'s your payslip';
if(!empty($email)){
$mail = new PHPMailer();
// smtp settings
$mail->isSMTP();
$mail->Mailer = "smtp";
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "sample#gmail.com"; // gmail address
$mail->Password = "sample123"; // gmail password
$mail->Port = 587;
$mail->IsHTML(true);
$mail->SMTPSecure = 'tls';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
// email settings
$mail->isHTML(true);
$mail->setFrom($email,$name);
$mail->addAddress($email);
$mail->Subject = ($subject);
$mail->Body = $body;
$mail->AddAttachment($pdfname); [check the picture][1]
if($mail->send()){
$status = "success";
$response = "Email is sent!";
} else {
$status = "failed";
$response = "Something is wrong: <br/>". $mail->ErrorInfo;
}
I want to put the generated pdf to the specific folder to be organized, because it just appear somewhere. thankyou very much for your help

Phpmailer - Getting multiple entries of custom headers while looping

This is my mailing script, i m using for loop to send multiple emails.
if(isset($_POST['submit'])){
require ("vendor/autoload.php");
$fr_email = $_POST['fr_email'];
$from = $_POST['from'];
$reply_to = $_POST['reply_to'];
$sub = $_POST['sub'];
$tm = $_POST['email'];
$str = "<117418239>
<128422057>";
$listid= explode("\n",$str);
$mail = new PHPMailer\PHPMailer\PHPMailer;
for ($x = 2; $x > 0 ; $x--){
$mail->isSMTP();
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
));
$mail->SMTPDebug = 0;
$mail->Host = 'hostname';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->Username = 'uname';
$mail->Password = 'pwd';
$mail->SMTPSecure = 'tls';
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
$mail->isHTML(true);
$mail->setFrom($fr_email,$from);
$mail->AddReplyTo($reply_to,$from);
$mail->Sender=$fr_email;
$mail->Subject=$sub;
$mail->XMailer = ' ';
$mail->addCustomHeader('List-ID', $listid[$x-1]);
$body = $_POST['body'];
$mail->Body=$body;
//$mail->AltBody=$altbody;
$mail->addAddress($tm);
if ($mail->send()) {
echo "\nMessage sent\n";
}
else {
echo 'Mailer error: ' . $mail->ErrorInfo;
}
$mail->clearAddresses();
$mail->SmtpClose();
} // for loop close
}
I'm using "addcustomheader" function to add list id header field. Bcoz i m running a loop i m getting multiple entries of list id field in my email header. [even though i m closing my smtp connection after each iteration]
for 1st email
List-ID: <117418239>
for 2nd email
List-ID: <117418239>
List-ID: <128422057>
Is there a way to clear the custom headers after each iteration of the loop, like clearing the email address
Yes. Note the name of the method addCustomHeader – it adds a header, and does not replace existing ones. To fix this, call clearCustomHeaders() after sending within your loop. See the docs on this method.

PHPMailer not working

I have tried my best to recieve message on a gmail account using phpmailer but i can't. It shows message sent successfully but i don't recieve any message. I even edited the sendmail.ini file. This is the code:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
//$mail->SMTPDebug = 2;
//$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myemail#gmail.com';
$mail->Password = 'mypassword';
$mail->SMTPSecure = 'ssl';
$mail->Port = 587;
$mail->setFrom('myemail#gmail.com', 'Mailer');
$mail->addAddress('example#gmail.com', 'Joe User');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if($mail->send()){
echo 'Message has been sent successfully';
} else {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
I no so many questions concerning this have been answered but i still need help
try:
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
Hope it will help.
Change setFrom to SetFrom and try to change IP to 465.
You can also try the following.
$mail = new PHPMailer();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email#gmail.com";
$mail->Password = "password";
$mail->SetFrom("example#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email#gmail.com");
if($mail->send()){
echo 'Message has been sent successfully';
} else {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
If you still get error than provide the error message.
First of all, try to uncomment line
$mail->isSMTP();
If you send mail via https, try to add following code:
$mail->SMTPOptions = array( 'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true ) );

PHPMailer: The following From address failed

I have some troubles with my sending loop with PHP Mailer:
<?php
foreach ($packTabCible as $key => $pack) {
// PHPMailer
$mail = new PHPMailer;
$mail->Mailer = "smtp";
$mail->isSMTP();
$mail->SMTPKeepAlive = true;
$mail->SMTPAuth = true;
$mail->Encoding = '8bit';
$mail->SMTPDebug = 3;
$mail->setLanguage('fr', './PHPMailer/language/');
$mail->CharSet = 'UTF-8';
$mail->WordWrap = 10000;
$mail->Host = $hostname;
$mail->Username = $username;
$mail->Password = $pass;
$mail->SMTPSecure = $tls;
$mail->Port = $port;
$mail->FromName = utf8_decode($from_name);
$mail->isHTML(true);
$mail->Subject = utf8_decode($sujet);
$mail->From = $from_mail;
foreach ($pack as $email => $md5) {
$mail->addAddress($email);
$header = str_replace("#miroir#", $link_miroir, $ip_header_html);
$footer = str_replace("#lien#", $link_unsub, $ip_footer_html);
$content = str_replace("<body>", "<body>".$header, $message);
$content = str_replace("</body>", $footer.'</body>', $content);
$content = str_replace("#email#", $email, $content);
$mail->Body = $content;
$mail->AltBody = str_replace("#miroir#", $link_miroir, utf8_decode($ip_header_txt))
. $txt
. str_replace("#lien#", $link_unsub, $ip_footer_txt);
$mail->send();
$mail->clearAddresses();
usleep($timer);
}
$mail->SmtpClose();
usleep(5000000);
}
?>
I'm 100% sure of the validity of the parameters I send to PHPMailer.
There is 2 loops, and I reset the connexion after each loop of $packTabCible with $mail->SmtpClose();
But, PHPMailer return the error:
The following From address failed: news#my-domain.com : MAIL FROM command failed,0 ok
,
2,SMTP server error: MAIL FROM command failed Detail: 0 ok
SMTP code:
2SMTP server error: MAIL FROM command failed Detail: 0 ok
SMTP code:
2
But news#my-domain.com is the good one so the problem is elsewhere...
If someone have an idea, big thanks

PHPMailer, SMTP connect() failed error with Gmail

I’m trying to make a contact form and I’m using PHPMailer. I tried that on localhost with xampp and it works perfect. But when i upload to my host i get the error SMTP connect() failed.
Here is my code:
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->Host = "smtp.gmail.com";
$m->Username = "mymail#gmail.com";
$m->Password = "mypass";
$m->SMTPSecure = "ssl";
$m->Port = "465";
$m->isHTML();
$m->Subject = "Hello world";
$m->Body = "Some content";
$m->FromName = "Contact";
$m->addAddress('mymail#gmail.com', 'Test');
I've tried to change the port to 587 and the SMTPsecure to tls (and all the combinations). But doesn’t work. Any advice to solve this?
Thanks
This answer work form me: https://stackoverflow.com/a/47205296/2171764
I use:
$mail->Host = 'tls://smtp.gmail.com:587';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
You may need to specify the address from which the message is going to be sent, like this:
$mail->From = 'user#domain.com';
I would also give isHTML a parameter, either true or false:
$m->isHTML(true);
Another option is trying to drop the port specification all together. There are several other parameters that you may find useful. The following example is code I've tested, see if you can adapt it for your uses:
$mail = new PHPMailer;
$mail->isSMTP();/*Set mailer to use SMTP*/
$mail->Host = 'mail.domain.com';/*Specify main and backup SMTP servers*/
$mail->Port = 587;
$mail->SMTPAuth = true;/*Enable SMTP authentication*/
$mail->Username = $username;/*SMTP username*/
$mail->Password = $password;/*SMTP password*/
/*$mail->SMTPSecure = 'tls';*//*Enable encryption, 'ssl' also accepted*/
$mail->From = 'user#domain.com';
$mail->FromName = $name;
$mail->addAddress($to, 'Recipients Name');/*Add a recipient*/
$mail->addReplyTo($email, $name);
/*$mail->addCC('cc#example.com');*/
/*$mail->addBCC('bcc#example.com');*/
$mail->WordWrap = 70;/*DEFAULT = Set word wrap to 50 characters*/
$mail->addAttachment('../tmp/' . $varfile, $varfile);/*Add attachments*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
$mail->isHTML(false);/*Set email format to HTML (default = true)*/
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
header("Location: ../docs/confirmSubmit.html");
}
Hope this helps!

Categories