如何使用php脚本发送电子邮件? [英] How to send an email using php script?

查看:176
本文介绍了如何使用php脚本发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
    require_once "Mail.php";
    $from = "<niko@gmail.com>";
    $to = "<niko@hotmail.com>";
    $subject = "Hi!";
    $body = "Hi,\n\nHow are you?";
    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $username = "<niko@gmail.com>";
    $password = "somepassworrd";
    $headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
    $smtp = Mail::factory('smtp', array ('host' => $host,'port' => $port,'auth' =>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>");
?> 

当我尝试执行这些php脚本时,我收到这些错误

When I try to execute these php script I get these error

  FATAL ERROR Class Mail NOT found ON number line 18

我知道上述问题可能与这些

I know the above question is possible duplicate of these

从PHP页面使用GMail SMTP服务器发送电子邮件

但它不工作。我的PHP版本是5.3.4 Xampp 1.7.4版本。

But its not working.My PHP version is 5.3.4 Xampp 1.7.4 version.

 mail('caffeinated@example.com', 'My Subject', $message); \\ I tried these

但它显示一个警告,表示丢失标题。

But it shows a warning saying that missing headers.


  1. 如何发送电子邮件使用php脚本?

  1. How do I send an email Using the php script?

我们可以在PHP中发送电子邮件而不使用身份验证吗?因为vb.net使用服务器身份验证,而是在Google上找到的大部分代码都没有php验证。所以我有疑问

Can we send an email without using authentication in PHP ? Because vb.net uses server authentication but most of the code i found on google is without authentication for php. so I got a doubt

最后请帮助我在2个小时左右的时间里尝试!

Finally please help me with these trying from an 2 hours or so!

推荐答案

PHP mail()函数用于通过Sendmail发送邮件。如果你想使用一些SMTP服务器,你可以使用Zend_Mail这使得这个东西很容易:
http://framework.zend.com/manual/en/zend.mail.html

The PHP mail() function is for sending mail via Sendmail. If you want to use some SMTP server, you can use Zend_Mail which makes this thing very easy: http://framework.zend.com/manual/en/zend.mail.html

使用Zend_Mail你只需要写这样做:

Using Zend_Mail you only need to write something like this:

$config = array('auth' => 'login',
            'username' => 'myusername',
            'password' => 'password');

$transport = new Zend_Mail_Transport_Smtp('mail.server.com', $config);

$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('sender@test.com', 'Some Sender');
$mail->addTo('recipient@test.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);

以上处理您的身份验证并发送邮件。

The above handles authentication for you and sends a mail.

这篇关于如何使用php脚本发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