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..
Related
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 need to send the email to the multiple recipients but I am getting error in my code. I need to send the email to multiple recipients.
<?php
require 'phpmailer/PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.domain.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->setFrom('info#domain.com', 'name');
$mail->addReplyTo('info#domain.com', 'name');
$mail->addAddress($to_id);
$mail->Subject = $subject;
$mail->msgHTML($message);
if(!$mail->send()) {
$error = "Mailer Error: " . $mail->ErrorInfo;
?>
<script>alert('<?php echo $error ?>');</script>
<?php
}
else {
echo "Message Sent Successfully";
}
}
?>
Try this code.
require 'PHPMailer/PHPMailerAutoload.php';
function SendPHPMail($to, $from, $subject, $htmlContent, $attachments = array())
{
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'emailAddress#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => true,
'allow_self_signed' => true
)
);
$mail->From = 'emailAddress#gmail.com'; //sender emailAddress
$mail->FromName = 'name'; //sender name
//Here $to has multiple emailAddress
//$to = array('address1#domain.com','address2#domain.com','address3#domain.com');
if(!empty($to)){
foreach($to as $emailAddress){
$mail->addAddress($emailAddress);
}
} else{
throw new \Exception('No emails found!');
}
if(!empty($attachments)){
foreach($attachments as $attachment){
$mail->addAttachment($attachment);
}
}
//$mail->addCC();
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $htmlContent;
if(!$mail->send()) {
throw new \Exception($mail->ErrorInfo);
}
}
instead of $mail->addAddress($to_id); use something like this:
$mail->addAddress("peter#doe.com , hannes#mail.com , manuel#domain.com", "...");
You can use just like that:
$mail->AddAddress('email1#domain.com', 'First Email');
$mail->AddAddress('email2#domain.com', 'Second Email');
Or if you have emails in an array $_POST['toid'], than you can use AddAddress() in a loop.
$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);
I am having problem with sending mail using phpmailer. I am a beginner programmer. I am trying to make contact form. My code is as follow(submit.php). Please suggest me.. Thanks in advance .
session_start();
require_once 'libs/phpmail/PHPMailerAutoload.php';
$errors = array();
if(isset($_POST['name'], $_POST['phone'],$_POST['mail'],$_POST['message'])){
$fields = array(
'name' => $_POST['name'],
'phone' => $_POST['phone'],
'email' => $_POST['mail'],
'message' => $_POST['message']
);
foreach ($fields as $field => $data) {
if(empty($data)){
$errors[] = 'The '. $field . ' field is required';
}
}
if(empty($errors)){
$m = new PHPMailer;
$m -> isSMTP();
$m -> SMTPAuth = true;
//$m -> SMTPDebug = 2;
$m -> Host = 'smtp.gmail.com';
$m -> Username = 'xxxx#gmail.com';
$m -> Password = 'xxxx';
$m -> SMTPSecure = 'ssl';
$m -> Port = 465;
$m -> isHTML();
$m -> Subject = 'Contact form submitted';
$m -> Body = 'From: ' . $fields['name']. '('. $fields['phone'] . $fields['email']. ')'.'<p>' .$fields['message'] .'</p> ';
$m -> FromName = 'Contact';
// $m ->addReplyTo($fields['email'], $fields['name']);
$m -> addAddress('ssss#gmail.com', 'xxxxxxxx');
if($m->send()){
header('Location: thanks.php');
die();
}else{
$errors[] = 'Sorry could not send email. Please try again';
}
}
}else{
$errors[] = 'some thing went wrong';
}
$_SESSION['error'] = $errors;
$_SESSION['field'] = $fields;
header('Location: form.php');
My setting phpmailer, everything works
function __construct ($to, $subject, $body) {
date_default_timezone_set('Etc/UTC');
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
$mail->CharSet = 'UTF-8';
//Set the hostname of the mail server
$mail->Host = "mail.xxxxxx.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
$mail->AuthType = 'PLAIN';
//Username to use for SMTP authentication
$mail->Username = "xxxx";
//Password to use for SMTP authentication
$mail->Password = "xxxx";
//Set who the message is to be sent from
$mail->setFrom('erp#xxxxxx.com');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress($to);
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($body);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
$this->mail = $mail;
}
function SendMail () {
//send the message, check for errors
if (!$this->mail->send()) {
return "Mailer Error: " . $this->mail->ErrorInfo;
} else {
return true;
}
}
// using
$email = $this->request->getPost('email');
$smtp = new \SmtpClient($email, 'Test', $template);
$result = $smtp->SendMail();
Remove
$m -> Subject = 'Contact form submitted';
And try again.
When I remove the subject it worked.
I have used PHPMailer for SMTP and there is problem in sending mail with error "Mailer Error: The following From address failed: no-reply#mydomain.org.uk"
My code is as follows:
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "localhost;"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = ""; // SMTP username
$mail->Password = ""; // SMTP password
$mail->From = $email_address;
$mail->FromName = $email_address;
$mail->AddAddress($arrStudent[0]["email"]);
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Subject";
$theData = str_replace("\n", "<BR>", $stuff);
$mail->Body = $theData; // "This is the <b>HTML body</b>";
$mail->AltBody = $stuff;
if (!$mail->Send()) {
$sent = 0;
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
i researched everything and when i debug inside class.smtp.php i found error the function "get_lines()" is returning error value "550 Authentication failed"
The code was working fine previously, i am wondering how this problem came suddenly.
Desperate for some help.
Thanks,
Biplab
public function sendEmail ( $subject, $to, $body, $from = FALSE ) {
require_once('mailer.class.php');
$mailer = new PHPMailer();
//do we use SMTP?
if ( USE_SMTP ) {
$mailer->IsSMTP();
$mailer->SMTPAuth = true;
$mailer->Host = SMTP_HOST;
$mailer->Port = SMTP_PORT;
$mailer->Password = '';
$mailer->Username = '';
if(USE_SSL)
$mailer->SMTPSecure = "ssl";
}
$mailer->SetFrom($from?$from:ADMIN_EMAIL, ADMIN_NAME);
$mailer->AddReplyTo ( ADMIN_EMAIL, ADMIN_NAME );
$mailer->AddAddress($to);
$mailer->Subject = $subject;
//$mailer->WordWrap = 100;
$mailer->IsHTML ( TRUE );
$mailer->MsgHTML($body);
require_once('util.class.php');
$mailer->AltBody = Util::html2text ( $body );
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if ( ! $mailer->Send() ) {
return FALSE;
}
else {
$mailer->ClearAllRecipients ();
$mailer->ClearReplyTos ();
return TRUE;
}
}
I've used like that... SetFrom should be used in place of From... that's your error buddy... :))
try adding belowe line to php.ini
extension=php_openssl.dll
restart and try again
I am using YII's Mailer with PHPMailer, and this works for me:
$mail = Yii::createComponent('application.extensions.mailer.EMailer');
$mail->Username = $this->SMTP_USERNAME; // SMTP username
$mail->Password = $this->SMTP_PASSWORD; // SMTP password
$mail->SMTPAuth = true;
$mail->From = $this->fromAddress;
$mail->Host = $this->SMTP_SERVER_ADDRESS;
$mail->FromName = $this->fromName;
$mail->CharSet = 'UTF-8';
$mail->Subject = Yii::t('mailer', $this->subject);
$mail->Body = $this->message;
$mail->AddReplyTo($this->toAddress);
$mail->AddAddress($this->toAddress);
$mail->IsSMTP(true);
$mail->IsHTML(true);
$mail->Send();
Hope that helps?