Php uses pear_smtp to send emails

Source: Internet
Author: User
Tags learn php programming
This article mainly introduces the materials related to sending mails using pear_smtp in php. the content is rich. if you are interested, please refer to the mail function provided by PHP, if sendmail is configured in win, the email cannot be sent. Third-party pear/mail can be used to directly connect to the mail sending server through smtp. For example (smtp.163.com ). Therefore, there is no need to install similar software such as sendmail on the local 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 are you?";    $host = "smtp.163.com"; $port = "25";  $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("
   

" . $mail->getMessage() . "

"); } else { echo("

Message successfully sent!

"); } ?>

This is a non-encrypted method.
PHPer mostly uses the mail function to send emails, but we can use other SMTP servers to send emails. we recommend that you use PEAR's mail package to send emails.

 $subject = "This mail is sent from SMTP.";$mail_body = "This is the body of 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 that here:if (PEAR::isError($mail_sent)) { print($mail_sent->getMessage());}
  
 

Case 3:

Before using the following source code, configure the pear path and download the net_smtp package.
In the php. ini file, select different settings based on your operating system.

; UNIX: "/path1:/path2" include_path = ". :. /php/pear "; Windows:" \ path1; \ path2 "; include_path = ".; c: \ php \ pear "require 'net/SMTP. php '; $ host = '2017. com '; // smtp server ip address or domain name $ username = 'arcow'; // username $ password = 'secret' for logging on to the smtp server '; // login smtp server password $ from = 'arcow @ 126.com '; // who sent the email $ rcpt = array ('test @ test.com', 'arcow @ 126.com '); // Multiple recipients can be set $ subj = "Subject: Who You Are"; // Subject $ body = "test it"; // mail content/* create a class */if (! ($ Smtp = new Net_SMTP ($ host) {die ("Class Net_SMTP cannot be initialized! \ N ");}/* start to connect to the SMTP server */if (PEAR: isError ($ e = $ smtp-> connect ())) {die ($ e-> getMessage (). "\ n");}/* smtp Authentication Required */$ smtp-> auth ($ username, $ password, "PLAIN "); /* Set the sender's mailbox */if (PEAR: isError ($ smtp-> mailFrom ($ from ))) {die ("the sender's email address cannot be set to <$ from> \ n");}/* Set the recipient */foreach ($ rcpt as $ to) {if (PEAR:: isError ($ res = $ smtp-> rcp.pdf ($ to) {die ("the email cannot be delivered to <$ to> :". $ res-> getMessage (). "\ n") ;}}/* start to send the mail 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 all the content in this article. three cases of sending emails using pear_smtp in php hope to help you learn php programming.

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.