$myXML =
'<?xml version="1.0" encoding="ISO-8859-2"?>
<Document-ORDRES>
<ORDRES-Header>
<OrderResponseNumber>123</OrderResponseNumber>
<OrderResponseDate>0</OrderResponseDate>
<BuyerOrderNumber>0</BuyerOrderNumber>
<BuyerOrderDate>0</BuyerOrderDate>
<DeliveryDate>0</DeliveryDate>
</ORDRES-Header>
</Document-ORDRES>';
$xml=simplexml_load_string($myXML) or die("Error: Cannot create object");
public function send_mail($xml){
print_r($xml);
echo "</br></br>";
$data;
require 'C:\xampp\htdocs\PHPMailer-master\PHPMailerAutoload.php';
header('Content-Type: text/html; charset=utf-8');
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = "...";
$mail->Port = 587;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->SMTPAuth = true;
$mail->Username = "...";
$mail->Password = "...";
$mail->setFrom('...', '...');
$mail->addAddress('...', '');
$mail->Subject = '...';
$mail->AltBody = " ";
$mail->msgHTML("Test");
$mail->addAttachment($xml, "xml.xml");
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
Can you tell me what should I do?
I want send this xml like attachment, but in mail I haven't got attachments.
I've got just e-mail text.
Use addStringAttachment() method instead of addAttachment():
$mail->addStringAttachment($xml, "xml.xml");
addAttachment() adds attachment with path to a file, so alternatively you would have to save the xml as a file:
$file = __DIR__ . '/xml.xml';
file_put_contents($file, $myXML);
then add it and delete it...
$mail->addAttachment($file);
$mail->send();
unlink($file);
Related
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
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
?>
Phpmailer Configuration: Lumen.
Previously it was working? But now the same configuration throw Failed to connect to server:
I am a newbie to laravel/lumen framework. This is my PHPmailer configuration, I don't know what I am doing wrong over here. Please somebody help me here.
<?php
namespace App\Repositories;
use App\Repositories\BaseRepository;
use App\Models\ForgetModel;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
class ForgetRepository extends BaseRepository{
private $forget;
public function __construct(ForgetModel $forget) {
$this->forget = $forget;
}
public function save_verification_code($email,$verification_code)
{
$query = $this->forget->onMaster()
->insert(array('email'=>$email,'code'=>$verification_code));
}
public function send_forget_password_email($to,$message)
{
$subject = 'Verification code to reset your password';
$from = 'xyz#gmail.com';
$body = $message;
$headers = 'From: ' . strip_tags($from) . '\r\n';
$headers .= 'MIME-Version: 1.0\r\n';
$headers .= 'Content-Type: text/html; charset=ISO-8859-1\r\n';
$mail = new PHPMailer(); // create a new object
$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 = 'xyz#gmail.com';
$mail->Password = '123456';
$mail->SetFrom($from);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress($to);
if(!$mail->Send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
//echo "Message has been sent";
}
return true;
}
}
1. run the command "composer require phpmailer/phpmailer" in your cmd.
namespace App\Repositories;
use App\Repositories\BaseRepository;
use App\Models\ForgetModel;
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';
class ForgetRepository extends BaseRepository{
private $forget;
public function __construct(ForgetModel $forget) {
$this->forget = $forget;
}
public function save_verification_code($email,$verification_code)
{
$query = $this->forget->onMaster()
->insert(array('email'=>$email,'code'=>$verification_code));
}
public function send_forget_password_email($to,$message)
{
$mail = new PHPMailer(true);
try {
//Server settings
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//$mail->Host = $host;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 465
$mail->IsHTML(true);
$mail->Username = $username;
$mail->Password = $password;
$mail->SetFrom($username);
$mail->Subject = $subject;
$mail->Body =$body;
$mail->AddAddress($to);
$mail->Send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
}
I wanna send an email, but when i send it, there is problem like this:
Fatal error: Uncaught Error: Call to undefined function setContext() in C:\xampp\htdocs\rapor\admin\mail.php:27 Stack trace: #0 {main} thrown in C:\xampp\htdocs\rapor\admin\mail.php on line 27
I GET "id" from URL and SELECT with query in mail.php page.
This is my code:
<?php
require '../PHPMailer/PHPMailerAutoload.php';
require '../config/connect.php';
$kodenilai = $_GET["id"];
$queryKirim = mysqli_query($konek,"SELECT nilai.*, pelajaran.nama_pelajaran, siswa.nama_siswa, siswa.nis FROM siswa, pelajaran, nilai, datakelas, kelas
WHERE nilai.kode_siswa=siswa.kode_siswa
AND nilai.kode_pelajaran=pelajaran.kode_pelajaran
AND datakelas.kode_siswa=siswa.kode_siswa
AND datakelas.kode_kelas=kelas.kode_kelas
AND kelas.kode_kelas=nilai.kode_kelas
AND nilai.kode_nilai='$kodenilai'")or die("gagal".mysqli_error());
$data=mysqli_fetch_array($queryKirim);
$dataNilai['nis'] = $data['nis'];
$dataNilai['nama_siswa'] = $data['nama_siswa'];
$dataNilai['semester'] = $data['semester'];
$dataNilai['nama_pelajaran']= $data['nama_pelajaran'];
$dataNilai['nilai_tugas'] = $data['nilai_tugas'];
$dataNilai['nilai_tugas2'] = $data['nilai_tugas2'];
$dataNilai['nilai_tugas3'] = $data['nilai_tugas3'];
$dataNilai['nilai_uts'] = $data['nilai_uts'];
$dataNilai['nilai_uas'] = $data['nilai_uas'];
function setContext($data) {
$postdata = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
return stream_context_create($opts);
}
$context = setContext($dataNilai);
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = "mail.besp.gq";
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "********";
$mail->Password = "********";
$mail->setFrom('info#besp.gq', 'Info');
$mail->addReplyTo('admin123#besp.gq', 'Admin');
$mail->addAddress('ujangujing765#gmail.com', 'ujangujing765');
$mail->Subject = 'PHPMailer SMTP test';
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
$mail->msgHTML(file_get_contents('mail-temp/content.php', false, $context));
$mail->AltBody = 'This is a plain-text message body';
//$mail->addAttachment('images/phpmailer_mini.png');
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
In the script above, i set the setContext is data array. is there problem?
Thanks first..
The below function to send email but got any mail or error :
function send_Email($email, $subject, $body ,$cc = '')
{
$this->Log("Sending email with To: ". $email. " <br>Subject: ". $subject ." <br>Body: ".$body);
$mail = new PHPMailer();
$mail->IsHTML(true);
$mail->IsSMTP();
$mail->CharSet = "utf-8";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = email_host;
$mail->Username = email_username;
$mail->Password = email_password;
$mail->SetFrom('support#xyv.com', 'zzz Support');
$mail->AddReplyTo('support#xyv.com', 'zzz Support');
$mail->AddAddress($email);
$mail->AddBCC('support#xyv.com');
$mail->AddBCC('zzz#zzz.com');
if(!empty($cc)) {
$mail->addCC($cc);
}
$mail->Subject = $subject;
$mail->Body = $body;
echo "<pre>"; print_r($mail);echo "</pre>";
$response = $mail->Send();
var_dump($response);
return $response;
}
Debug : IN DEBUG MODE NOT GET ERROR
[error_count:protected] => 0
It would really help to read the docs and base your code on the examples provided, which show how to get info about errors, and show debug output. You're not getting any output because you're not asking for any. From the example in the readme:
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
That will show you the reason for any sending failure. To enable SMTP debug output, do this:
$mail->SMTPDebug = 2;