views:

1312

answers:

4

Hey, I've tried several variations of ASP & PHP native & 3rd party mailer programs. I have hit a wall at almost every point between setting ini parameters from within the scripts to permission denied/transport fails.

I would be wasting time to show my examples for what I've tried so far. All my searches produce too many results with too many forums with too many 3rd party installations. I want to find the simplest way to set this up.

I don't care about fancy html formatting, attachments, colors, radio buttons, check boxes, etc... I would just love to find something with one text form field and a submit button that upon clicking it will send a simple email to me. I thought this would be a lot easier, but I never worked with IIS servers before.

A step by step approach or a link to a tested true resource would be greatly appreciated.


EDIT
The closest I got, with the least cryptic of error messages was with this asp code from tizag...

<%
'Sends an email
Dim mail
Set mail = Server.CreateObject("CDO.Message")
mail.To = Request.Form("To")
mail.From = Request.Form("From")
mail.Subject = Request.Form("Subject")
mail.TextBody = Request.Form("Body")
mail.Send()
Response.Write("Mail Sent!")
'Destroy the mail object!
Set mail = nothing
%>

The error I get back is:

CDO.Message.1 error '80040220'

The "SendUsing" configuration value is invalid.
+2  A: 

For ASP.NET:

Install ASP.NET - I will assume that you have version 2, 3, or 3.5 of .NET installed. In a command prompt:

cd \Windows\Microsoft.NET\Framework\v2.0.50727
aspnet_regiis.exe -i

Create a file in the appropriate virtual directory (maybe inetpub\wwwroot?) called email.aspx with the following text:

<%@ Page language="c#"  %>
<%@ Import Namespace="System.Net.Mail" %>

<script runat="server">
    protected void SendButton_Click(object sender, EventArgs e)
    {
     MailMessage message = new MailMessage(
               "[email protected]",
               FromEmail.Text,
               Subject.Text,
               Message.Text);

     SmtpClient client = new SmtpClient("127.0.0.1");
     client.Send(message);
    }
</script>

<html>
    <body>
     <form runat="server">
      <table>
       <tr>
        <td>From Email:</td>
        <td><asp:TextBox id="FromEmail" runat="server" /></td>
       </tr>
       <tr>
        <td>Subject:</td>
        <td><asp:TextBox id="Subject" runat="server" /></td>
       </tr>
       <tr>
        <td>Message:</td>
        <td><asp:TextBox id="Message" runat="server" TextMode="MultiLine" /></td>
       </tr>
       <tr>
        <td></td>
        <td><asp:Button id="SendButton" runat="Server" Text="Send" OnClick="SendButton_Click" /></td>
       </tr>
      </table>
     </form> 
    </body> 
</html>

You will need to change the string "127.0.0.1" to the IP address of your SMTP server and you will need to change the "[email protected]" to the email address that you would like the mail sent to.

You could also require that the user enters an email address (and a valid one), a subject, and a message, by adding RequiredFieldValidator's and a RegularExpressionValidator.

consultutah
runtime error on this one.... Server Error in '/' Application.
CheeseConQueso
Does it say what the error is?
consultutah
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".
CheeseConQueso
Create a file called web.config with the following:<configuration> <system.web> <customErrors mode="Off"> </system.web></configuration>
consultutah
+1  A: 

Sending email in ASP using the CDO.Message object is really easy.

