PHP uses PEAR_SMTP to send mail _php tips

Source: Internet
Author: User
Tags auth getmessage learn php pear

PHP's own mail function compares the egg pain, the SendMail is configured under win or the message cannot be sent. Pear/mail with a third party can connect to the mail server directly via SMTP. such as (smtp.163.com). Therefore, it is not necessary to install SendMail and other similar software on this machine.
Make sure the Pear Mail package is installed.

<?php 
 require_once "vendor/autoload.php"; 
  
 $from = "test<test@163.com>"; 
 $to = "Test <test@outlook.com>"; 
 $subject = "hi!"; 
 $body = "Hi,\n\nhow Are You"; 
  
 $host = "smtp.163.com"; 
$port = "a"; 
 $username = "test@163.com"; 
 $password = "test123"; 
  
 $headers = Array (' From ' => $from, 
  ' to ' => $to, 
  ' Subject ' => $subject); 
 $SMTP = mail::factory (' smtp ', 
  Array (' Host ' => $host, 
   ' Port ' => $port, 
   ' auth ' => true, 
  //' Debug ' =>true, 
   ' username ' => $username, 
   ' password ' => $password)); 
  
 $mail = $smtp->send ($to, $headers, $body); 
  
 if (Pear::iserror ($mail)) { 
  echo ("<p>". $mail->getmessage (). "</p>"); 
 } else { 
  echo ("<p>message successfully sent!</p>"); 
 >

This is not an encryption method.
Phper most use mail functions to send mail, but we can use other SMTP servers to send, and here we recommend using PEAR's mail package to send mail.

 $subject = "This mail are sent from SMTP.";
$mail _body = "This is the" the ' mail which is sent using SMTP. " 
$from = "From:from Name <fromaddress@xpertdeveloper.com>"; 
$to = "To:to Name <toaddress@xpertdeveloper.com>"; 
 
$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 Here:if PEar::iserror ($mail _sent)) {print ($mail _sent->getmessage ());}
 

A third case:

Before using the following source code, please configure pear's path and download the NET_SMTP package.
Choose a different setup method for 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 mail $rcpt = Array (' test@test.com ', ' arcow@126.com '),//can set multiple recipients $subj = "Subject: who are you \ n"/subject $body = "Test it";//message content/ * Create a class * */if (! ($smtp = new Net_smtp ($host)))
{Die ("Unable to initialize class net_smtp!\n");} /* Start connecting SMTP server/if (pear::iserror ($e = $smtp->connect ())) {Die ($e->getmessage ()).
"\ n");
/* SMTP requires authentication/$SMTP->auth ($username, $password, "PLAIN"); /* Set the sender's mailbox/if (Pear::iserror ($smtp->mailfrom ($from)) {die ("Cannot set the sender's mailbox to < $from >\n");/* Set the recipient/foreach ($rcpt as $to) {if (pear::iserror ($res = $smtp->rcptto ($to))) {Die ("mail cannot be delivered to < $to:".) $res->getmessage ().
  "\ n"); /* Start Sending message content//if (Pear::iserror $smtp->data ($subj. "\ r \ n". $body))) {Die ("Unable to send datA\n ");
/* Disconnect/* $SMTP->disconnect (); echo "Send success!" ";?>

Above is the entire content of this article, PHP use pear_smtp send mail three cases, hope to learn PHP program design help.

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.