Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP页面使用GMail SMTP服务器发送电子邮件_Php_Email_Smtp_Gmail - Fatal编程技术网

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

从PHP页面使用GMail SMTP服务器发送电子邮件,php,email,smtp,gmail,Php,Email,Smtp,Gmail,我试图从PHP页面通过GMail的SMTP服务器发送电子邮件,但我收到以下错误: 身份验证失败[SMTP:SMTP服务器不支持身份验证(代码:250,响应:mx.google.com,在您的服务中,[98.117.99.235]大小35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES管道传输)] 有人能帮忙吗?这是我的密码: <?php require_once "Mail.php"; $from = "Sandra Sender <send

我试图从PHP页面通过GMail的SMTP服务器发送电子邮件,但我收到以下错误:

身份验证失败[SMTP:SMTP服务器不支持身份验证(代码:250,响应:mx.google.com,在您的服务中,[98.117.99.235]大小35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES管道传输)]

有人能帮忙吗?这是我的密码:

<?php
require_once "Mail.php";

$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <ramona@microsoft.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "smtp.gmail.com";
$port = "587";
$username = "testtest@gmail.com";
$password = "testtest";

$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>");
 }
?>

设置


另外,请查看端口25是否正常工作。

您的代码似乎没有使用TLS(SSL),这是一个错误

您可以通过设置

$host=”ssl://smtp.gmail.com";


您的代码看起来与主机名方案中的ssl://类似。

Gmail需要端口465,而且是来自phpMail:)的代码。

问题中列出的代码需要两个更改

$host = "ssl://smtp.gmail.com";
$port = "465";
SSL连接需要端口465。

//Pear邮件库
// Pear Mail Library
require_once "Mail.php";

$from = '<fromaddress@gmail.com>';
$to = '<toaddress@yahoo.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'johndoe@gmail.com',
        'password' => 'passwordxxx'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}
需要一次“Mail.php”; $from=''; $to=''; $subject='Hi!'; $body=“您好,\n\n您是什么人?”; $headers=数组( 'From'=>$From, '至'=>$至, “主题”=>$Subject ); $smtp=Mail::工厂('smtp',数组( '主机'=>'ssl://smtp.gmail.com', “端口”=>“465”, “auth”=>正确, '用户名'=>'johndoe@gmail.com', '密码'=>'密码XXX' )); $mail=$smtp->send($to、$headers、$body); if(PEAR::isError($mail)){ echo(“”.$mail->getMessage()。

”); }否则{ 回显(“消息已成功发送!

”); }
可以使用外部服务器发送电子邮件

下面是一个演示如何使用Gmail服务器的示例:

require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";

//Connect to localhost on port 25
$swift =& new Swift(new Swift_Connection_SMTP("localhost"));


//Connect to an IP address on a non-standard port
$swift =& new Swift(new Swift_Connection_SMTP("217.147.94.117", 419));


