如何使用gmail从PHP发送邮件?

时间:2010-12-23 14:51:12

标签: apache send php

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

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

4 个答案:

答案 0 :(得分:1)

我在Windows 7环境中实施了msmtp.sourceforge.net并结合了Google帐户,就像梦一样。

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文件的轻微调整,你应该好好去。

答案 1 :(得分:0)

答案 2 :(得分:0)

您可以使用PHPMailer来执行此操作。这是tutorial

答案 3 :(得分:0)

使用phpmailer类 这是示例代码 下载phpmailer类goto 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);