How do I connect to the SMTP server to send emails?

posted by web10 on 2009-07-01 01:10:46
Hello,
I have a simple "contact us" form, is there a php function to connect to the SMTP server?
Bye
dail avatar
dail
12
Sure! You can use PHPMailer http://phpmailer.codeworxtech.com/
If you don't have any special needs, you can use this simple php email class.
 
class Email{
 
    var $from, $to, $message, $headers, $subject, $charset;
 
    public function __construct($from='', $to='', $subject='', $message='', $charset='utf-8'){
        $this->from    = $from;
        $this->to      = $to;
        $this->subject = $subject;
        $this->message = $message;
        $this->charset = $charset;
    }
 
    public function to($to){
        $this->to = $to;
    }
 
    public function from($from){
        $this->from = $from;
    }
 
    public function subject($subject){
        $this->subject=$subject;
    }
 
    public function message($message){
        $this->message = $message;
    }
 
    public function headers($type='plain'){
        if (isset($this->from)){
            if ($type=='html') 
                    $content_type = 'text/html';
            else 
                    $content_type = 'text/plain';
            $headers  = "From: ".$this->from."\n";
            $headers .= "MIME-Version: 1.0\n";
            $headers .= "Content-Type: $content_type; charset=\"".$this->charset."\"\n";
            $this->headers = $headers;
        }
    }
 
    public function to_html(){
        if (isset($this->message))
                $this->message = "<html><head></head><body>".nl2br($this->message)."</body></html>";
    }
 
    public function send(){         
        if (isset($this->from, $this->to, $this->message, $this->subject))
            mail($this->to, $this->subject, $this->message, $this->headers);
    }
}
/********************************* EXAMPLES *********************************/
//text/plain email
$email = new Email();
$email->from("from@from.com");
$email->to("to@to.it");
$email->subject("This is a text/plain email");
$email->headers('plain');
$email->send();
 
//text/html email
$email = new Email();
$email->from("from@from.com");
$email->to("to@to.com");
$email->subject("This is a text/html email");
$email->headers('html');
$email->to_html(); // Adds html tags to the message
$email->send();
 
//Short
$email = new Email($from, $to, $subject, $message);
$email->send();
 
49 answers - 0 questions
  Positive        Negative

Josware avatar
Josware
1
|| if you are note familiar with objects (class), you can use the PHP mail function (php.net/manual/en/function.mail.php)
 
<?php
/*
 *Summary: define a message, a subject and a destinatary, (the headers are not allways a must)
 *tip: append \r\n at the end of the message, it helps some times
 */
 
$message = "Hello:\n This is a sample \n Message" . "\r\n";
 
//The Reply-To, most of the cases is not needed, that is just in case the reply email is different from the sender
$headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
 
// Send
mail('YOUR_EMAIL@example.com', 'Your Subject', $message, $headers);
?> 
 
2 answers - 0 questions
+ 1  Positive        Negative



Answer the question



Top Users
  • dail (12)
  • livin52 (3)
  • camu (3)
  • softweb (2)
  • Nadine (1)
  • Josware (1)
  • lfelipecr (1)
  • gregoriohc (1)
  • Mitu (1)
  • ryan714 (1)