Click here to Skip to main content
15,947,476 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using PHPMailer class and using $Variables for some of the mail addresses.
The problem is that if the $Variable is empty, PHP Mailer responds with :-
Message could not be sent. Mailer Error: Invalid address: (cc):

A portion of the code is as follows::-
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
    //Server settings
    //$mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail - > isSMTP(); // Set mailer to use SMTP
    $mail - > Host = ''; // Specify main and backup SMTP servers
    $mail - > SMTPAuth = true; // Enable SMTP authentication
    $mail - > Username = 'xxx'; // SMTP username
    $mail - > Password = 'xxx'; // SMTP password
    $mail - > SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
    $mail - > Port = 2525; // TCP port to connect to

    $mail - > AllowEmpty = true;
    $mail - > setFrom('', 'Adrian Boshoff');
    //$mail->addAddress($mrow['email']); //This is an example of creating a variable from an array field
    $mail - > addAddress($email); //This is an example of creating a variable from an array field
    $mail - > addReplyTo('');
    $mail - > addCC($addemail);
    $mail - > AllowEmpty = true;

    $mail - > addAttachment('eco.pdf'); // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    $mail - > isHTML(true); // Set email format to HTML
    $mail - > Subject = $subject;
    $mail - > Body = << < END < !doctype html >
        < html xmlns = ""
    xmlns: v = "urn:schemas-microsoft-com:vml"
    xmlns: o = "urn:schemas-microsoft-com:office:office" >
        < head >
        <!-- NAME: SELL PRODUCTS -->

What I have tried:

I tried adding
$mail->AllowEmpty = true;

Which has not helped.
Updated 24-May-18 0:35am
Richard MacCutchan 24-May-18 7:43am    
What does the varaible $addemail contain?
Member 13840807 24-May-18 7:52am    
$addemail is sent from a form on a previous page and is a field that may be left empty by the user, because it is a CC field for an email. If there is nobody that needs to be CC'ed int he mail, then it will not have an entry posted.
I have created a temporary solution by giving the HTML form field a value="" but would ultimately like to resolve this.
Richard MacCutchan 24-May-18 7:59am    
Check its value, and do not add it to the message if it is empty.
Member 13840807 24-May-18 8:25am    
Thanks Richard, I think your suggestion is the way to go. I just have to get my head around how to code that, but I am confident I will manage to.
Richard MacCutchan 24-May-18 8:51am    

1 solution

Try like this

for complete code follow this link
How to send mail using php mailer with attachment - PHPKIDA[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900