//Connect to Gmail (PHP5)
$swift = new Swift(new Swift_Connection_SMTP(
    "smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS));
使用,通过Gmail凭证发送邮件非常容易:

<?php
require_once 'swift/lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
  ->setUsername('GMAIL_USERNAME')
  ->setPassword('GMAIL_PASSWORD');

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance('Test Subject')
  ->setFrom(array('abc@example.com' => 'ABC'))
  ->setTo(array('xyz@test.com'))
  ->setBody('This is a test mail.');

$result = $mailer->send($message);
?>

我不推荐Pear邮件。自2010年以来一直没有更新。同时读取源文件;源代码几乎已经过时,是用PHP4风格编写的,并且已经发布了许多错误(Google it)。我用的是Swift邮件

Swift-Mailer集成到用PHP5编写的任何web应用程序中,为发送具有多种功能的电子邮件提供了一种灵活而优雅的面向对象方法

使用SMTP、sendmail、postfix或自定义传输发送电子邮件 您自己的实现

支持需要用户名和密码和/或加密的服务器

在不剥离请求数据的情况下防止标头注入攻击 内容

发送MIME兼容的HTML/多部分电子邮件

使用事件驱动插件自定义库

使用低内存处理大型附件和内嵌/嵌入图像 使用

这是一个免费的开源软件,你可以下载并上传到你的服务器上。(功能列表从所有者网站复制)

Gmail SSL/SMTP和Swift Mailer的工作示例如下

// Swift Mailer Library
require_once '../path/to/lib/swift_required.php';

// Mail Transport
$transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465)
    ->setUsername('username@gmail.com') // Your Gmail Username
    ->setPassword('my_secure_gmail_password'); // Your Gmail Password

// Mailer
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject Here')
    ->setFrom(array('sender@example.com' => 'Sender Name')) // can be $_POST['email'] etc...
    ->setTo(array('receiver@example.com' => 'Receiver Name')) // your email / multiple supported.
    ->setBody('Here is the <strong>message</strong> itself. It can be text or <h1>HTML</h1>.', 'text/html');

// Send the message
if ($mailer->send($message)) {
    echo 'Mail sent successfully.';
} else {
    echo 'I am sure, your configuration are not correct. :(';
}
//Swift邮件程序库
require_once'../path/to/lib/swift_required.php';
//邮件运输
$transport=Swift\u SmtpTransport::newInstance('ssl://smtp.gmail.com', 465)
->setUsername('username@gmail.com')//您的Gmail用户名
->setPassword('my_secure_gmail_password');//你的Gmail密码
//梅勒
$mailer=Swift\u mailer::newInstance($transport);
//创建消息
$message=Swift\u message::newInstance('这里的主题很棒')
->setFrom(数组('sender@example.com“=>”发件人名称“)//可以是$\u POST['email']等。。。
->设置为(数组('receiver@example.com“=>”接收者名称“)//您的电子邮件/支持多个。
->setBody('text/HTML');
//发送消息
如果($mailer->send($message)){
回显“已成功发送邮件”;
}否则{
echo“我确定,您的配置不正确。”;
}

我希望这能有所帮助。快乐编码…:)

通过Gmail使用phpMail库发送邮件 请不要从中加载库文件


我也有这个问题。我设置了正确的设置并启用了不太安全的应用程序,但仍然无法正常工作。最后,我启用了它,它对我起了作用。我希望这对某人有所帮助。

要在Ubuntu中安装PEAR的Mail.php,请运行以下命令集:

    sudo apt-get install php-pear
    sudo pear install mail
    sudo pear install Net_SMTP
    sudo pear install Auth_SASL
    sudo pear install mail_mime

我为没有“@gmail.com”sufix的GSuite帐户提供了一个解决方案。此外,我认为它将适用于@gmail.com的GSuite帐户,但尚未尝试过。
首先,您应该有权更改GSuite帐户的选项“allos?w Loss secure app”。如果您有权限(您可以在帐户设置->安全中进行检查),则必须停用“两步因素身份验证”,转到页面末尾,并将“允许不太安全的应用程序”设置为“是”。这就是全部。如果您没有更改这些选项的权限,则此线程的解决方案将不起作用。检查以更改“允许更少…”选项。

我尝试了@shasi kanth提供的建议,但没有成功。我阅读了文档,几乎没有做什么更改。因此,我使用以下代码通过Gmail发送邮件,其中vendor/autoload.php由composer获得,composer要求“swiftmailer/swiftmailer:^6.0”:



不会的-谷歌要求在465或587上交付。请参阅。什么是
Mail.php
??我从哪里得到这个文件?谁能给我一个链接,在那里我可以得到Mail.php文件。因为我试过了,但没有效果。上面这个例子中的@符号是什么?我一个也没看到!我相信myaccount.gmail.com和myaccount@gmail.com在电子邮件标准中。如果指定了服务器,则不需要包含@gmail。只需键入用户名
myaccount
。只需更改GMAIL_用户名、GMAIL_密码以及发件人和收件人地址,“第一次”就可以了。没有其他解决办法对我有效。谢谢。我同意,swift-mailer是一个即时的邮件解决方案,比摆弄pear容易得多。别忘了启用PHP的PHP_openssl扩展。使用SwiftMailer的不错的解决方案+1arrgh。我不能让斯威夫特梅勒去上班。我不知道如何与你交流
// Swift Mailer Library
require_once '../path/to/lib/swift_required.php';

// Mail Transport
$transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465)
    ->setUsername('username@gmail.com') // Your Gmail Username
    ->setPassword('my_secure_gmail_password'); // Your Gmail Password

// Mailer
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject Here')
    ->setFrom(array('sender@example.com' => 'Sender Name')) // can be $_POST['email'] etc...
    ->setTo(array('receiver@example.com' => 'Receiver Name')) // your email / multiple supported.
    ->setBody('Here is the <strong>message</strong> itself. It can be text or <h1>HTML</h1>.', 'text/html');

// Send the message
if ($mailer->send($message)) {
    echo 'Mail sent successfully.';
} else {
    echo 'I am sure, your configuration are not correct. :(';
}
<?php
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 */
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
    sudo apt-get install php-pear
    sudo pear install mail
    sudo pear install Net_SMTP
    sudo pear install Auth_SASL
    sudo pear install mail_mime
<?php
     require_once 'vendor/autoload.php';
     $transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))->setUsername ('SendingMail')->setPassword ('Password');

     $mailer = new Swift_Mailer($transport);

     $message = (new Swift_Message('test'))
      ->setFrom(['Sending mail'])
      ->setTo(['Recipient mail'])
      ->setBody('Message')
  ;

     $result = $mailer->send($message);
    ?>