PHPMailer Error on parameters - php

Trying to send emails with multiple attachments using PHPMailer
require is fine
$mail = new PHPMailer(); is fine
script stops working when I start adding in the parameters for the mails.
PHP function :
function kamp_bulk_mail(){
require $_SERVER['DOCUMENT_ROOT'].'/assets/PHPMailer-master/PHPMailerAutoload.php';
require $_SERVER['DOCUMENT_ROOT'].'/assets/PHPMailer-master/class.phpmailer.php';
$content = mysqli_fetch_array(sql('select','select * from kampen_mailtemplate where kamp = '.$_POST['id']));
$spelers = sql('select','select * from kampen_spelers where kamp = '.$_POST['id']);
while($spelers_ = mysqli_fetch_array($spelers)){
echo "1";
$mail = new PHPMailer(true);
try {
echo "2";
$mail->From("test#sender.be");
echo "3";
$mail->FromName("Admin");
$mail->addAddress($spelers_['email_voogd']);
echo "4";
foreach(glob("kampen/configuratie/bijlagen/".$_POST['id']."_*") as $filename){
$mail->addAttachment($filename);
}
echo "5";
$mail->Subject = $content['titel'];
$mail->Body = $content['content'];
}
catch(phpmailerException $e){
echo $e->errorMesage();
}
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
}
This echoes 12
EDIT : adjusted code to show where the problem begins

try changing
$mail->addAdress($spelers_['email_voogd']);
To
$mail->addAddress($spelers_['email_voogd']);
Its just a typo I think
Here is a list of all the method names

Related

I receive two messages using PHPMailer

I do not understand why two messages come to my mail.
The send function is launched once and the message about the successful sending displays once.
<?php
require('class.phpmailer.php');
$email = new PHPMailer();
$email->CharSet = 'UTF-8';
$email->From = $_POST['mailmy'];
$email->FromName = '«Тэкс»';
$email->Subject = 'Ваша новая кухня почти готова.';
$email->Body = $_POST['mailText'];
$email->AddAddress( $_POST['mailMeil']);
$email->Send();
echo 'Message has been sent';
if (!$email->send()) {
echo "Mailer Error: " . $email->ErrorInfo;
} else {
echo "Message sent!";
}
?>
You call the send() method twice:
$email->Send(); // first time
echo 'Message has been sent';
if (!$email->send()) { // second time
The code is doing exactly what you told it to do: send twice.
What you should do is store the result the first time and test that:
$sent = $email->Send();
echo 'Message has been sent';
if (!$sent) {
As an aside: your echo statement there doesn’t make sense. You shouldn’t tell the user the message has been sent if you don’t know that yet.

PHPMailer Only one attachment is sent in the following code but i want multiple attachment

Following is my code
include("Phpmailer.php");
$phpmail = new PHPMailer();
$phpmail->Mailer = 'mail';
$phpmail->Host = 'relay-hosting.secureserver.net';
$phpmail->ClearAddresses();
$phpmail->ContentType = 'text/html';
$phpmail->From = "from#mail.com";
$phpmail->FromName = "Company";
$phpmail->Sender = "from#mail.com";
$phpmail->AddAddress("to#mail.com","Test user");
$phpmail->Subject = 'Multiple Attachment';
$mailBoddy = file_get_contents("files/example_text_file.txt");
$arrayFind = array("{ABC}","{XYZ}");
$arrayReplace = array("Test","Multiple Attachment");
$mailBoddy = str_replace($arrayFind,$arrayReplace,$mailBoddy);
$phpmail->Body = $mailBoddy;
// attachment
$phpmail->AddAttachment("files/example1.pdf","Example 1");
$phpmail->AddAttachment("files/example2.pdf","Example 2");
// send email
if(!$phpmail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $phpmail->ErrorInfo;
} else {
echo 'Message has been sent';
};
Problem is that in the above code is, it only send one attachment which is first one example1 not including the second one. I want to send multiple files like "example1" and "example2" in the single email.

$mail->send() in PHPMailer is returning true but I didn't receive mail

I'm using PHPMailer to send emails from my website but $mail->send() is returning true and mail is not sending. No error is reporting in my error log. I hosted my site in Bigrock. I didn't find any errors in my code.
<?php
if(isset($_POST['submit']))
{
require 'class.smtp.php';
require 'PHPMailerAutoload.php';
ini_set('SMTP','localhost' );
ini_set('sendmail_from', 'exmaple#gmail.com');
$fromrec=$_POST['from'];
$from="example#gmail.com";
$subject=$_POST['sf'];
$message=$_POST['message'];
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->setFrom($from, 'Rahul');
$mail->addAddress("example1#gmail.com");
$mail->Subject = $subject;
$mail->Body = "From:".$fromrec."".$message;
if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else if($mail->send()) {
echo 'Message has been sent.';
echo $mail->ErrorInfo;
}
else
{
echo 'Mailer error: ' . $mail->ErrorInfo;
}
}
?>
On this issue, I have consulted Bigrock customer care and I made a chat with them. It's simple in the above code the from address email id must be domain specific and to address would be anything. After changing the email id to domain specific and Host to mail.example.com. My problem has been solved. If anyone got the same problem please try this.

PHPMailer Master not sending Email

I am trying to send an email with a .PDF attached using PHPMailer Master.
I am receiving no email, seeing no errors and cannot understand what's going on.
Here is my code:
$url = ''. $_SERVER['DOCUMENT_ROOT'] .'/free-quote/';
$mail = new PHPMailer;
$mail->setFrom('removed#email.com', 'Company Name');
$mail->addAddress(''. $email2 .'', ''. $full_name .'');
$mail->Subject = 'Website Quotation ('. $quotation_ref .')';
$mail->addStringAttachment(file_get_contents($url), ''. $quotation_ref .'.pdf');
$mail->Body = 'Hi! This is my first e-mail sent through PHPMailer.';
I have also included the required file:
require('PHPMailer-master/class.phpmailer.php');
Ideas?
From the PHPMailer Docs:
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
https://github.com/PHPMailer/PHPMailer

PHPMailer sends duplicate email

PHPMailer
<?php
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->SMTPSecure = "tls";
$mail->Mailer = "smtp";
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "xxx";
$mail->Password = "xxx";
$mail->setFrom('xxx', 'Website');
//Send to Admin
$AdminEmail = 'admin.example#gmail.com';
$mail->AddAddress($AdminEmail, $AdminName);
$mail->Subject = "This is an email";
$mail2 = clone $mail;
$body = 'Hi Admin. This is an email';
$mail->Body = $body;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
//Send to User
$UserEmail = 'user.example#gmail.com';
$mail2->AddAddress($UserEmail, $UserName);
$body2 = 'Hi User. This is an email';
$mail2->Body = $body2;
if(!$mail2->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail2->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
I have an issue where when i send the emails, Admin will receive $mail and $mail2 when they supposed to just receive $mail. While there's no problem with User. Works fine by receiving $mail2. I've tried to put $mail->ClearAddresses(); but it's still the same. By chance, what is the problem?
Clone the email variable before adding the address and stuff of the admin. (As suggested in the comments)
You need to create different-different object for both admin and user email
//Send to Admin
$mail = new PHPMailer;
$mail->IsHTML(true);
$AdminEmail = 'admin.example#gmail.com';
$mail->AddAddress($AdminEmail, $AdminName);
$mail->Subject = "This is an email";
$mail2 = clone $mail;
$body = 'Hi Admin. This is an email';
$mail->Body = $body;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
//Send to User
$mail = new PHPMailer;
$mail->IsHTML(true);
$UserEmail = 'user.example#gmail.com';
$mail->AddAddress($UserEmail, $UserName);
$body2 = 'Hi User. This is an email';
$mail->Body = $body2;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}

Categories