I'm using Swiftmailer to send emails in Symfony project.
$msg = (new \Swift_Message("Subject"))
->setFrom('xxx123#gmail.com')
->setTo('yyy#gmail.com')
->setBody($mailContent);
In the received email, the name of Sender looks like xxx123. But the name for this email id in its configuration is XXX YYY.
I can use,
->setFrom('xxx123#gmail.com' => 'XXX YYY')
but, I don't want to set name directly in the code as it may be changed to something else like YYY ZZZ.
How can I achieve this? Thanks in advance!!
Sender name is always set in the header of every message. So header can be set by any email client. Your SwiftMailer is also email client in this case.
Your email server can rewrite header but only in specific cases. Like Gmail rewrite all email addresses that you didn't validate in the account settings to your base email.
If you need to fetch your sender name from Gmail account you can use Gmail-API. Authenticate your application with OAuth and send GET request to url:
https://www.googleapis.com/gmail/v1/users/me/settings/sendAs/YOUR_EMAIL_ADDRESS
You will receive JSON like this:
{
"sendAsEmail": "YOUR_EMAIL_ADDRESS",
"displayName": "SENDER NAME THAT YOU NEED",
"replyToAddress": "",
"signature": "Your standard signature for emails created by Gmail in browser",
"isPrimary": true,
"isDefault": true
}
More about Google API for this request you can find here: https://developers.google.com/gmail/api/v1/reference/users/settings/sendAs/get
You can use this code:
$fromEmail = "xxx123#gmail.com";
$fromName = "Sender Name";
$msg = (new \Swift_Message("Subject"))
->setFrom([$fromEmail => $fromName])
->setTo('yyy#gmail.com')
->setBody($mailContent);
If you put only email, code will cut name from email until #. In your case it's: xxx123#gmail.com
Related
I am sending email using php mailer and email is going fine to the inbox
but next time when i reply back instead of email coming to under same conversation it create a new email even it should show all the email subject
any one has an idea ?
The email you send via php must have a header
Reply-To: <from-email-address>
From: <from-email-address>
as well as a
To: <destination-email-address>
header line for it to appear as a conversation thread.
Also the subject line should be the same or be the same with a prepended 'Re: '
I got the Gmail Rest API sending part working but the e-mail doesn't include the signature and in recipient's inbox the 'from' label is sender's user id not the name of the user.
This is in php.
$mime = new Mail_mime();
$mime->setFrom("ABCD"); //This doesn't work....
$mime->setSubject('Testing');
$mime->setTXTBody('This is a demo mail');
$mime->addTo('a#a.com');
$message_body = $mime->getMessage();
$encodeMessage = base64url_encode($message_body);
$message = new Google_Service_Gmail_Message();
$message->setRaw($encodeMessage);
$send = $service->users_messages->send('me', $message);
Is there anyway to include the signature and change the 'from'?
The signature is not added by the API because it is a setting on the web client, not a global setting for the entire account. If you configure your Gmail account on Thunderbird, Outlook or another email client, Gmail will not add the signature either. You should think about Gmail in two separate ways:
The web client interface, accessible at https://mail.google.com, which is just an email client like any other;
Your inbox, the place where messages end up in, which is completely independent of the clients you use to access it.
In other words, this is an email client-dependent setting, and the only thing the clients do is add a bit of text to the text you write yourself, nothing else.
I know this is old but you can retrieve via API the users signature.
https://developers.google.com/admin-sdk/email-settings/#manage_signature_settings
you can then append to your email that you are composing.
I have the following code which sets up the SMTP server:
ini_set("send_from", "test#gmail.com");
ini_set("SMTP", "smtp.gmail.com");
and i create a simple mail in this way:
mail("test#yahoo.com", "A subject", "My message for you", "From: TEST");
When I run this code, it fails to send mail to Yahoo e.g. some.email#yahoo.com. But when i use any Gmail mail address as the first argument, it works.
What's wrong ?
To send mail as an authenticated user you should use email authentication methods like SPF, DKIM etc.
Also you need to make sure your domain should point to your IP address and IP address MUST point to same domain. This is called Reverse DNS
Other good practice that prevents mails from going into spam folder are
Make sure you have a unsubscribe link
Make sure the Reply-To header is added and the email used here is a valid email.
Add a Name in the To field. Like First Last <email#example.com>
Add a postal address of the company you are mailing from which must include a phone number.
There was a form to white list email senders IP for yahoo. Now I dont find it. So try the above things, It should work well.
In thi case you dont auth (user name passwort) and dont usw tls. This wont be accepted.
Better use this:
XAMPP Sendmail using Gmail account
or an framework to send emails via smtp like
Zend Mail Gmail SMTP
http://framework.zend.com/manual/1.12/en/zend.mail.sending.html
Here a code example
http://framework.zend.com/downloads/latest#ZF1
require('Zend/Mail.php');
$config = array(
'ssl' => 'tls',
'port' => 587,
'auth' => 'login',
'username' => 'your_gmail_address#gmail.com',
'password' => 'password'
);
$smtpConnection = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
Zend_Mail::setDefaultTransport($smtpConnection);
Zend_Mail::setDefaultFrom('your_gmail_address#gmail.com', 'Your real name');
$mail = new Zend_Mail();
$mail->addTo('any_address#yahoo.com', 'Test');
$mail->setSubject(
'Demonstration - Sending Mails per SMTP Connection'
);
$mail->setBodyText('...Your message here...');
$mail->send($smtpConnection);
I am using Swift Mailer to check for bounced messages. I have created one separate account for bounce messages, however when I set the return path, it does not allow the bounce message send to that account. Is it normal or is it a code error?
$verp = 'bounces-' . str_replace('#', '=', $row['ReplyTo']) . '#gmail.com';
$message = Swift_Message::newInstance()
->setSubject($row['Subject'])
->setFrom(array($row['ReplyTo'] => $row['FromName']))
->setReturnPath($verp)
->setBody($html, 'text/html')
->addPart($txt, 'text/plain');
I am now using VERP, it seems that it is to locate a delivery error? But not for sending the message to a bounce mail account?
Yes, that is normal. When sending email through Gmail's SMTP servers, it will force the return-path to be the account you are sending from.
Your only solution is to search for a provider which allows you to set the return-path.
It's not a gmail issue, it's a requirement of the SMTP specification, as defined in RFC 5321 section 4.4:
A message-originating SMTP system SHOULD NOT send a message that already contains a Return-path header field.
It also says that while SMTP systems should not inspect message content at all (i.e. they don't look at headers), a gateway from some other context to SMTP SHOULD remove any return-path header. In short, if you're adding a return-path header yourself, you're doing it wrong.
The return-path header you see in a received message is created by the receiver, not the sender, and is derived from the SMTP MAIL FROM command used to deliver the message. This address need not to have anything in common with the From address header within the message, and designates where the message should be sent to in the event of a delivery failure, i.e. exactly what you want the VERP address for.
I don't know about SwiftMailer, but in PHPMailer you can set the SMTP envelope sender value by setting the Sender property, and a receiver will convert that into a return-path message header on reception.
I have noticed that some mails come with the from address like :
Adam Mannet via www.findyourfriend.com.
What headers do I need to pass to the native PHP mail() function to accomplish this when sending e-mail?
I think the via tag is added by the mail server when somebody uses an external (from his domain) smtp server.
Per example,
my email is iceduck#iceduck.net but I send email via Gmail's smtp, you will see From iceduck#iceduck.net via Gmail.com.
If you want to make the via appear then you'll have to send your mail through the smtp server you want to appear in the via.
You can check out this link : http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm
I don't think the solution is in the header.
Best
You should check PHP mail documentation. There is a description how to modify 'From' header. You should use additional_headers to change that header information and include 'friendly name' beside your e-mail address.
<?php
// The message
$message = "Message content";
$headers = 'From: User Name <user#example.com>';
// Send
mail('test#example.com', 'My Subject', $message, $headers);
?>
This should make your from address look like: 'User Name via www.example.com'