PHP uses PEAR_SMTP to send mail messages timed to send Python email Outlook timed to send

Source: Internet
Author: User
Tags learn php pear
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 (smtp.163.com). 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 = "smtp.163.com"; $port = "";  $username = "test@163.com";  $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 = "toaddress@xpertdeveloper.com"; Setting up the headers$headers["from"] = $from; $headers ["to"] = $to; $headers ["Subject"] = $subject; $headers ["reply-to"] = "reply@address.com"; $headers ["content-type"] = "text/plain; CHARSET=ISO-2022-JP ";  $headers ["return-path"] = "returnpath@address.com"; Setting up the SMTP setting$smtp_info["host"] = "smtp.server.com"; $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, please configure the path of the pear, download the NET_SMTP package
Choose a different setup method based on your operating system in the php.ini file

; UNIX: "/path1:/path2" include_path = ".:/php/pear";; Windows: "\path1;\path2"; include_path = ".; C:\php\pear "Require ' net/smtp.php '; $host = ' 126.com ';//smtp server's IP or domain name $username= ' arcow ';//Login SMTP server username $password= '  Secret ';//Login SMTP server password $from = ' arcow@126.com '; Who sent the message $rcpt = Array (' test@test.com ', ' arcow@126.com ');//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.

The above describes the use of PHP pear_smtp send mail, including the sending of e-mail, pear aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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: info-contact@alibabacloud.com 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.