I am trying to make an email look like it came from a specific user within our company for an automated client followup email. For some reason I cannot change the "FROM" to look like anyone but the account I log into gmail with.
I know for a fact that PHP mailer library can make the FROM address from anyone without any problems - but for some reason I can't in python. We have an enterprise gmail account if that helps.
Here is the code I am working with
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")
Here is the PHP that will allow you to change from address to whatever you want
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();
Python offers a ` library to send emails- “SMTP lib”. “smtplib” creates a Simple Mail Transfer Protocol client session object which is used to send emails to any valid email id on the internet. The Port number used here is '587'.
Add to the msg
msg['Reply-To'] = "THIS IS THE EMAIL I WANT TO [email protected]"
Edit
One thing you can do is add the return email in your Accounts and Import in gmail. and select it not as an alias. this will allow you to send emails from your main account as the other account. using the from address of the other account
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With