Here's a more complex sample with error handling and email address validation.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<% Option Explicit %>
<%
    Dim bPostback : bPostback = request.ServerVariables("REQUEST_METHOD") = "POST"
    Dim sResult : sResult = ""

    If bPostBack Then
     Dim sSmtpServer : sSmtpServer = "your.smtp.server"
     Dim sToEmail : sToEmail = "[email protected]"
     Dim sFromEmail : sFromEmail = request.Form("email")
     Dim sName : sName = request.Form("name")
     Dim sSubject : sSubject = request.Form("subject")
     Dim sBody : sBody = request.Form("body")

     Dim oEmailer : Set oEmailer = New Emailer

     oEmailer.SmtpServer = sSmtpServer
     oEmailer.FromEmail = sFromEmail
     oEmailer.ToEmail = sToEmail
     oEmailer.Subject = sSubject
     'oEmailer.HTMLBody = ""
     oEmailer.TextBody = sBody


     If oEmailer.sendEmail Then
      sResult = "Email sent!"
     Else
      sResult = "Something went terribly wrong!(" + oEmailer.ErrMsg + ")"
     End If
    End If

    'Simple Emailer
    Class Emailer
     Private oCDOM
     Public FromEmail
     Public ToEmail
     Public Subject
     Public HTMLBody
     Public TextBody
     Public SmtpServer
     Public Port
     Public ErrMsg

     Private Sub Class_Initialize()
      Set oCDOM = Server.CreateObject("CDO.Message")

      TextBody = ""
      'HTMLBody = ""
      Port = 25
     End Sub

     'SQL IsNull equivalent
     Private Function ifNull(vVar1, vVar2)
      If Not IsNull(vVar1) Then
       ifNull = vVar1
      Else
       ifNull = vVar2
      End If
     End Function

     'Verifies that the Email address conforms to RFC standard.
     Public Function isEmail(emailStr)
      isEmail = False
      If IsNull(emailStr) Then Exit Function

      Dim emailPat : emailPat = "^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$"
      Dim loRE : Set loRE = New RegExp

      loRE.IgnoreCase = True
      loRE.Global = True
      loRE.Pattern = emailPat

      If loRE.Test(emailStr) Then isEmail = True
      Set loRE = Nothing
     End Function

     Public Function sendEmail()
      sendEmail = True

      'On Error Resume Next

      If IfNull(SmtpServer,"") = "" Then sendEmail = False
      If Not isEmail(FromEmail) Then sendEmail = False
      If Not isEmail(ToEmail) Then sendEmail = False
      If IfNull(Trim(Subject),"") = "" Then sendEmail = False
      If IfNull(Trim(HTMLBody),"") = "" And IfNull(Trim(TextBody),"") = "" Then sendEmail = False
      If sendEmail = False Then Exit Function

      oCDOM.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
      oCDOM.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")=SmtpServer
      oCDOM.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=CINT(Port)
      oCDOM.Configuration.Fields.Update

      oCDOM.From = FromEmail
      oCDOM.To = ToEmail
      oCDOM.Subject = Subject
      'oCDOM.HTMLBody = "<div>" & HTMLBody & "<div>"
      oCDOM.TextBody = TextBody

      oCDOM.Send

      If Err.Number <> 0 Then
       ErrMsg = Err.description
       Err.Clear
       sendEmail = False
       Exit Function
      Else
       sendEmail = True
       Exit Function
      End If
     End Function

     Private Sub Class_Terminate()
      Set oCDOM = Nothing
     End Sub
    End Class
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
     <title>Emailer</title>
    </head>
    <body>
     <p><strong><%=sResult%></strong></p> 
     <form action="" method="post" target="_self">
      <label for="name">Name:</label><input id="name" name="name" type="text" size="50" maxlength="50" />
      <br />
      <label for="email">Email:</label><input id="email" name="email" type="text" size="50" maxlength="1024" />
      <br />
      <label for="subject">Subject:</label><input id="subject" name="subject" type="text" size="50" maxlength="1024" />
      <br />
      <label for="body">Body:</label>
      <br />
      <textarea id="body" name="body"></textarea>
      <br />
      <input name="Submit" value="Submit" type="submit" />
     </form>
    </body>
</html>
CptSkippy
CDO.Message.1 error '80040213'The transport failed to connect to the server.
CheeseConQueso
Stupid question, did you replace "your.smtp.server" on Line 8 with your SMTP server? Does your SMTP server require authentication to send email?
CptSkippy
yeah haha... dont know about the auth... were going to check over the server settings on monday
CheeseConQueso
+1  A: 

For your ASP example, I think you are just missing the smtp server configuration:

<%
'Sends an email
Dim mail
Set mail = Server.CreateObject("CDO.Message")
mail.To = Request.Form("To")
mail.From = Request.Form("From")
mail.Subject = Request.Form("Subject")
mail.TextBody = Request.Form("Body")

mail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
'Name or IP of remote SMTP server
mail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.server.com"
'Server port
mail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25 
mail.Configuration.Fields.Update


mail.Send()
Response.Write("Mail Sent!")
'Destroy the mail object!
Set mail = nothing
%>

As in the ASP.NET example, you would need to change "smtp.server.com" to your SMTP server's IP address.

consultutah
Microsoft VBScript runtime error '800a01a8'Object required: 'myMail'
CheeseConQueso
Fixed above. Sorry for the typo's
consultutah
+1  A: 

PHP: use the builtin mail (...) method. If your not doing anything fancy you can use it directly.

Example:

form.php
<?php
if($_REQUEST['action']=='mail'){
    $to="[email protected]";
    $from="[email protected]";
    $subject="this is my subject";
    $body=$_POST['body'];
    $headers="From: $from\r\nTo: $to\r\nSubject: $subject\r\n";

    if (stristr($to,"Content-Type")||stristr($subject,"Content-Type")||stristr($body,"Content-Type")||stristr($headers,"Content-Type")) {
           header("HTTP/1.0 403 Forbidden");
           echo "YOU HAVE BEEN BANNED FROM ACCESSING THIS SERVER FOR TRIGGERING OUR SPAMMER TRAP";
           exit;
    }

    if(mail($to,$subject,$body,$headers)){
         echo("Succesfully mailed to $to");
         exit();
    }else{
         echo("Failed to send mail<br>");
    }         
}
?>
<form method=post>
    <input type=hidden name=action value=mail>
    <textarea name=body rows=4 cols=40><?php echo($_POST['body']); ?></textarea>
    <input type=submit value=Send>
</form>

PS: The Content-Type check is to prevent someone from hijacking your email form.

PSS: This will use the mailserver as set in php.ini, that is the only setting that should matter.

Ben Reisner
I tried this one before....... Could not execute mail delivery program
CheeseConQueso