how to grab all images with foreach

Asked

Viewed 137 times

0

how do I get all the img with the foreach and send by email, I put my function to send the email inside the loop but it won’t send me only the first and if I put the function outside the loop it sends me the last someone could help me?

code

if(!empty($_POST)){
        foreach($_POST['ck'] as $ck){
            $msg = "<img src=\"http://www.site.com.br/images/$ck\"width=\"100px\" height=\"70px\"><br>";

                // manda e-mail usuario
                $mail = new PHPMailer();
                $mail-> IsSMTP();
                $mail->CharSet = "UTF-8";
                $mail->SMTPAuth = true;
                $mail->SMTPSecure = 'tls';
                $mail->Port = 587;
                $mail->Host = "smtp.zoho.com";
                $mail->Username = "[email protected]";
                $mail->Password = "senha";
                $mail->SetFrom("[email protected]", "usuario");

                $mail->AddAddress("[email protected]", "usuario");
                $mail->Subject = "assunto";
                $mail->msgHTML($msg);

                if($mail->send()){
                    echo "enviado com sucesso";
                    exit();
                }else{
                    echo "Erro ao enviar o email".$mail->ErrorInfo;
                }
            }       

    }
  • In an email you want to have multiple images or want an image by email?

  • in an email several img

1 answer

0


Leave out everything that does not vary, such as the creation of the Phpmailer object and send it from the email. Use the property Body to format/manipulate/assign email measurement.

if(!empty($_POST)){

    $mail = new PHPMailer();
    $mail-> IsSMTP();
    $mail->CharSet = "UTF-8";
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;
    $mail->Host = "smtp.zoho.com";
    $mail->Username = "[email protected]";
    $mail->Password = "senha";
    $mail->SetFrom("[email protected]", "usuario");
    $mail->Subject = "assunto";
    $mail->AddAddress("[email protected]", "usuario");

    foreach($_POST['ck'] as $ck){
        $msg = "<img src=\"http://www.site.com.br/images/$ck\"width=\"100px\" height=\"70px\"><br>";
        $mail->Body .= $msg;
    }

    if($mail->send()){
        echo "enviado com sucesso";
        exit();
    }else{
        echo "Erro ao enviar o email".$mail->ErrorInfo;
    }           
}       
  • intendi thanks for the help man. now it worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.