Posted on: November 5th, 2008 Mail Functions in PHP and ASP
PHP - Using mail function in PHP is much simpler than ASP. But, the email always go to the junk/bulk/spam filter for some emails like Hotmail and MSN. Also, different with ASP, mail function in PHP doesn’t have the authentication for username and password for mail server.
// receiver email
$to = receiver@mail.com ;
// email subject
$subject = “This is for the mail subject”;
// email message
$message = “The message goes here”;
// email headers. kind of long, to prevent from spam filter.
// works for gmail and yahoo, still caught by hotmail and MSN.
// note: Cc and Bcc below are optional
$headers = “Return-path: “.”\n”;
$headers .= “Reply-to: “.”\n”;
$headers .= “Content-Type: text/html; charset=windows-1252\n”;
$headers .= “Content-Transfer-Encoding: 7bit\n”;
$headers .= “From: Email Sender “.”\n”;
$headers .= “Cc: name@mail.com”.”\n”;
$headers .= “Bcc: name@mail.com”.”\n”;
$headers .= “X-Priority: 3\n”;
$headers .= “X-Mailer: Some X-Mailer Version 2.01\n”;
$headers .= “MIME-Version: 1.0\n”;
$headers .= “\n\n”;
// if the mail sent
if(mail($to, $subject, $message, $headers))
{
echo “Message sent!!!.”;
}
// if mail not send
else
{
echo “Message not sent. Please make sure you’re not
running this on localhost and also that you
are allowed to run mail() function from your webserver”;
}
ASP - I wrote the mail function on a function, to use it just simply call the function. On ASP mail function, I included the METADATA, and it suppose to prevent the mail caught by spam filter. Also ASP mail function has authentication with username and password.
‘ call the function
call sendEmail(mail.domain.com, sender@mail.com, somePassword, sender@mail.com, receiver@mail.com, emailSubject, cc@mail.com, bcc@mail.com, sender@mail.com, emailMessage)
‘ the function. note the METADATA and the authentication with username and password
Sub SendEmail(SMTPServer, SendUserName, SendPassword, EmailFrom, EmailSendTo, Subject, SendCc, SendBcc, SendReplyTo, HTMLText)
%>
<!–
METADATA
TYPE=”typelib”
UUID=”CD000000-8B95-11D1-82DB-00C04FB1625D”
NAME=”CDO for Windows 2000 Library”
–>
<%
Set cdoConfig = CreateObject(”CDO.Configuration”)
With cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = SMTPServer
.Item(cdoSMTPAuthenticate) = 1
.Item(cdoSendUsername) = SendUserName
.Item(cdoSendPassword) = SendPassword
.Update
End With
Set cdoMessage = CreateObject(”CDO.Message”)
With cdoMessage
Set .Configuration = cdoConfig
.From = EmailFrom
.To = EmailSendTo
.Subject = Subject
.Cc = SendCc
.Bcc = SendBcc
.ReplyTo = SendReplyTo
.HtmlBody = HTMLText
.Send
End With
Set cdoMessage = Nothing
Set cdoConfig = Nothing
End Sub