Prot
Prot

Reputation: 11

Transfer textarea lines to arrays php

I'm trying to use a form with textarea where I will put some emails. After the form is submitted, phpmailer.php has to connect to smtp. Then for each line/email in textarea it sends an email.

Is there a way to do this without opening an smtp connection for each line/email?

This is the code in phpmailer:


try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp.example.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = '[email protected]';                     // SMTP username
    $mail->Password   = 'secret';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
    $mail->addAddress('[email protected]');               // Name is optional
    $mail->addReplyTo('[email protected]', 'Information');
    $mail->addCC('[email protected]');
    $mail->addBCC('[email protected]');

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

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

Upvotes: 1

Views: 44

Answers (1)

Slava Rozhnev
Slava Rozhnev

Reputation: 10163

In this case you can explode emails posted by form and add each of them as Bcc like:

$_POST['emails'] = 
    '[email protected]
    [email protected]
    [email protected]
    [email protected]
    [email protected]';

$emails=explode(PHP_EOL, $_POST['emails']);
   
foreach ($emails as $email) {
    $mail->addBCC($email);
}

test PHP code online

Upvotes: 1

Related Questions