+ 2

Send emails with python

I need code to send an email in python from my gmail account, and I know how to do this, but I do not know how to add a subject in the subject box. Do any of you know ? - Thanks

5th Sep 2018, 9:38 PM
Carson
Carson - avatar
6 odpowiedzi
+ 1
well is the 'subject' in the body, or in its own box ?
6th Sep 2018, 2:42 AM
Carson
Carson - avatar
+ 1
Al Swiegert wrote a great tutorial on it. you should check it out https://automatetheboringstuff.com/chapter16/ Try this: import smtplib smtpObj = smtplib.SMTP('smtp.gmail.com', 587) smtpObj.ehlo() smtpObj.starttls() smtpObj.login('YOURADDRESS@GMAIL.COM', 'SUPERSECRETPASSWORD') smtpObj.sendmail('YOURADDRESS@GMAIL.COM', 'THEIRADDRESS@GMAIL.COM', 'Subject: So long.\nDear Alice, so long and thanks for all the fish. Sincerely, Bob') Yes, the "Subject" is part of the "Body", if it is not specified like above, the subject line will be left blank and only the body will contain text. The newline character separates the subject from the main body. NOTE: If you are still having trouble, try changing your port to 465 instead of 587. Best of luck!
6th Sep 2018, 2:57 AM
Steven M
Steven M - avatar
0
smtpObj.sendmail('you@gmail.com ', 'them@gmail.com', 'Subject: How to add a Subject.\n Carson, the \\n separates the "Subject" line from the "Body".') something like this?
6th Sep 2018, 2:06 AM
Steven M
Steven M - avatar
0
Maybe something like this? ================== # Send an HTML email with an embedded image and a plain text message for # email clients that don't want to display the HTML. from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage # Define these once; use them twice! strFrom = 'FROM@gmail.com' strTo = 'TO@yahoo.com.mx' # Create the root message and fill in the from, to, and subject headers msgRoot = MIMEMultipart('related') msgRoot['Subject'] = 'test message' msgRoot['From'] = strFrom msgRoot['To'] = strTo msgRoot.preamble = 'This is a multi-part message in MIME format.' # Encapsulate the plain and HTML versions of the message body in an # 'alternative' part, so message agents can decide which they want to display. msgAlternative = MIMEMultipart('alternative') msgRoot.attach(msgAlternative) msgText = MIMEText('This is the alternative plain text message.') msgAlternative.attach(msgText) # We reference the image in the IMG SRC attribute by the ID we give it below msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!', 'html') msgAlternative.attach(msgText) # This example assumes the image is in the current directory fp = open('Green Apple.gif', 'rb') msgImage = MIMEImage(fp.read()) fp.close() # Define the image's ID as referenced above msgImage.add_header('Content-ID', '<image1>') msgRoot.attach(msgImage) # Send the email (this example assumes SMTP authentication is required) import smtplib smtp = smtplib.SMTP('smtp.gmail.com: 587') smtp.starttls() smtp.login('USERAUTH@gmail.com', 'PASSWORD') smtp.sendmail(strFrom, strTo, msgRoot.as_string()) smtp.quit()
6th Sep 2018, 3:16 AM
Miguel Angel Acosta
Miguel Angel Acosta - avatar
0
im afraid that will not work
6th Sep 2018, 3:22 AM
Carson
Carson - avatar
0
Just, IS there any way to have the subject outside the body ?
6th Sep 2018, 1:58 PM
Carson
Carson - avatar