I receive two messages using PHPMailer - php

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.

Related

Won't send an e-mail

So I'm using PHPAutoload to send an e-mail. However the mail won't send. I made a contact form where I ask for your name, subject and message and implemented that into my php code. Can anybody help me?
Thanks in advance.
<?php
include(HPHMailerAutoload);
$result="";
if(isset($_POST['submit'])){
require 'PHPMailerAutoload.php';
$mail->Host='smtp.gmail.com';
$mail->Port=587;
$mail->SMTPSecure='tls';
$mail->Username='test#test.com';
$mail->Password='******';
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->addAddress('email');
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->isHTML(true);
$mail->Subject='Test ';
$mail->Body='Test';
}
?>
You forgot to call send() so add this code after $mail->Body='Test';
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo; // you can redirect to an error page
} else {
echo 'Message sent!'; // you can redirect to a thank page
}

$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 Error on parameters

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

PHPmailer duplicate email issue - conditional statement with $mail->Send()

I came across a bizarre issue with PHPmailer (version 5.1) that I'm trying to workaround. I've seen quite a bit of good feedback on here, so I thought I would give it a try. I've found that when I attempt to create a customized confirmation message with a conditional statement based on $mail->send(), I receive duplicate emails. I can duplicate it with the generic testemail.php script that comes with the PHPMailer download. Here's the code:
require '../class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$mail->SMTPDebug = 1;
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "mail.domain.com"; // SMTP server
$mail->Username = "username"; // SMTP server username
$mail->Password = "password"; // SMTP server password
$mail->IsSendmail();
$mail->From = "example_from#domain.com";
$mail->FromName = "First Last";
$to = "example#domain.com";
$mail->AddAddress($to);
$mail->Subject = "PHP Mailer test";
$message = "This is a test. \n";
$mail->Body = $message;
$mail->Send();
if ($mail->Send()) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
The above code echoes the "Message has been sent" confirmation, but then sends two emails. If I comment out the $mail->send() line, I still receive the "message has been sent" confirmation and only get one message. If I remove the conditional statement and leave the $mail->send() line commented out, no email is sent.
Why does adding a conditional statement cause an email to be sent without calling the $mail->send() method?What is the proper way of adding a customized confirmation message?
When you put $mail->Send() in your conditional, you're actually calling it again, sending another message, and checking if that second message was sent.
If you just keep
if ($mail->Send()) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
and get rid of the original, unconditional Send call, you should be fine.
Alternatively, if it's clearer for you or if you need to do some processing elsewhere that depends on whether the message was successfully sent, you could do the essentially equivalent:
$status = $mail->Send();
if ($status) {
echo 'Message has been sent.';
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}

Categories