-
-
- include_once("class.phpmailer.php");
- /**
- * Define email module configuration information
- */
- define("SMTP_HOST","smtp.mail.yahoo.com "); // SMTP host
- define("SMTP_MAIL"," XXXX@yahoo.cn"); // SMTP user email
- define("SMTP_PASS"," XXXX"); // Password for SMTP
define("SERVICE_MAIL"," XXXX@yahoo.cn"); // SMTP user email
- define("SERVICE_NAME","PHPBOOK Email Test"); // SMTP name used
/**
- * Use phpmailer to send email module
- *
- * @param string $email
- * @param string $user
- * @param string $subject
- * @param string $body
- * @return bool
- */
- function sendMail($email,$user,$subject,$body)
- {
- $mail = new PHPMailer();
- //$this;
- $mail ->IsSMTP(); // Set up SMTP
- $mail->Host = SMTP_HOST; // Set SMTP server address
- $mail->SMTPAuth = true; // Turn on SMTP permission verification
- $mail-> Username = SMTP_MAIL; // SMTP username
- $mail->Password = SMTP_PASS; // SMTP server password
$mail->From = SERVICE_MAIL; // Set sender address
- $mail->FromName = SERVICE_NAME; // Set the sender's name
- $mail->AddAddress($email, $user); // Add the recipient's address
- $mail->AddReplyTo(SERVICE_MAIL, SERVICE_NAME); / / Set reply address
$mail->WordWrap = 50; // Set display format
- $mail->IsHTML(true); // Set email to support html
- $mail-> ;Subject = $subject;
- $mail->Body = $body;
- $mail->AltBody = ""; // Text type mail
if(!$mail- >Send())
- {
- return $mail->ErrorInfo;
- }
- return true;
- }
//Start sending test email: fsockopen() [function.fsockopen ]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/xiehui/admin/mail/class.smtp.php on line 89
- $tomail = " XXXX@126.com";
- $user = " XXXXlinux ";
- $_mailSubject = "Email test example!"; // Email subject group sent to the user
- $_mailBody = "Sina"; // Mail content team
- sendMail($tomail,$user,$_mailSubject,$_mailBody);
- ?>
-
Copy code
Experiments have proven that Yahoo's SMTP is very easy to use, but the so-called Sina one is actually not easy to use. I was stuck for a long time.
Method 4, given to the program written by socket
Encapsulation class for sending emails using socket:
-
- class sendmail{
- var $lastmessage; //Record the last response message returned
- var $lastact; //Last action in string form
- var $welcome; //Used in HELO Later, users are welcome
- var $debug; //Whether to display debugging information
- var $smtp; //smtp server
- var $port; //smtp port number
- var $fp; //socket handle
- //Send mail function
- function send_mail($smtp, $welcome="", $debug=false) {
- if(empty($smtp)) die("SMTP cannot be empty!");
- $this->smtp=$smtp;
- if(empty($welcome)) {
- $this->welcome=gethostbyaddr("localhost");
- }else
- $this->welcome=$welcome;
- $this->debug=$debug;
- $this->lastmessage="";
- $this->lastact="";
- $this->port="25";
- }
- //显示调试信息
- function show_debug($message, $inout) {
- if ($this->debug) {
- if($inout=="in"){ //响应信息
- $m='<< ';
- }else
- $m='>> ';
- if(!ereg("n$", $message))
- $message .= "
";
- $message=nl2br($message);
- echo "${m}${message}";
- }
- }
- //执行传递的命令
- function do_command($command, $code) {
- $this->lastact=$command;
- $this->show_debug($this->lastact, "out");
- fputs ( $this->fp, $this->lastact );
- $this->lastmessage = fgets ( $this->fp, 512 );
- $this->show_debug($this->lastmessage, "in");
- if(!ereg("^$code", $this->lastmessage))
- return false;
- else
- return true;
- }
- //邮件发送处理
- function send( $to,$from,$subject,$message) {
- //连接服务器
- $this->lastact="connect";
- $this->show_debug("连接到SMTP 服务器: ".$this->smtp, "out");
- $this->fp = fsockopen ( $this->smtp, $this->port );
- if ( $this->fp ) {
- $this->set_socket_blocking( $this->fp, true );
- $this->lastmessage=fgets($this->fp,512);
- $this->show_debug($this->lastmessage, "in");
- if (! ereg ( "^220", $this->lastmessage ) ) {
- return false;
- }else{
- $this->lastact="HELO " . $this->welcome . "n";
- if(!$this->do_command($this->lastact, "250")){
- fclose($this->fp);
- return false;
- }
- $this->lastact="MAIL FROM: $from" . "n";
- if(!$this->do_command($this->lastact, "250")){
- fclose($this->fp);
- return false;
- }
- $this->lastact="RCPT TO: $to" . "n";
- if(!$this->do_command($this->lastact, "250")){
- fclose($this->fp);
- return false;
- }
- //开始发送邮件正文
- $this->lastact="DATAn";
- if(!$this->do_command($this->lastact, "354")){
- fclose($this->fp);
- return false;
- }
- //开始处理邮件主题头
- $head="Subject: $subjectn";
- if(!empty($subject) && !ereg($head, $message)){
- $message = $head.$message;
- }
- //开始处理邮件From头
- $head="From: $fromn";
- if(!empty($from) && !ereg($head, $message)) {
- $message = $head.$message;
- }
- //开始处理邮件To头
- $head="To: $ton";
- if(!empty($to) && !ereg($head, $message)) {
- $message = $head.$message;
- }
- //处理结束串
- if(!ereg("n.n", $message))
- $message .= "n.n";
- $this->show_debug($message, "out");
- fputs($this->fp, $message);
- $this->lastact="QUITn";
- if(!$this->do_command($this->lastact, "250")){
- fclose($this->fp);
- return false;
- }
- }
- return true;
- }else{
- $this->show_debug("连接失败!!", "in");
- return false;
- }
- }
- }
- ?>
复制代码
Example of sending email using socket:
-
- include ("./sendmail.class.php");
- $mail = new sendmail();
- $email = "Hello, this is a test email!";
- $sendmail = new send_mail("smtp.mail.126.com","PHPBOOK",true); //Display the adjustment information
- if($mail->send("XXXX@126.com", "XXXX@ 126.com", "Test SOCKET email", $email)) {
- echo "Sent successfully!
";
- }else{
- echo "Failed to send!
";
- }
- ?>
Copy code
|