Difference between revisions of "Email in Scripts"

From Hostek.com Wiki
Jump to: navigation, search
(In Code)
(PHP Email Sample)
 
(2 intermediate revisions by one other user not shown)
Line 16: Line 16:
 
' ** SET AND UPDATE FIELDS PROPERTIES **
 
' ** SET AND UPDATE FIELDS PROPERTIES **
 
With objCDOConf
 
With objCDOConf
' ** OUT GOING SMTP SERVER **
+
' ** OUTGOING SMTP SERVER **
  
' ** SMTP PORT **
+
' ** SMTP SERVER **
 
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpmailer.hostek.net"
 
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpmailer.hostek.net"
  
' ** CDO PORT **
+
' ** SMTP PORT **
 
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
 
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  
' ** TIMEOUT **
 
 
.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 
.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  
 +
' ** TIMEOUT **
 
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
 
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
  
Line 38: Line 38:
 
' ** UPDATE THE CDOSYS CONFIGURATION **
 
' ** UPDATE THE CDOSYS CONFIGURATION **
 
Set iMsg.Configuration = objCDOConf
 
Set iMsg.Configuration = objCDOConf
 
  
  
Line 78: Line 77:
 
==PHP Email Sample==
 
==PHP Email Sample==
  
 +
<pre>
 +
<?php
 +
 +
$EmailBody = '';
 +
 +
if (isset($_POST['submit'])) {
 +
 +
foreach($_POST as $key => $value)
 +
{
 +
if ($key != 'submit'){
 +
if($value != ""){
 +
$EmailBody .= $key . ": " . $value . "<br />";
 +
}
 +
}
 +
 +
}
 +
}
 +
 +
?>
 +
<!-- Example Form -->
 +
<form action="" method="post">
 +
First Name:<br />
 +
<input type="text" name="First Name"><br /><br />
 +
Last Name: <br />
 +
<input type="text" name="Last Name"><br /><br />
 +
Phone: <br />
 +
<input type="text" name="Phone"><br /><br />
 +
Email Address: <br />
 +
<input type="text" name="Address"><br />
 +
<br />
 +
<input type="submit" name="submit" value="Send">
 +
<br />
 +
<br />
 +
<!-- End Example Form -->
 +
<?php
 +
 +
if($EmailBody != "") {
 +
 +
    $fromAddress = "example@example.com";
 +
    $toAddress = "example@example2.com";
 +
 +
    $headers .= "MIME-Version: 1.0\r\n";
 +
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
 +
    $headers .= "From: " . $fromAddress ." < " . $fromAddress . ">\r\n";
 +
    $headers .= "To: " . $toAddress . " < " . $toAddress . ">\r\n";
 +
    $headers .= "Reply-To: " . $fromAddress . " < " . $fromAddress . ">\r\n";
 +
    $headers .= "X-Priority: 1\r\n";
 +
    $headers .= "X-MSMail-Priority: High\r\n";
 +
    $headers .= "X-Mailer: Example Server";
 +
 +
if (mail($toAddress, "Example Email Form", $EmailBody, $headers)) {
 +
      echo "Mail sent to ".$toAddress.".<br /><br />\n";
 +
    }
 +
    else {
 +
      echo "Error sending mail.<br />\n";
 +
    }
 +
}
 +
?>
 +
</pre>
  
 
[[Category:Email]]
 
[[Category:Email]]

Latest revision as of 17:17, 13 March 2015

Sending Email via Scripts

To send email via code or scripts (ie web pages) you can use either the built in SMTP host smtpmailer.hostek.net which doesn't require a username/password (only from the server) or you can authenticate using your email account, which does require the username and password too.


ASP Email Sample

Here is a sample using CDOSYS to send email from ASP pages. CDOSYS replaces CDONTS on Windows 2003 servers and after.

NOTE: We suggest using CDOSYS for all ASP email applications.

<%
Dim objCDOConf
Set objCDOConf = Server.CreateObject ("CDO.Configuration")

' ** SET AND UPDATE FIELDS PROPERTIES **
With objCDOConf
' ** OUTGOING SMTP SERVER **

' ** SMTP SERVER **
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpmailer.hostek.net"

' ** SMTP PORT **
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

' ** TIMEOUT **
.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

.Fields.Update
End With


Dim iMsg
set iMsg = CreateObject("CDO.Message")

' ** UPDATE THE CDOSYS CONFIGURATION **
Set iMsg.Configuration = objCDOConf


' apply settings to the message
With iMsg
  .To = "test@some_address.com"
  .From = "test@your_domain.com"
  .Subject = "This is a subject"
  .TextBody = "This is the body of the email."
  .Send
End With

' cleanup of variables
Set iMsg = Nothing
%>

.NET Email Sample

In Code

 MailMessage message = new MailMessage("from@email.address","to@email.address");
 message.Subject = "subject";
 message.Body = "body";
 SmtpClient client = new SmtpClient();
 client.Send(message);

In web.config

  <system.net>
     <mailSettings>
        <smtp deliveryMethod="Network">
           <network host="smtpmailer.hostek.net" port="25" />
        </smtp>
     </mailSettings>
  </system.net>

ColdFusion Email Sample

PHP Email Sample

<?php 

$EmailBody = '';

if (isset($_POST['submit'])) {

	foreach($_POST as $key => $value) 
	{		
		if ($key != 'submit'){
			if($value != ""){
				$EmailBody .= $key . ": " . $value . "<br />";	
			}
		}
		
	}
}

?>
<!-- Example Form -->
<form action="" method="post">
First Name:<br />
<input type="text" name="First Name"><br /><br />
Last Name: <br />
<input type="text" name="Last Name"><br /><br />
Phone: <br />
<input type="text" name="Phone"><br /><br />
Email Address: <br />
<input type="text" name="Address"><br />
<br />
<input type="submit" name="submit" value="Send">
<br />
<br />
<!-- End Example Form -->
<?php

if($EmailBody != "") {

    $fromAddress = "example@example.com";
    $toAddress = "example@example2.com";

    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= "From: " . $fromAddress ." < " . $fromAddress . ">\r\n";
    $headers .= "To: " . $toAddress . " < " . $toAddress . ">\r\n";
    $headers .= "Reply-To: " . $fromAddress . " < " . $fromAddress . ">\r\n";
    $headers .= "X-Priority: 1\r\n";
    $headers .= "X-MSMail-Priority: High\r\n";
    $headers .= "X-Mailer: Example Server";

	if (mail($toAddress, "Example Email Form", $EmailBody, $headers)) {
      		echo "Mail sent to ".$toAddress.".<br /><br />\n";
    } 
    else {
      		echo "Error sending mail.<br />\n";
    }
}
?>