PHP uses PEAR_SMTP to send mail, pear_smtp send mail _php tutorial

Source: Internet
Author: User
Tags pear

PHP uses PEAR_SMTP to send mail, pear_smtp send mail

PHP comes with the mail function compared to the egg pain, under win configured SendMail or unable to send mail. A third-party pear/mail can be used to send servers directly through SMTP connection messages. such as ( It is therefore not necessary to install similar software such as SendMail on this machine.
Make sure that the Pear Mail package is installed.

<?php  require_once "vendor/autoload.php";    $from = "Test
  ";  $to = "Test 
   ";  $subject = "hi!";  $body = "Hi,\n\nhow is you?";    $host = ""; $port = "";  $username = "";  $password = "test123";    $headers = Array (' from ' + $from, ' to ' and ' = '   $to,   ' Subject ' = $subject);  $SMTP = mail::factory (' smtp ',   Array (' host ' = = $host,    ' port ' = = $port,    ' auth ' = "= True,   //') Debug ' =>true,    ' username ' = $username,    ' password ' and $password));    $mail = $smtp->send ($to, $headers, $body);    if (Pear::iserror ($mail)) {   echo ("

" . $mail->getmessage (). "

"); } else { echo ("

Message successfully sent!

"); } ? >

This is a non-encrypted method.
Phper most use the Mail function to send mail, but we can use the other SMTP server to send, it is recommended to use the PEAR ' s mail package to send mail.

 $subject = "This mail was sent from SMTP."; $mail _body = "The body of the The mail which is sent using SMTP.";  $from = "From:from Name"
 "; $to = "To:to Name 
    ";  $receiver = ""; Setting up the headers$headers["from"] = $from; $headers ["to"] = $to; $headers ["Subject"] = $subject; $headers ["reply-to"] = ""; $headers ["content-type"] = "text/plain; CHARSET=ISO-2022-JP ";  $headers ["return-path"] = ""; Setting up the SMTP setting$smtp_info["host"] = ""; $SMTP _info["port"] = "25"; $SMTP _info["auth"] = true; $SMTP _info["username"] = "smtp_user";  $SMTP _info["password"] = "Smtp_password";  Creating the PEAR Mail object: $mail _obj =& mail::factory ("SMTP", $smtp _info);  Sending the Mail now$mail_sent = $mail _obj->send ($receiver, $headers, $mail _body);
 If any error the "See for" Here:if (Pear::iserror ($mail _sent)) {print ($mail _sent->getmessage ());} 


A third case:

Before using the following source code, configure the path of the pear, download the NET_SMTP package
Choose a different setting method in the php.ini file depending on your operating system

; UNIX: "/path1:/path2" include_path = ".:/php/pear";; Windows: "\path1;\path2"; include_path = ".; C:\php\pear "Require ' net/smtp.php '; $host = ' ';//smtp server's IP or domain name $username= ' arcow ';//Login SMTP server username $password= '  Secret ';//Login SMTP server password $from = ' '; Who sent the message $rcpt = Array (' ', ' ');//Can be set multiple receivers $SUBJ = "Subject: who you are \ \"//Subject $body = "Test it";//message content/* Build a class */if (! ($smtp = new Net_smtp ($host))) {die ("Cannot initialize class net_smtp!\n");} /* Start connecting to the SMTP server */if (pear::iserror ($e = $smtp->connect ())) {Die ($e->getmessage (). "\ n");} /* SMTP requires authentication */$SMTP->auth ($username, $password, "PLAIN");/* Set Sender Mailbox */if (Pear::iserror ($smtp->mailfrom ($from )) {die ("unable to set the sender's mailbox to < $from >\n");}  /* Set the Mail Recipient */foreach ($rcpt as $to) {if (Pear::iserror ($res = $smtp->rcptto ($to))) {Die ("mail cannot be delivered to < $to;:". $res->getmessage ().  "\ n"); }}/* begins sending the message content */if (Pear::iserror ($smtp->data ($subj. "\ r \ n". $body)) {die ("Unable to send data\n");} /* Disconnect */$SMTP->disconnecT (); echo "sent successfully!" ";? >

The above is the entire content of this article, PHP use pear_smtp send mail three cases, I hope to learn PHP program for you to help.

Articles you may be interested in:

    • A class that sends mail through SMTP in PHP, tested by
    • Code that uses SMTP to send mail under PHP
    • Phpmailer message class using to send mail methods
    • PHP Mail failed to send mail through SMTP of Windows solution
    • PHP uses SMTP to send mail samples that support attachments
    • PHP implements an SMTP mail-sending class that supports SSL connections
    • How PHP uses Pear's own mail class library to send mail
    • PHP uses pear to send mail (Windows environment)
    • PHP uses pear to implement the mail Send function in Windows environment configuration pear true techarticle PHP uses PEAR_SMTP to send mail, pear_smtp send mail PHP comes with the mail function compared to the egg pain, under win configured SendMail or unable to send mail. And the use of third-party pear/mail can be straight ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.