2013-02-05 102 views
4

我想從本地主機發送郵件到gmail服務器 forexample([email protected])。從本地主機發送到服務器的郵件

代碼示例:

<?php 
$to = "[email protected]"; 
$subject = "Hi!"; 
$body = "Hi,\n\nHow are you?"; 

if (mail($to, $subject, $body)) { 
    echo("<p>Message successfully sent!</p>"); 
} else { 
    echo("<p>Message delivery failed...</p>"); 
} 
?> 

我也改變了SMTP設置在php.ini 作爲

SMTP = mail.gmail.com 
smtp_port = 25 

但是,它仍然沒有工作,功能的郵件()被不工作。 請幫我

+0

讓我清楚的問題又來「我想從本地主機發送電子郵件到電子郵件地址,,,我不希望從本地主機到本地主機發送」 – Rahil

回答

1

嘗試使用SMTP服務器與Gmail。

ini_set("SMTP","ssl://smtp.gmail.com"); 
ini_set("smtp_port","465"); 

良好的閱讀

Send email from localhost with gmail

+0

還是同樣的錯誤:( – Rahil

+2

Gmail的SMTP服務將要求你進行身份驗證,在你的linux服務器上安裝'sendmail'或者類似的東西以便爲你發送電子郵件會更好。 –

+0

yup它可以通過phpmailer或者pear郵件包或類似的東西來完成並且設置gmail用戶名和密碼 –

0

100%的工作,我在我的網站上使用這個太

<?php 
$con=mysql_connect("mysql12","",""); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 
$db=mysql_select_db("data",$con); 
if(!$db) 
{ 
die('Could not select database'.mysql_error()); 
} 

$to=$_POST['to']; 
$subject=$_POST['subject']; 
$body=$_POST['tarea']; 

$query="select fname from table where email='$to'"; 
$fetch=mysql_query($query); 

while ($rows=mysql_fetch_array($fetch)) 
{ 
$name=$rows['fname']; 


$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
$headers .= "To:" .$name. "\r\n"; 
$headers .= 'From: <[email protected]>' . "\r\n"; 
mail($to, $subject, $body, $headers); 
} 
+0

不要忘記使用: $ headers ='MIME-Version:1.0'。「\ r \ n」; $ headers。='Content-type:text/html; charset = iso-8859- 1「。」\ r \ n「; – 2013-02-05 14:04:29

0

科林·莫雷利是正確的,你將需要進行身份驗證(登錄)使用Gmail的SMTP服務器。以下是工作代碼。參考。 Send email using the GMail SMTP server from a PHP page

<?php 

     require_once "Mail.php"; 

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

     $host = "ssl://smtp.gmail.com"; 
     $port = "465"; 
     $username = "[email protected]"; //<> give errors 
     $password = "password"; 

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

    ?> <!-- end of php tag--> 

P.S.難道你不能這樣做:

<?php 
// The message 
$message = "Line 1\r\nLine 2\r\nLine 3"; 

// In case any of our lines are larger than 70 characters, we should use wordwrap() 
$message = wordwrap($message, 70, "\r\n"); 

// Send 
mail('[email protected]', 'My Subject', $message); 
?> 

Ref。 http://php.net/manual/en/function.mail.php

編輯:

如果你想使用一個簡單的類,你可以在FTP扔未做服務器MODS的,只是使用的PHPMailer:

require_once('../class.phpmailer.php'); 
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded 

$mail    = new PHPMailer(); 

$body    = file_get_contents('contents.html'); 
$body    = eregi_replace("[\]",'',$body); 

$mail->IsSMTP(); // telling the class to use SMTP 
$mail->Host  = "mail.yourdomain.com"; // SMTP server 
$mail->SMTPDebug = 2;      // enables SMTP debug information (for testing) 
              // 1 = errors and messages 
              // 2 = messages only 
$mail->SMTPAuth = true;     // enable SMTP authentication 
$mail->SMTPSecure = "tls";     // sets the prefix to the servier 
$mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
$mail->Port  = 587;     // set the SMTP port for the GMAIL server 
$mail->Username = "[email protected]"; // GMAIL username 
$mail->Password = "yourpassword";   // GMAIL password 

$mail->SetFrom('[email protected]', 'First Last'); 

$mail->AddReplyTo("[email protected]","First Last"); 

$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic"; 

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 

$mail->MsgHTML($body); 

$address = "[email protected]"; 
$mail->AddAddress($address, "John Doe"); 

$mail->AddAttachment("images/phpmailer.gif");  // attachment 
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment 

if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 

參考。 http://phpmailer.worxware.com/index.php?pg=examplebgmail

0

php.ini中的SMTP設置僅適用於Windows主機。

如果您沒有運行Windows系統,您應該使用適當的類來實現SMPT功能或相應地配置您的本地sendmail/MTA。

0

好吧,我看不那麼久以前,我碰到這個附帶的問題

你需要這需要一個靜態IP和域名命名的服務器即本地主機不會工作

這可以通過設置郵件服務器POP3服務器或SMTP達捲曲身體捲曲功能相應地調整

function CurlMail($The_mail, $The_FamKey, $The_Service ,$The_Client) 
{ 

     //create array of data to be posted 

     $post_data['email'] = $The_mail; 
     $post_data['FamilyKey'] = $The_FamKey; 
     $post_data['Service'] = $The_Service; 
     $post_data['Client'] = $The_Client; 

     //traverse array and prepare data for posting (key1=value1) 
     foreach ($post_data as $key => $value) 
       { 
        $post_items[] = $key . '=' . $value; 
       } 
     //create the final string to be posted using implode() 
     $post_string = implode ('&', $post_items); 
     //create cURL connection 
     $curl_connection = curl_init('http://foo.com/mail.php'); 
     //set options 
     curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); 
     curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); 
     curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 0); 
     //set data to be posted 
     curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string); 
     //perform our request 
     $result = curl_exec($curl_connection); 
     //show information regarding the request 
     print_r(curl_getinfo($curl_connection)); 
     echo curl_errno($curl_connection) . '-' .curl_error($curl_connection); 
     //close the connection 
     curl_close($curl_connection); 
    } 

