如何通过localhost发送邮件?

时间:2021-01-29 18:12:31
<?php
$to = "rajesh.bakade65@gmail.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <rajesh.bakade@chpl.tv>' . "\r\n";
//$headers .= 'Cc: myboss@example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>

and my php.ini file is

我的php.ini文件是

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster@localhost

plz suggest any solution so that i will be able to send mail from localhost

PLZ建议任何解决方案,以便我能够从localhost发送邮件

Thanks

3 个解决方案

#1


5  

Use the feature-rich SwiftMailer and your settings should be like this:

使用功能丰富的SwiftMailer,您的设置应如下所示:

username:google email addres
password: your google password
smtp: smtp.gmail.com
port: 587

Here is how you can set your credentials and send the email.

您可以通过以下方式设置凭据并发送电子邮件。

require_once 'lib/swift_required.php';

//Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('your username')
  ->setPassword('your password');

//Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  ->setBody('Here is the message itself');

//Send the message
$result = $mailer->send($message);

#2


0  

You seem to be using the wrong port number forsmtp_port: when looking at the GMail settings, it looks like you'd need port 587 or 465. (Depending whether you want to use TLS or SSL encryption)

您似乎使用了错误的端口号forsmtp_port:在查看GMail设置时,看起来您需要端口587或465.(取决于您是否要使用TLS或SSL加密)

Furthermore I do not know whether PHP support SSL and/or TLS. If not you need to use an additional PHP library in order to be able to connection to the Google mail server.

此外,我不知道PHP是否支持SSL和/或TLS。如果不是,您需要使用额外的PHP库才能连接到Google邮件服务器。

#3


-2  

  1. require_once 'lib/swift_required.php';

    //Create the Transport $transport =
    Swift_SmtpTransport::newInstance('smtp.example.org', 25)  
    ->setUsername('your username')   ->setPassword('your password');
    
    //Sendmail $transport =
    Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
    
    //Create the Mailer using your created Transport $mailer =
    Swift_Mailer::newInstance($transport);
    
    //Create a message $message = Swift_Message::newInstance('Wonderful
    Subject')   ->setFrom(array('john@doe.com' => 'John Doe'))  
    ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))   ->setBody('Here is the message itself');
    
    //Send the message $result = $mailer->send($message);
    

    [1]: http://i.stack.imgur.com/t3Pbc.jpg

  2. require_once'lib / swift_required.php'; //创建Transport $ transport = Swift_SmtpTransport :: newInstance('smtp.example.org',25) - > setUsername('您的用户名') - > setPassword('您的密码'); // Sendmail $ transport = Swift_SendmailTransport :: newInstance('/ usr / sbin / sendmail -bs'); //使用您创建的Transport $ mailer =创建邮件程序 Swift_Mailer ::的newInstance($运输); //创建消息$ message = Swift_Message :: newInstance('很棒 主题') - > setFrom(array('john@doe.com'=>'John Doe')) - > setTo(array('veriver @ domain.org','other@domain.org'=>'A name')) - > setBody('这是消息本身'); //发送消息$ result = $ mailer-> send($ message);  [1]:http://i.stack.imgur.com/t3Pbc.jpg

#1


5  

Use the feature-rich SwiftMailer and your settings should be like this:

使用功能丰富的SwiftMailer,您的设置应如下所示:

username:google email addres
password: your google password
smtp: smtp.gmail.com
port: 587

Here is how you can set your credentials and send the email.

您可以通过以下方式设置凭据并发送电子邮件。

require_once 'lib/swift_required.php';

//Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('your username')
  ->setPassword('your password');

//Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  ->setBody('Here is the message itself');

//Send the message
$result = $mailer->send($message);

#2


0  

You seem to be using the wrong port number forsmtp_port: when looking at the GMail settings, it looks like you'd need port 587 or 465. (Depending whether you want to use TLS or SSL encryption)

您似乎使用了错误的端口号forsmtp_port:在查看GMail设置时,看起来您需要端口587或465.(取决于您是否要使用TLS或SSL加密)

Furthermore I do not know whether PHP support SSL and/or TLS. If not you need to use an additional PHP library in order to be able to connection to the Google mail server.

此外,我不知道PHP是否支持SSL和/或TLS。如果不是,您需要使用额外的PHP库才能连接到Google邮件服务器。

#3


-2  

  1. require_once 'lib/swift_required.php';

    //Create the Transport $transport =
    Swift_SmtpTransport::newInstance('smtp.example.org', 25)  
    ->setUsername('your username')   ->setPassword('your password');
    
    //Sendmail $transport =
    Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
    
    //Create the Mailer using your created Transport $mailer =
    Swift_Mailer::newInstance($transport);
    
    //Create a message $message = Swift_Message::newInstance('Wonderful
    Subject')   ->setFrom(array('john@doe.com' => 'John Doe'))  
    ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))   ->setBody('Here is the message itself');
    
    //Send the message $result = $mailer->send($message);
    

    [1]: http://i.stack.imgur.com/t3Pbc.jpg

  2. require_once'lib / swift_required.php'; //创建Transport $ transport = Swift_SmtpTransport :: newInstance('smtp.example.org',25) - > setUsername('您的用户名') - > setPassword('您的密码'); // Sendmail $ transport = Swift_SendmailTransport :: newInstance('/ usr / sbin / sendmail -bs'); //使用您创建的Transport $ mailer =创建邮件程序 Swift_Mailer ::的newInstance($运输); //创建消息$ message = Swift_Message :: newInstance('很棒 主题') - > setFrom(array('john@doe.com'=>'John Doe')) - > setTo(array('veriver @ domain.org','other@domain.org'=>'A name')) - > setBody('这是消息本身'); //发送消息$ result = $ mailer-> send($ message);  [1]:http://i.stack.imgur.com/t3Pbc.jpg