webMethods Learner

Icon

Reading, understanding, and trying to write it down

Sending mail with PHP using SMTP-auth (PEAR package)

To protect essentials network, servers, and customers from spamming, it requires the use of SMTP-auth for all methods of sending email. The basic PHP Mail function does not support SMTP-auth, therefore cannot be used.

To write your own PHP code to send email using SMTP-auth, there are PHP PEAR Mail package available for usage (which supports smtp-auth). PHP code sample below shows how to use this feature to send an email:


require_once "Mail.php";

/**************************************************
EDIT the following variables for your own use
***************************************************/
$from = "user@domain";
$to = "user2@domain2";

$subject = "Hi!"; //type in subject here

$host = "host_ip"; // Your domain
$username = "user"; // Your user / full email address
$password = "password"; // Password to your email address

/**************************************************
***************************************************/

$body = "Test";

foreach($_POST as $a => $b)
{
$body .= $a .": ". $b . "\n";
}
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
/**************************************************
ERROR MESSAGE
***************************************************/
?>

getMessage(); ?>
/**************************************************/
} else {
/**************************************************
SUCCESS MESSAGE
***************************************************/
?>
Message successfully sent!
/**************************************************/
}


?>

grabbed from here :-)

Advertisement

Filed under: PHP, ,

2 Responses

  1. [...] kerjaan ke mas Yudhi. hahaha.. dan dapet donk. Mas Yudhi emang buaekk. Kemarin kan udh berhasil ngoprek PEAR::MAIL, sekarang misinya dilanjutkan dengan meng-install SVN (subversion) di server ubuntu tercintanya, [...]

  2. devilkazuma says:

    disitu kan ada require_once (“Mail.php”); tapi aku ga ngeliat ada file mail.php. Apakah mail.php itu bawaan dari phpnya atau gmana?

    Iya,
    Mail.php sudah bawaan PHP::PEAR.
    Biasanya ada di ../Pear/Mail/Mail.php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 

December 2008
M T W T F S S
    Jan »
1234567
891011121314
15161718192021
22232425262728
293031  
Follow

Get every new post delivered to your Inbox.