2012-06-25 105 views
4

我想讓電子郵件看起來像它來自我們公司內的一個特定用戶,用於自動化客戶端後續電子郵件。出於某種原因,我無法將「FROM」更改爲任何人,只是我登錄到Gmail的帳戶。Python從不同的地址發送電子郵件到gmail

我知道一個事實,即PHP郵件程序庫可以使任何人的FROM地址沒有任何問題 - 但由於某種原因,我不能在Python中。如果有幫助,我們有一個企業gmail帳戶。

這裏是我與

import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.MIMEImage import MIMEImage 

def sendFollowupEmail(html): 
    msg = MimeText('body') 
    msg['Subject'] = 'subject' 
    msg['From'] = "THIS IS THE EMAIL I WANT TO [email protected]" 
    msg['To'] = "[email protected]" 




    username = '[email protected]' 
    password = 'password' 
    server = smtplib.SMTP('smtp.gmail.com:587') 
    server.starttls() 
    server.login(username,password) 
    server.sendmail(me, you, msg.as_string()) 
    server.quit()  








    if __name__ == '__main__': 
    sendFollowupEmail("test123") 

在這裏工作的代碼是PHP,這將允許你從地址更改爲任何你想要的

function sendFollowUpEmail($options) { 

     /* 
     * Send an email to a person or group. 
     * Dependencies: PHPMailer 
     * options: { 
     *  'to' -> who to send the email to, 
     *  'from'-> who the email was sent from, 
     *  'subject'-> subject of the email, 
     *  'body' -> the body of the email 
     * } 
     */ 
     $host = 'smtp.gmail.com'; 
     $username = "[email protected]"; 
     $password = "password"; 
     $port = 465; 
     echo error_reporting(E_STRICT); 

     require_once('PHPMailer/class.phpmailer.php'); 
     $mail = new PHPMailer(); 
     $body = $options['body']; 

     $mail->IsSMTP(); 
     $mail->IsHTML(true); 

     $mail->SMTPAuth = true; 
     $mail->SMTPSecure = "ssl"; 
     $mail->Host = $host; 
     $mail->Port = $port; 
     $mail->Username = $username; 
     $mail->Password = $password; 
     $mail->SetFrom($options['from'], $options['from']); 

     if($options['bcc']!='') { 
      $mail->AddBCC($options['bcc'], $options['bcc']); 
     } 

     //$mail->AddReplyTo("[email protected]","First Last"); 
     $mail->Subject = $options['subject']; 
     $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; 
     $mail->MsgHTML($body); 
     $address = $options['to']; 
     $mail->AddAddress($address); 
     $mail->send(); 
+0

你試過'server.sendmail(味精['從'],你,msg.as_string())'? –

+0

是的,仍然沒有工作。 – lodkkx

+0

從我使用git-format-patch的經驗中,我相信用gmail做smtp是不可能的。我試圖設置我的加號地址,它不允許我。我不確定 – Daenyth

回答

5

加入味精

msg['Reply-To'] = "THIS IS THE EMAIL I WANT TO [email protected]" 

編輯

您可以做的一件事是在您的帳戶中添加回覆電子郵件,並在Gmail中添加回覆電子郵件地址和導入。並選擇它不作爲別名。這將允許您發送主賬戶中的電子郵件作爲其他賬戶。使用從另一個帳戶的地址

+0

允許將回復發送到電子郵件地址,但它仍然作爲我用作訪問的帳戶離開。 – lodkkx

+1

爲什麼要投票?這遵循python遵循的RFC 822協議。 – corn3lius

相關問題