Tech Tip: Gmail SMTP in 4D: Understanding the "From" Address Behavior
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: April 7, 2025
When using Gmail’s SMTP service in 4D, you might notice that changing $email.from doesn’t affect the actual sender address in the recipient’s inbox. No matter what value you assign to $email.from , Gmail enforces the authenticated account (set in $server.user) as the sender.
Why does this happen?
Gmail's SMTP security measures prevent unauthorized email spoofing by ensuring that all outgoing emails originate from the authenticated user’s address. This means the sender's email will always be the one used for authentication ($server.user).Example :
var $transporter : 4D.SMTPTransporter // Connection to the mail server $server:=New object $server.host:="smtp.gmail.com" // Google's SMTP service $server.port:=587 $server.headerCharset:="US-ASCII_UTF8_QP" $server.bodyCharset:="US-ASCII_UTF8_QP" $server.logFile:="LogTest.txt" // Log file saved in the Data/Logs folder // Define authentication credentials $server.user:="your-email@gmail.com" // Sender's email address $server.password:="your-app-password" // SMTP application password // Prepare and send the email $transporter:=SMTP New transporter($server) // Verify the connection to the server $status:=$transporter.checkConnection() // Create and configure the email object $email:=New object $email.subject:="Test email using Gmail SMTP" $email.from:="alias-email@gmail.com" // Attempting to use an alias $email.to:="recipient-email@gmail.com" $email.htmlBody:="SMTP server test" // use html syntax // Send the email $status:=$transporter.send($email) |
Note : Even though $email.from is set to "alias-email@gmail.com", the email will still appear as being sent from "your-email@gmail.com" in most cases.
How to use a different sender address?
If you want to send emails using a different display address, the correct approach is to configure a verified alias in your Gmail account. Here’s how:
- Go to Gmail Settings → Click on the gear icon → "See all settings."
- Navigate to "Accounts and Import."
- Under "Send mail as," click "Add another email address."
- Enter the alias email and verify it.
Now you can fill the $email.from with the alias added.
Important : While the display name in the received email can now reflect your alias, the underlying authenticated account remains the sender. This setup ensures that your emails display the desired "From" name while still using Gmail's SMTP service.