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
Comments
So what's the problem? You are feeding it invalid addresses and not handling errors properly? |
Thank you, I have managed to fix the bug by editing phpmailer class |
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. |
I've noticed what your actual problem is from that error message:
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 |
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 |
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;
}
?> |
@OcranDave Don't hijack unrelated issues. You're getting an error message that says:
Is it that much of a stretch to think that this might mean that you have left the message body empty? 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. |
Oh ok thank you! Synchro And here is the code ".$_POST['msg'].""; } if($mail->send()){ echo "email sent"; } else{ echo "error"; } } ?><textarea type="msg" placeholder="Message" ></textarea>
|
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 |
Hello,
No other mails appear in the message in the email inbox |
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. |
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);
}
}
The text was updated successfully, but these errors were encountered: