0

I am trying to send a newsletter to the email addresses from my table. The table displays the email addresses and the names. The table shows but the functionality of PHP mailer does not in Cpanel. When the Send Newsletter button is clicked, I should at least expect an error message which does not happen. My Autoload file is in the correct directory. Any ideas?

<?php require 'vendors/phpmailer/PHPMailerAutoload.php'; 
?>
<button type="button" name="bulk" class="btn-full">Send Newsletter</button>
<table>
                    <thead>
                    <tr>
                    <th style="text-align:left;">Email Address</th>
                    <th style="text-align:left;">Name</th>
                    </tr>    
                    </thead>
                    <tbody>

                    <?php

    $query = "SELECT * FROM Emails ORDER BY EmailID DESC";
    $selemails = mysqli_query($connection,$query);  

    while($row = mysqli_fetch_assoc($selemails)) {
        $emailadd = $row['email'];
        $emailname = $row['name'];
        echo "<tr>";
        echo "<td style='text-align:left;'>$emailadd</td>";
        echo "<td style='text-align:left;'>$emailname</td>";
        echo "</tr>";

    }
          ?>   
                    </tbody>
                    </table>


<?php

if(isset($_POST['bulk'])){
    $queryb = "SELECT * FROM Emails";
    $selemailer = mysqli_query($connection,$queryb);
    while($row = mysqli_fetch_assoc($selemailer)) {
        $emailsend = $row['email'];
        $namesend = $row['name'];
        sendEmail($emailsend, $namesend);
 }
function sendEmail($emailsend, $namesend){
    $htmlversion = "
 <html> 
    <head> 
        <title>Highlights for Janurary 2021</title> 
    </head> 
    <body>
    <section style='min-height: 100vh; font-size: 16px; background-color: #729dd0; padding: 8px 16px;'>
    <h1 style='text-align: center; font-weight: 400; font-size: 36px; color:#fff;'>Highlights for Janurary 2021</h1>
    <p>Hello <?php echo '$namesend'; ?></p>
    <p style='font-size: 16px; color:#fff;'>Your Email Address: <?php echo '$emailsend'; ?></p>
        </section>
    <section style='height: 40vh; background-color: #4ec9e6; padding: 1.2%; font-size:28px; font-weight: 300; color:#fff; text-align: center;'>
    If you would like to unsubscribe from my monthly newsletters, please click the button below.
        <br>
    <a href='cylical.com/apanel/BlogPanel/unsub.php?address=<?php echo '$emailsend';?>' name='unsub' style='padding:1.2%; width:60%;display: block;margin: 20px auto;border-radius:20px;cursor:pointer; background-color: #ff3030; color:#fff; border: 2px solid #111; font-size:20px; text-decoration: none;'>Unsubscribe</a> 
    <p style='font-size: 30px;'>Newsletter sent from Apple</p>
    <p style='font-size: 24px;'>Social Media Box</p>
    <p style='font-size: 20px;'>Street Address</p>
    </section>
    </body> 
    </html>

";  
$textversion = "Hi, '$namesend'.\r\n This is a test email.";
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);


    $mail->isSMTP();
    $mail->SMTPDebug  = 1;
    $mail->Debugoutput = 'html';
    $mail->Host       = "smtp.gmail.com";
    $mail->SMTPAuth   = true;
    $mail->Username   = "sample@gmail.com";
    $mail->Password   = "password123";
    $mail->SMTPSecure = "tls";
    $mail->Port       = 25;
    

    //Recipients
    $mail->setFrom('sample@gmail.com', 'Dylan Ma');
    $mail->addAddress($emailsend, $namesend);               // Name is optional
   


    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Monthly Newsletter';
    $mail->Body    = $htmlversion;
    $mail->AltBody = $textversion;
    $mail->send();
    if(!$mail->send()) {
            echo 'Newsletter could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Newsletter has been sent <br>';
}
}
}
?>

Edit: I am using the 5.2 stable release and followed the documentation from: https://github.com/PHPMailer/PHPMailer/tree/5.2-stable

  • 1
    Port right with TLS? Oh. And `$mail->send(); if(!$mail->send()) {...` is wrong. Try `$retval = $mail->send()` then check `$retval`. Don't try and send twice! – ficuscr Sep 29 '20 at 20:34
  • For SMTP servers that is used in port 25 and even with that Ive tried TLS, SSL in combination with ports 25,465,587. – JuicyYellow Sep 29 '20 at 20:38
  • Ive tried to check $mail->send() from another variable and that did not work. The mail is not sending at all. – JuicyYellow Sep 29 '20 at 20:46
  • 1
    You are running PHP mailer with Exceptions turned on but you are not catching any Exceptions. [Please view this Duplicate post instead](https://stackoverflow.com/questions/2386544/error-handling-with-phpmailer). Then you can get a very specific error message from PHPMailer as to why it does not send. Update your Question with this message. – Martin Sep 29 '20 at 21:01
  • I am using PHPMailer 5.2 and tried the try/exception method before swithcing to PHPMailer 5.2. If PHPMailer works, I would have gotten: echo 'Newsletter could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; which I do not see from my end. – JuicyYellow Sep 30 '20 at 06:35

0 Answers0