Archive
Sending email using PHP, C#, VB
Headers are the parts seen at the top of emails. Typically :
| To : | A comma seperated list of recipient emails. |
| From : | The senders email address. |
| Reply-To : | The email address where replies should be sent to. |
| Return-Path : | Kinda the same thing as the Reply-To. Some email clients require this, others create a default. |
| Subject : | Subject of the email. |
| CC : | Carbon Copy. A comma seperated list of more recipients that will be seen by all other recipients. |
| BCC : | Blind Carbon Copy. A comma seperated list of more recipients that will not be seen by any other recipients. |
The TO and SUBJECT filds have already been discussed as the first and second variables in the MAIL command. The extra headers seen above can be entered as a 4th variable in the MAIL command.
A way to send email using PHP is this :
mail("yourplace@somewhere.com","My email test.","Hello, how are you?","From: myplace@here.com\r\nReply-To: myplace2@here.com\r\nReturn-Path: myplace@here.com\r\nCC: sombodyelse@noplace.com\r\nBBC: hidden@special.com\r\n");
Now this looks kinda messy when it’s all in there without using variables. That above example has all of the extra headers being declared. You may specify all or some or none as you desire. There are 2 very important things to note here :
| 1. | These headers need their NAMES: shown unlike the first three variables in the mail command. The header names are CaSe SeNsItIvE. Use per example. |
| 2. | Each header is ended with the Return and Newline characters. \r\n There are no spaces between each header. |
For better organization, these extra headers can be declared in a variable string like our previous examples.
$to = "yourplace@somewhere.com";
$subject = "My email test.";
$message = "Hello, how are you?";
$headers = "From: myplace@here.com\r\n";
$headers .= "Reply-To: myplace2@here.com\r\n";
$headers .= "Return-Path: myplace@here.com\r\n";
$headers .= "CC: sombodyelse@noplace.com\r\n";
$headers .= "BCC: hidden@special.com\r\n";
if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>
[ C# ]
MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body.";
mail.Headers.Add( "Reply-To", "alternate_email@mycompany.com" );
SmtpMail.SmtpServer = "localhost"; //your real server goes here SmtpMail.Send( mail );
[ VB.NET ]
Dim mail As New MailMessage()
mail.To = "me@mycompany.com"
mail.From = "you@yourcompany.com"
mail.Subject = "this is a test email."
mail.Body = "this is my test email body."
mail.Headers.Add("Reply-To", "alternate_email@mycompany.com")
SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(mail)
Using count directly in loops/cycles is wrong (PHP)
Count is a PHP command that … counts all elements in an array, or properties in an object.
But when you need to use it in loops, first assign a new variable then use the variable in the loop. For example :
<?php
//size of $arr ~ 2000 elements
//WRONG variant (Time exec ~ 19 sec)
for($i=0;$i<count($arr);$i++)
{
//... loop code here ...
}
//RIGHT variant(Time exec ~ 0.2 sec)
$arr_size=count($arr);
for($i=0;$i<$arr_size;$i++)
{
//... loop code here ...
} ?>
Another example :
<?php
for ($i=0; $i<10000; $i++) {
$arr[] = $i;
}
$time11 = microtime_float();
$bf = "";
for ($i=0; $i<count($arr); $i++) {
$bf .= $arr[$i]."\n";
}
$time12 = microtime_float();
$time1 = $time12 - $time11;
print "First: ".$time1."\n";
$time21 = microtime_float();
$l = count($arr);
for ($i=0; $i<$l; $i++) {
$bf .= $arr[$i]."\n";
}
$time22 = microtime_float();
$time2 = $time22 - $time21;
print "Second: ".$time2."\n";
?>
The output from the code above is (when run many times):
First: 0.13001585006714
Second: 0.099159002304077
First: 0.12128901481628
Second: 0.079941987991333
First: 0.18690299987793
Second: 0.13346600532532
As you can see, the second method (which doesnt use count() directly in the loop) is faster than the first method (which uses count() directly in the loop).
Diving into PHP, from install to SQL statements

-
Day 1: Installation
-
Day 2: Variables
-
Day 3: Passing Values From Page to Page
-
Day 4: Multiple Variables and the “foreach” Statement
-
Day 5: Refactoring, Arrays, and Functions – Oh My!
-
Day 6: Including Files
-
Day 7: Regular Expressions
-
Day 8: Strings
-
Day 9: First-Time Visitors
-
Day 10: Getting Started With MySql
-
Day 11: SQL Insert Statements
-
Day 12: The File System
MAMP : Mac – Apache – MySQL – PHP
The abbreviation "MAMP" stands for: Macintosh, Apache, Mysql and PHP.
With just a few mouse-clicks, you can install Apache, PHP and MySQL for Mac OS X



