我正在尝试让一封电子邮件看起来像是来自我们公司内的某个特定用户,用于自动化客户跟进电子邮件。但出于某些原因,我无法更改“FROM”地址,使其看起来不是我登录Gmail帐户的那个地址。
我知道PHP Mailer库可以轻松地将“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 CHANGE@domain.com"
msg['To'] = "client@client.com"
username = 'accessaccount@gmail.com'
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 = "accessaccount@gmail.com";
$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("name@yourdomain.com","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();
server.sendmail(msg['From'], you, msg.as_string())
吗? - Martijn Pieters