【问题标题】:How to send a mail from PHP using gmail?如何使用 gmail 从 PHP 发送邮件?
【发布时间】:2011-05-30 00:29:27
【问题描述】:

我是 PHP 的初学者,但我想向@yahoo.com、@gmail.com 和其他类似的电子邮件地址发送邮件。我读过一些教程,但我没有 SMTP 服务器(我什至不知道那是什么),但我读过可以通过 GMail (smtp.gmail.com) 发送的地方。这怎么可能 ?

我在 Windows 7 上运行 Apache 服务器。

【问题讨论】:

标签: apache send php


【解决方案1】:

我已经在我的 Windows 7 环境中实现了msmtp.sourceforge.net,并结合了一个谷歌帐户,就像做梦一样。

The configuration file you'll need for Gmail is as follows:

A system wide configuration is optional.
     # If it exists, it usually defines a default account.
     # This allows msmtp to be used like /usr/sbin/sendmail.
     account default

     # The SMTP smarthost.
    host smtp.gmail.com
    domain smtp.gmail.com
    tls on
    tls_certcheck off
    tls_starttls on
    auth on
    user user@domain.co.uk
    from user@domain.co.uk
    password yourpasswordhere
    port 587
    logfile C:\msmtp\msmtplog.txt

     # Construct envelope-from addresses of the form "user@oursite.example".
     auto_from on
     maildomain user@domain.co.uk

在为 msmtp 提供的文档中提供了其他信息,但本质上是下载的文件、此配置和对 php.ini 文件的轻微调整,您应该可以继续使用。

【讨论】:

  • 这如何实现?
  • 我已更新问题以反映所需的一些配置。
【解决方案2】:

您可以使用 PHPMailer 来完成。这是tutorial

【讨论】:

    【解决方案3】:

    【讨论】:

    • 我试过了,但在 require_once "Mail.php" 处出现错误;
    • 那条线是什么?什么是mail.php
    【解决方案4】:

    使用 phpmailer 类 这是示例代码 下载phpmailer类转到http://sourceforge.net/projects/phpmailer/

        require('./class.phpmailer/class.phpmailer.php');
    if(isset($_GET['name'])&&isset($_GET['email'])&&isset($_GET['message']))
    {
    define('GUSER', 'youremail@gmail.com'); // GMail username
    define('GPWD', 'yourgmailpass'); // GMail password  
    
    
    function smtpmailer($to, $from, $from_name, $subject, $body) { 
          global $error;
          $mail = new PHPMailer();  // create a new object
          $mail->IsSMTP(); // enable SMTP
          $mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
          $mail->SMTPAuth = true;  // authentication enabled
          $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
          $mail->Host = 'smtp.googlemail.com';
          $mail->Port = 465; 
          $mail->Username = GUSER;  
          $mail->Password = GPWD;           
          $mail->SetFrom($from, $from_name);
          $mail->Subject = $subject;
          $mail->Body = $body;
          $mail->AddAddress($to);
          if(!$mail->Send()) { echo 'error';}
         else{echo 'message send';}
    }
    $name="sender : ".$_GET['email']."";
        $message=$name."\n".$_GET['message'];
        $subject="a Message from :".$_GET['name'];
    
        smtpmailer('Destinationemail@yahoo.com', '', 'youremail@mail.com',$subject ,$message , $message); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 2017-05-25
      • 2015-10-22
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多