[SOLVED] Installation based on Docs, process script doesn't

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
LegionSmith
Forum Newbie
Posts: 5
Joined: Thu Mar 15, 2007 11:30 pm

[SOLVED] Installation based on Docs, process script doesn't

Post by LegionSmith »

Hi,

I'm using SwiftMailer 3.0.5-PHP4 on a PHP 4.4.4 server.
What I'm trying to do is send batch emails using the stock form provided by the docs. I ultimately need this form to email roughly 200 subscribers in my database, and hope to use Dreamweaver's behaviors to do that. But currently I only have (1)one email address under recipients.
I've also used the script provided in the docs with my own information where required.
Now, when I fill out the form and click submit I get send to a blank page (my handler script) and that's it. No redirection to a success page or a failure page. Also no email has been sent. I've tried numerous tweaks and debugging but still cannot find the problem. I'm two weeks into this now and totally out of time. My client needs this running next week. I'll post all my code below. Any help would be most appreciated!
Thanks,
Michael Smith

**********Form Page Code*********

Code: Select all

<?php

//Display an error if something went wrong
if (!empty($_GET["error"]))
{
    switch ($_GET["error"])
    {
        case "not_enough_info": ?>
            <strong style="color: red;">You need to complete all fields marked *<strong><?php
            break;
        case "invalid_email": ?>
            <strong style="color: red;">Please provide a valid email address</strong><?php
            break;
        case "upload_failed": ?>
            <strong style="color: red;">The file you uploaded failed to attach, this could be a temporary problem.
            Please try later.</strong><?php
            break;
        case "sending_failed": ?>
            <strong style="color: red;">Temporary problem, please try later.</strong><?php
            break;
    }
}
 
?>
<form action="handle_newsletter.php" method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td class="label">Sender Name</td>
            <td><input type="text" name="sender_name" value="" /></td>
        </tr>
        <tr>
            <td class="label">Sender E-mail address</td>
            <td><input type="text" name="sender_email" value="info@calb.org" /></td>
        </tr>
        <tr>
            <td class="label">Title</td>
            <td><input type="text" name="comment_title" value="" /></td>
        </tr>
        <tr>
            <td class="label">Attachment (optional)</td>
            <td><input type="file" name="attachment" /></td>
        </tr>
        <tr>
            <td colspan="2">Body of Newsletter<br />
                <textarea name="comment_body" rows="10" cols="70"></textarea></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
        </tr>
    </table>
</form>
*****End Form*******

*******handle_newsletter.php AKA my processing script*********

Code: Select all

<?php
 
//Check if the required fields were sent
// Redirect back to the form if not
if (empty($_POST["sender_name"]) || empty($_POST["sender_email"])
    || empty($_POST["comment_title"]) || empty($_POST["comment_body"]))
{
    //redirect back to form
    header("Location: http://www.calb.org/addnewsletter.php"); //This should really be an absolute URL if you know it
exit();
}

//Copy into global variables
$name = $_POST["sender_name"];
$email = $_POST["sender_email"];
$title = $_POST["comment_title"];
$body = $_POST["comment_body"];
 
//Validate the email address using a regex (I suggest you use a better one than this!!)
if (!preg_match("/[a-zA-Z0-9_\\.-]+@[a-zA-Z0-9_\\.-]+/", $email))
{
    header("Location: http://www.calb.org/addnewsletter.php?e ... alid_email");
    exit();
}
 
//Check if an attachment was uploaded
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["attachment"]["tmp_name"]))
{
    if ($_FILES["attachment"]["error"])
    {
        //Redirect if the upload has failed
        header("Location: http://www.calb.org/addnewsletter.php?e ... oad_failed");
        exit();
    }
    $file_path = $_FILES["attachment"]["tmp_name"];
    $file_name = $_FILES["attachment"]["name"];
    $file_type = $_FILES["attachment"]["type"];
}
 
//Everything looks ok, we can start Swift
 
require_once "swift/Swift.php";
require_once "swift/Swift/Connection/SMTP.php";
 
//Enable disk caching if we can
if (is_writable("/tmp"))
{
    Swift_CacheFactory::setClassName("Swift_Cache_Disk");
    Swift_Cache_Disk::setSavePath("/tmp");
}
 
//Create a Swift instance
$swift =& new Swift(new Swift_Connection_SMTP("smtp.calb.org"));
 
//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
 
//Create the message to send
$message =& new Swift_Message("New comment: " . $title);
$message->attach(new Swift_Message_Part($body));
 
//If an attachment was sent, attach it
if ($file_path && $file_name && $file_type)
{
    $message->attach(
        new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
}
 
//Try sending the email
$sent = $swift->send($message, "michael@legiondata.org", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
 
if ($sent)
{
   	
	header("Location: http://www.calb.org/success.php");
    exit();
}
else
{
    
    header("Location:http://www.calb.org/addnewsletter.php?error=sending_failed");
    exit();
}
***end handle_newsletter.php (including extra blank space at bottom of page********

d11wtq | Please add

Code: Select all

 [/ php] tags around your PHP code when posting in the forum.[/color]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Turn error reporting on. I know it works because I've just helped somebody successfully modify it to send to a mailing list ;)

Code: Select all

<?php error_reporting(E_ALL); ini_set("display_errors", "On"); ?>
I'm guessing you have whitespace before the <?php tag. That will break it.

EDIT | You'll need to fix this too:

Code: Select all

header("Location:http://www.calb.org/addnewsletter.php?error=sending_failed");

//should be (SP added)

header("Location: http://www.calb.org/addnewsletter.php?e ... ing_failed");
LegionSmith
Forum Newbie
Posts: 5
Joined: Thu Mar 15, 2007 11:30 pm

Post by LegionSmith »

Ok, I did everything exactly as you said. Including error reporting. This is the message i get when the script runs.

Fatal error:
Uncaught Error of type [swift_connection_exception] with message [The SMTP connection failed to start [smtp.calb.org:25]: fsockopen returned Error Number 111 and Error String 'Connection refused']
@0 swift::swift() in /hsphere/local/home/colegion/calb.org/handle_newsletter.php on line 58
@1 swift::connect() in /hsphere/local/home/colegion/calb.org/swift/Swift.php on line 109

in /hsphere/local/home/colegion/calb.org/swift/Swift/Errors.php on line 99

I'm guessing the errors on line 58 in handle_newsletter.php and line 109 in Swift.php are really just based on the connection refused error because both of those lines refer to making the connection:

line 58
$swift =& new Swift(new Swift_Connection_SMTP("smtp.calb.org"));

line 109
$this->connect();

but I did notice that Swift.php has no php ending tag. ?>
When handle_newsletter.php had no ending tag the script didn't even error report. I had to add the tag before I got the error. But the docs said not to add ?> at the end of the script??
Any Ideas on whats going on here?

Thanks much for your time and help.
Michael Smith
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It's a stack trace dump (or my home-made one for PHP4) :)

Your host is refusing the connection. Is that server running a SMTP service on port 25? Can you telnet to it? Are your host allowing your traffic to be sent/received to port 25? If your with a shared host it's quite likely that they are denying the traffic through some firewall rules.

EDIT | ?> tag is not needed and Zend say you shouldn't use it if it's the last item on the page.
LegionSmith
Forum Newbie
Posts: 5
Joined: Thu Mar 15, 2007 11:30 pm

Post by LegionSmith »

Ok so I tried to telnet into smtp.calb.org 25 with no luck. connection refused. but then I tried mail.calb.org 25 and was able to get in. I modified the script to reflect mail.calb.org instead of smtp.calb.org and got this error:

Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/colegion/calb.org/handle_newsletter.php:3) in /hsphere/local/home/colegion/calb.org/handle_newsletter.php on line 82

so now my question is, can I use mail.calb.org instead of smtp.calb.org and if so, what's up with this error.

I'm not very well versed in this kind of thing and my hosting provider charges $250 an hour minimum 1 hour for this kind of support. (even to open up port 25, if they will)

Again, any help would be most appreciated.

Thanks,
Michael Smith
LegionSmith
Forum Newbie
Posts: 5
Joined: Thu Mar 15, 2007 11:30 pm

Post by LegionSmith »

Addendum to my last post. Using mail.calb.org did end up sending the mail through to me. But I still have that header error.

Thanks,
Michael Smith
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

d11wtq wrote:I'm guessing you have whitespace before the <?php tag. That will break it.
That's what I stated in my first post. You can't call header() if output has been sent. Including whitespace.
LegionSmith
Forum Newbie
Posts: 5
Joined: Thu Mar 15, 2007 11:30 pm

Post by LegionSmith »

Yeah, my fault. I missed that last space in your code. I got it working after your last post.

Thanks!
Post Reply