Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

php - PhpMailer, how do I write the variable value correctly for multiple addCC email address?

I have a PhpMailer working code as follow: (short version)
(the variable already defined before hand)

// Sender and recipient settings
$mail->setFrom($pengirim_email, $pengirim_nama);
$mail->addAddress($untuk_email, $untuk_nama);
$mail->addReplyTo($pengirim_email, $pengirim_nama);

Next, I add multiple email address for CC mail:

$mail-->addCC('[email protected]','Abdul');
$mail-->addCC('[email protected]','Borat');

It work as expected.

Now since I'm planning that the email address will come from the SQL query, so for the time being I want to know how do I have to fill the SQL 'CarbonCopy' column table with multiple email addresses - by trying to make a "hardcoded" variable value. So I try like this as the replacement for the addCC above:

$tembusan="'[email protected]','Abdul';'[email protected]','Borat'"; //not working
$CC = explode(';', $tembusan); //not working
for ($i = 0; $i < count($CC); $i++) {$mail->addCC($CC[$i]);} //not working

But it throw me an error like this :

Error in sending email. Mailer Error: Invalid address: (cc): '[email protected]','Abdul'

So I change the $tembusan into like this:

$tembusan="[email protected],Abdul;[email protected],Borat"; //not working

It gives me almost the same like the error before:

Error in sending email. Mailer Error: Invalid address: (cc): [email protected],Abdul

Next, I try also this kind of code :

$tembusan="'[email protected]','Abdul';'[email protected]','Borat'"; //not working
$CC = explode(';', $tembusan); //not working
foreach($CC as $CCemail){$mail->AddCC($CCemail;} //not working

And it also throw the same error:

Error in sending email. Mailer Error: Invalid address: (cc): '[email protected]','Abdul'

If I echo the last code like this foreach($CC as $CCemail){echo $CCemail. '<br/>';}, it give me a result like this :

'[email protected]','Abdul'
'[email protected]','Borat'

In my real code, I have a valid email address. The email address in the code above is just for an example.

Where did I do wrong ?

PS
btw, if I remove the "name" for the email address:

$tembusan="[email protected];[email protected]"; //working
$CC = explode(';', $tembusan); //working
foreach($CC as $CCemail){$mail->AddCC($CCemail;} //working

it runs as expected (but in the gmail, the CC name is aaa and bbb).

question from:https://stackoverflow.com/questions/65850488/phpmailer-how-do-i-write-the-variable-value-correctly-for-multiple-addcc-email

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Please do a further explode . try

$tembusan="[email protected],Abdul;[email protected],Borat"; 
$CC = explode(';', $tembusan); 

for ($i = 0; $i < count($CC); $i++) {
$DD = explode(',', $CC[$i]);
$mail->addCC($DD[0], $DD[1]);
} 

Please note that I have removed the ' characters . (you may use str_replace of PHP to eliminate these characters)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...