Hey there,

I have an email script that is not functioning for me. It gets to line 78, where to actually goes to send the mail, but the SMTP server rejects it with the error code 554. Any ideas on where the formating for the email is getting messed up?

Thank Again

<?php

$mailto = 'subscriptions@XXXX.com' ;
$subject = 'Subscribe - Website Newsletter Signup' ;
$formurl = 'http://XXXX.com/newsletter.html' ;
$thankyouurl = 'http://XXXX.com/sent.html' ;
$errorurl = 'http://XXXX.com/error.html' ;
$email_is_required = 1;
$name_is_required = 0;
$comments_is_required = 0;
$uself = 0;
$use_envsender = 1;
$use_sendmailfrom = 1;
$smtp_server_win = '' ;
$use_webmaster_email_for_from = 0;
$use_utf8 = 1;
$my_recaptcha_private_key = '' ;

define( 'MAX_LINE_LENGTH', 50 );
$headersep = (!isset( $uself ) || !$uself) ? "\r\n" : "\n" ;
$content_type = (!isset( $use_utf8 ) || ($use_utf8 == 0)) ? 'Content-Type: text/plain; charset="iso-8859-1"' : 'Content-Type: text/plain; charset="utf-8"' ;
if (!isset( $use_envsender )) { $use_envsender = 0 ; }
if (isset( $use_sendmailfrom ) && $use_sendmailfrom) {
	ini_set( 'sendmail_from', $mailto );
}
if (isset( $smtp_server_win ) && strlen($smtp_server_win)) {
	ini_set( 'SMTP', $smtp_server_win );
}
$envsender = "-f$mailto" ;
$companyname = (isset($_POST['companyname']))? $_POST['companyname'] : $_POST['name'] ;
$email = $_POST['email'] ;
$contactfirstname = $_POST['contactfirstname'] ;
$contactlastname = $_POST['contactlastname'] ;
$phonenumber = $_POST['phonenumber'] ;
$http_referrer = getenv( "HTTP_REFERER" );

if (!isset($_POST['email'])) {
	header( "Location: $formurl" );
	exit ;
}
if (($email_is_required && (empty($email) || !preg_match('/@/', $email))) || ($name_is_required && empty($companyname)) || ($comments_is_required && empty($comments))) {
	header( "Location: $errorurl" );
	exit ;
}
if ( preg_match( "/[\r\n]/", $companyname ) || preg_match( "/[\r\n]/", $email ) ) {
	header( "Location: $errorurl" );
	exit ;
}
if (strlen( $my_recaptcha_private_key )) {
	require_once( 'recaptchalib.php' );
	$resp = recaptcha_check_answer ( $my_recaptcha_private_key, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field'] );
	if (!$resp->is_valid) {
		header( "Location: $errorurl" );
		exit ;
	}
}
if (empty($email)) {
	$email = $mailto ;
}
$fromemail = (!isset( $use_webmaster_email_for_from ) || ($use_webmaster_email_for_from == 0)) ? $email : $mailto ;

if (function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc()) {
	$comments = stripslashes( $comments );
}

$messageproper =
    "#Company Name#: $companyname\n" . 
	"#E-Mail#: $email\n" . 
    "#First Name#: $contactfirstname\n" . 
    "#Last Name#: $contactlastname\n" . 
    "#Phone Number#: $phonenumber\n"

$headers =
	"From: \"Website\" <website@XXXX.com>" . $headersep . "Reply-To: \"$companyname\" <$email>" . $headersep . "X-Mailer: website" .
	$headersep . 'MIME-Version: 1.0' . $headersep . $content_type ;

if ($use_envsender) {
	mail($mailto, $subject, $messageproper, $headers, $envsender );
}
else {
	mail($mailto, $subject, $messageproper, $headers );
}
header( "Location: $thankyouurl" );
exit ;

?>

HTML of the Form

<div style="position:absolute; left:284px; top:267px; width:458px; height:282px;">
    <div id="frag_9" style="text-align:left;">
    <form action="http://XXXX.com/scripts/feedback.php" method="post">
    <table border="0" cellpadding="8" cellspacing="8">
    <tr><td><label for="companyname">Company Name:*</label>:</td><td><input type="text" name="companyname" id="companyname" size="25" /></td></tr>
    <tr><td><label for="email">E-mail Address</label>:</td><td><input type="text" id="email" name="email" size="25" /></td></tr>
    <tr><td><label for="contactfirstname">Contact First Name:</label>:</td><td><input type="text" name="contactfirstname" id="contactfirstname" size="25" /></td></tr>
    <tr><td><label for="contactlastname">Contact Last Name:</label>:</td><td><input type="text" name="contactlastname" id="contactlastname" size="25" /></td></tr>
    <tr><td><label for="phonenumber">Phone Number:</label>:</td><td><input type="text" name="phonenumber" id="phonenumber" size="25" /></td></tr>
    <tr>
    <td align="center" colspan="2">
    <input type="submit" value="Submit" /><br />
    </td>
    </tr>
    </table>
    </form>
    </div></div>

    midlandtruck,

    554 is a vague response. You will need to post the entire error string, along with 554 there should be a reason for rejection.

    It doesn't exclusively mean there is something wrong with your code.

    It might be being rejected for policy reason, invalid mailbox, etc.

    Once we see the entire error message we may be able to help.

      Here is the entire string:

      PHP Warning: mail() [function.mail]: SMTP server response: 554 Message refused. in C:\inetpub\wwwroot\XXXX\feedback.php on line 76

      Thanks for the help.

        midlandtruck,

        Windows servers have to be configured via php.ini in order to use the mail() command out of the box.

        If this is a hosted environment, your web host can provide you details on the SMTP information.

        I would suggest Swiftmailer if it uses an external SMTP server as it follows RFC standards pretty well (from my experience).

        If this is your own server, generally from what I know Windows uses an SMTP Virtual Server, not an actual server, which relays mail through an outside host.

        you can use gmail as an smtp server but the from address must match the address that you are sending from.

        This may also be the case for the account you have set up, the mailbox might need to exist.

        Let me know if this helps at all...

          Thanks again for the help. I probably should have provided more info. No, the problem is with he script, there is no doubt. I run a VPS, and I have other websites that are able to use the mail function fine. Since all of the other applications work, I must have made a mistake with the script. I also tested the scripted on two other shared hosting sites for the buisness, and no effect. The problem is with the script. Thanks again though.

            5 days later
            Write a Reply...