Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with with Bcc and CC #980

Closed
sportyboty opened this issue Jan 31, 2017 · 11 comments
Closed

Issue with with Bcc and CC #980

sportyboty opened this issue Jan 31, 2017 · 11 comments

Comments

@sportyboty
Copy link

I'm building a mail system with phpmailer which will use bcc or cc. when i add emails on bcc like this

bcc1@example.com , bcc2@example.com, bcc3@example.com, bcc4@example.com,
bcc5@example.com , bcc6@example.com

I get this error when i try to send

Fatal error: Uncaught exception 'phpmailerException' with message 'Invalid address: (addAnAddress bcc): ,' in C:\xampp\htdocs\ultimate\vendor\phpmailer\phpmailer\class.phpmailer.php:881 Stack trace: #0 C:\xampp\htdocs\ultimate\vendor\phpmailer\phpmailer\class.phpmailer.php(845): PHPMailer->addOrEnqueueAnAddress('bcc', ',', '') #1 C:\xampp\htdocs\ultimate\preg.php(51): PHPMailer->addBCC(',') #2 {main} thrown in C:\xampp\htdocs\ultimate\vendor\phpmailer\phpmailer\class.phpmailer.php on line 881

this is my code for storing data

if ($bcc != '') {
$indiBCC = explode(" ", $bcc);
foreach ($indiBCC as $key => $value) {
$mail->addBCC($value);
}
}

@Synchro
Copy link
Member

Synchro commented Jan 31, 2017

So what's the problem? You are feeding it invalid addresses and not handling errors properly?

@sportyboty
Copy link
Author

Thank you, I have managed to fix the bug by editing phpmailer class

@Synchro
Copy link
Member

Synchro commented Jan 31, 2017

That's really the wrong way to do it. You should never have to edit library files; use the class properly, work around actual bugs or unwanted defaults by using a subclass. If you don't do that, your code will break when you update the library.

@Synchro Synchro closed this as completed Jan 31, 2017
@Synchro
Copy link
Member

Synchro commented Jan 31, 2017

I've noticed what your actual problem is from that error message:

addOrEnqueueAnAddress('bcc', ',', '')

here you can see that it's passing just a single comma as an email address. Handle this properly like this:

if ($bcc != '') {
    $indiBCC = explode(" ", $bcc);
    foreach ($indiBCC as $key => $value) {
        try {
            $mail->addBCC($value);
        } catch (phpmailerException $e) {
            echo 'Error: '. $e->getMessage();
        }
    }
}

What you've found is that you can't rely on explode to do a good enough job here.

@OcranDave
Copy link

Fatal error: Uncaught phpmailerException: Message body empty in C:\xampp\htdocs\NEWPROJECT\contactform\PHPMailer\class.phpmailer.php:1272 Stack trace: #0

I have this error have received . Please any help

C:\xampp\htdocs\NEWPROJECT\contactform\PHPMailer\class.phpmailer.php(1212): PHPMailer->preSend() #1 C:\xampp\htdocs\NEWPROJECT\contactform\contact.php(58): PHPMailer->send() #2 {main} thrown in C:\xampp\htdocs\NEWPROJECT\contactform\PHPMailer\class.phpmailer.php on line 1272

@OcranDave
Copy link

OcranDave commented Jan 13, 2018

here is the code:

<?php

session_start();

// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
//use PHPMailer\PHPMailer;
use PHPMailer\Exception;

//Load composer's autoloader
require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer(true); 
try {
if(isset($_POST['name'], $_POST['email'], $_POST['message'])){
$fields = [
  'name' => $_POST['name'],
    'email' => $_POST['email'],
	  'message' => $_POST['message']
];

foreach($fields as $field => $data){
	if(empty($data)){
		$errors[] = ' The ' . $field . ' field is required.';
	}
}                             // Passing `true` enables exceptions
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.comcast.net';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'ocrand9@gmail.com';                 // SMTP username
    $mail->Password = 'teres';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

	//Content
    $mail->isHTML(true);    
	
	// Set email format to HTML
    $mail->Subject = 'Contact form submitted';
    //$mail->AltBody = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ')<p>' . $fields['message'] . '</p>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
   
   //$mail->FromName = 'Contact';
    //Recipients
   $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('ocrand9@gmail.com', 'David Ocran');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('davisetad@gmail.com', 'David');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
   // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    if($mail->send()){
			header('location: thanks.php');
			die();
		}
		else{
			$errors[] = 'Sorry, could not send email. Try again later.';
		}
}


$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;

header('Location: index.php');

} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

?>

@PHPMailer PHPMailer deleted a comment from pilacroix Jan 13, 2018
@Synchro
Copy link
Member

Synchro commented Jan 13, 2018

@OcranDave Don't hijack unrelated issues.

You're getting an error message that says:

Message body empty

Is it that much of a stretch to think that this might mean that you have left the message body empty?
The message body is set in the Body property, which you're not setting, hence the error.

This makes no sense at all:

use PHPMailer\Exception;

//Load composer's autoloader
require 'PHPMailer/PHPMailerAutoload.php';

The first line here is just wrong - you're not setting the full-qualified class name for the Exception class, which you only need to do if you're using PHPMailer 6.0. Then you're loading the autoloader that is only present in PHPMailer 5.2, and does not exist in 6.0. You've got things very confused. I suggest you scrap this code, use PHPMailer 6.0, and start again with one the of the examples.

@OcranDave
Copy link

Oh ok thank you! Synchro
i have this error
Fatal error: Uncaught Error: Call to undefined method PHPMailer\PHPMailer\PHPMailer::isTHML() in C:\xampp\htdocs\tutorial\index.php:19 Stack trace: #0 {main} thrown in C:\xampp\htdocs\tutorial\index.php on line 19

And here is the code
i have installed autoloader

setFrom("davisetad@gmail.com" ,"David Ocran"); $mail->addAddress($_POST['email'] ,$_POST['name']); $mail->isTHML(true); $mail->Subject = "Here are the subject of email"; $mail->Body = "

".$_POST['msg']."

"; } if($mail->send()){ echo "email sent"; } else{ echo "error"; } } ?>
<textarea type="msg" placeholder="Message" ></textarea>

@Synchro
Copy link
Member

Synchro commented Jan 14, 2018

Oh come on, this is pathetic. Can you at least read the error message and make the tiniest bit of effort to look at what you've written? The function is called isHTML, not isTHML, HTML being a well-known markup language and THML being nothing in particular. It's not that much of a stretch.

@Ademgenc53
Copy link

Hello,

    foreach ($mails AS $mail){        
      $mail->addBCC($mail, "Title");
    }

No other mails appear in the message in the email inbox
But there is the second email "undisclosed-recipients"
https://antenfiyati.com/email.png

@Synchro
Copy link
Member

Synchro commented May 19, 2020

Yes, it's an empty RFC822 group address. They are not strictly necessary, but it's a convention that's been around for at least 40 years.

@PHPMailer PHPMailer locked as resolved and limited conversation to collaborators May 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants