Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Code for sending emails, via SMTP, to recipients listed in a Text file

    Hi, All Coders!
    Whatever I know of PHP and HTML, I have learnt it from the Tutorials and Tips available on the Internet.
    I have jotted together a code for accessing email addresses from a text file, and then sending email to each address one at a time. My text file contains, for testing purposes, only two email addresses; and the name of this file is input via an HTML file. The PHP code is given below, please:-
    <?php
    // read the selected-list of emails from HTML file.
    $listname = $_POST['listname'];

    // count how many emails there are.
    $email_list2 = file($listname);
    $total_emails = count($email_list2);
    echo $total_emails . '<br />';

    //open the file for reading and writing
    $email_list = fopen($listname, "r+");
    $to = fgets($email_list);
    echo $to . '<br />';
    require("class.phpmailer.php");

    $mail = new PHPMailer();

    $mail->IsSMTP(); // set mailer to use SMTP
    $mail->Host = "mail.domain.com"; // SMTP server generally it is: smtp.domain.com
    $mail->SMTPAuth = true; // settings
    $mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
    $mail->Port = 25;
    $mail->Username = "info@domain.com"; // same as for webmail
    $mail->Password = "***"; // same as for webmail

    $mail->SMTPDebug = 1;
    $mail->IsHTML(true); // set email format to HTML
    $mail->ContentType = "text/html"; // set email format to HTML
    $mail->MsgHTML($body); // set email format to HTML
    $mail->Timeout = 120; // set longer timeout for latency or servers that take a while to respond

    $mail->From = "info@domain.com";
    $mail->FromName = "xyz";

    $mail->AddReplyTo('noreply@domain.com', 'List manager');

    $mail->AddAddress($to); //see above
    //$mail->AddAddress($to2); //see below
    //$mail->AddCC($email_list);
    $mail->Subject = $sub;
    $mail->Body = $body;

    $mail->WordWrap = 50;

    $sub = "CALL FOR PAPERS";
    $body = "<?php header('!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd' . 'html xmlns='http://www.w3.org/1999/xhtml' . 'MIME-Version: 1.0' . 'Content-type: text/html; charset=iso-8859-1'); ?>";
    $body .= "<html><body>";
    $body .= "<div align='center' style='color: #008000; font-face: Verdana, fontsize: 3'>";
    $body .= "PAPERS<br /><br />";
    $body .= "<span>To achieve the best International standards in this field, we are in collaboration with ...<br /><br /></span>";
    $body .= "Regards<br /><br />";
    $body .= "<img src='xxxxx.png' alt='image' width='131' height='131' /><br /><br />";
    $body .= "</div></body></html>";

    /*** set the counter to one ***/
    $i = 0;

    /*** loop over the file pointer ***/
    while($i<$total_emails)
    {
    /*** read the line into a buffer ***/
    $to2 = fgets($email_list);
    $i++;
    echo "<br />" . "The number of additional records processed are:" . $i . " " . "viz.<br />" . $to2 . "<br />";
    $mail->AddAddress($to2);
    }

    if(!$mail->Send())
    {
    echo '<br />Message was not sent.';
    echo '<br />Mailer error: ' . $mail->ErrorInfo;
    }
    else
    {
    echo '<br />Message sent to:<br />';
    echo '$to<br />';
    }
    //while loop is now closed
    ?>

    The Text file (test.txt) contains the following emails (actual id is replaced by xxx and yyy) on two separate lines:-
    xxx@yahoo.com
    yyy@yahoo.com
    Error Displayed:-
    2
    xxx@yahoo.com

    The number of additional records processed are:1 viz.
    yyy@yahoo.com

    The number of additional records processed are:2 viz.

    SMTP -> ERROR: RCPT not accepted from server: 501 Syntax error in parameters or arguments
    Message was not sent.
    Mailer error: SMTP Error: The following recipients failed:

    Query: Which part of the Code contains the Syntax error, and how can it be corrected, please? Thanks in Advance!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That's not a syntax error, thats a SMTP rejection error. Your sendmail has rejected your request to send. That indicates that you have an error with your configuration or the data is malformed. With a RCPT I'd suggest it's rejecting the data you've provided. Make sure that all of your body text is valid, that you give it a valid from address and to address, as well as a subject. Your current code does not contain a subject.
    Without any security as well, you are putting yourself at risk. Make sure you are aware of the risks of opening arbitrary files and rejecting invalid files and urls from the input.

    I'm also unsure of what you are doing here:
    PHP Code:
    $body = "<?php header('!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd' . 'html xmlns='http://www.w3.org/1999/xhtml' . 'MIME-Version: 1.0' . 'Content-type: text/html; charset=iso-8859-1'); ?>";
    That is not a valid header in PHP. That should probably be simply HTML code, NOT php mixed with HTML.

    I also do not use the PHPMailer class, but I'd assume it'd have an AddAddresses method on it to allow an array of addresses to add. Assuming it does have that, or that the AddAddress is a vararg function which can determine an array versus a single parameter, you can probably give it the contents of a file() instead.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •