1<?php
2
3error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
4
5require_once "Mail.php";
6
7$host = "ssl://smtp.dreamhost.com";
8$username = "youremail@example.com";
9$password = "your email password";
10$port = "465";
11$to = "address_form_will_send_TO@example.com";
12$email_from = "youremail@example.com";
13$email_subject = "Subject Line Here:" ;
14$email_body = "whatever you like" ;
15$email_address = "reply-to@example.com";
16
17$headers = array ('From' => $email_from, 'To' => $to, 'Subject' => $email_subject, 'Reply-To' => $email_address);
18$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
19$mail = $smtp->send($to, $headers, $email_body);
20
21if (PEAR::isError($mail)) {
22echo("<p>" . $mail->getMessage() . "</p>");
23} else {
24echo("<p>Message successfully sent!</p>");
25}
26?>
27
1<?php
2
3$to = 'nobody@example.com';
4
5$subject = 'the subject';
6
7$message = 'hello';
8
9$headers = 'From: webmaster@example.com' . "\r\n" .
10
11 'Reply-To: webmaster@example.com' . "\r\n" .
12
13 'X-Mailer: PHP/' . phpversion();
14
15mail($to, $subject, $message, $headers);
16
17?>
1
2I migrated an application to a platform without a local transport agent (MTA). I did not want to configure an MTA, so I wrote this xxmail function to replace mail() with calls to a remote SMTP server. Hopefully it is of some use.
3
4function xxmail($to, $subject, $body, $headers)
5{
6 $smtp = stream_socket_client('tcp://smtp.yourmail.com:25', $eno, $estr, 30);
7
8 $B = 8192;
9 $c = "\r\n";
10 $s = 'myapp@someserver.com';
11
12 fwrite($smtp, 'helo ' . $_ENV['HOSTNAME'] . $c);
13 $junk = fgets($smtp, $B);
14
15 // Envelope
16 fwrite($smtp, 'mail from: ' . $s . $c);
17 $junk = fgets($smtp, $B);
18 fwrite($smtp, 'rcpt to: ' . $to . $c);
19 $junk = fgets($smtp, $B);
20 fwrite($smtp, 'data' . $c);
21 $junk = fgets($smtp, $B);
22
23 // Header
24 fwrite($smtp, 'To: ' . $to . $c);
25 if(strlen($subject)) fwrite($smtp, 'Subject: ' . $subject . $c);
26 if(strlen($headers)) fwrite($smtp, $headers); // Must be \r\n (delimited)
27 fwrite($smtp, $headers . $c);
28
29 // Body
30 if(strlen($body)) fwrite($smtp, $body . $c);
31 fwrite($smtp, $c . '.' . $c);
32 $junk = fgets($smtp, $B);
33
34 // Close
35 fwrite($smtp, 'quit' . $c);
36 $junk = fgets($smtp, $B);
37 fclose($smtp);
38}
39