然後在同一個SMTP Live服務器設置是指爲php.ini這些細節 MAIL。PHP

ini_set('SMTP', "127.0.0.1"); 
ini_set('smtp_port', "25"); 

$to = "[email protected]"; 
$subject = "Test mail"; 
$message = "Hello! This is a simple email message."; 
$from = "[email protected]"; 
$headers = "From:" . $from; 
mail($to,$subject,$message,$headers); 
echo "Mail Sent."; 
1

你可以在python中運行這段代碼,它允許你在localhost中設置一個服務器。沒有什麼需要在php.ini中進行更改。 (在php.ini中smtp應該是localhost,端口應該是25.默認設置)。希望這可以幫助。 :)

import smtpd 
import smtplib 
import asyncore 
class SMTPServer(smtpd.SMTPServer): 

    def __init__(*args, **kwargs): 
     print "Running smtp server on port 25" 
     smtpd.SMTPServer.__init__(*args, **kwargs) 

    def process_message(*args, **kwargs): 
     to = args[3][0] 
     msg = args[4] 
     gmail_user = 'yourgmailhere' 
     gmail_pwd = 'yourgmailpassword' 
     smtpserver = smtplib.SMTP("smtp.gmail.com",587) 
     smtpserver.ehlo() 
     smtpserver.starttls() 
     smtpserver.ehlo 
     smtpserver.login(gmail_user, gmail_pwd) 
     smtpserver.sendmail(gmail_user, to, msg) 
     print 'sent to '+to 
     pass 

if __name__ == "__main__": 
    smtp_server = SMTPServer(('localhost', 25), None) 
    try: 
     asyncore.loop() 
    except KeyboardInterrupt: 
     smtp_server.close() 
相關問題