phpmailer change mail sender - php

When I send mail from phpmailer and I wanted to response I get response e-mail address like admin#domain.com. But I want change it to office#domain.com.
So I added:
$mail->AddReplyTo('office#domain.com', 'First Last');
But in e-mails to response I get both (office and admin) and I want only office#domain.com
I changed it to:
$mail->Sender='admin#domain.pl';
$mail->SetFrom('office#domain.pl','First Last');
I get
SMTP Error: Data not accepted.
SMTP server error: 5.7.1 Forged sender address:
My phpmailer version is: 5.2.6

The reply to addresses needed to be added before the from address:
$mail->addReplyTo('replyto#email.com', 'Reply to name');
$mail->setFrom('mailbox#email.com', 'Mailbox name');
With this order all is OK.
addReplyTo not AddReplyTo
Alternative:
You can clear replyTo array before:
$mail->ClearReplyTos();
$mail->addReplyTo(example#example.com, 'EXAMPLE');

Setting Sender is the correct approach to do this, so you're doing that right. The error you are seeing is probably down to SPF checks at the receiver - if the sender domain has SPF set up and it does not allow sending from your IP, it will reject it with the error you are seeing.

Related

Zend_Mail and Mailtrap : bad sender address

I'm sending email to mailtrap in this way
$mail = new Zend_Mail();
$mail->setFrom("senderaddress#yahoo.it", 'Temporary sender name');
I am already using mail trap for a lot of projects, so I know that I can send email using these email address and name as "from"
What doesn't works
The problem is that $mail->send() throws an Exception
5.1.7 Bad sender address syntax
Little debug
So I debugged Zend code. I am now sure it's sending from as
Temporary sender name <senderaddress#yahoo.it>
I also tried avoiding litteral name, so using only
$mail->setFrom("senderaddress#yahoo.it");
The header is written using only
<senderaddress#yahoo.it>
But nothing changed
What I'm not understanding
I am not able to understand if this very old Zend project is NOT sending at all the message or if Mailtrap is refusing.
Questions
What is wrong with this sender address ?
Is this an error from Zend_Mail or from Mailtrap?
And obviously, how to fix ?
You can try this way:
Zend_Mail::setDefaultFrom('senderaddress#yahoo.it', 'Temporary sender name');
$mail = new Zend_Mail();
$mail->setBodyText('...Your message here...');
$mail->send($transport);

Getting warning message in Gmail Account when I send email by using phpmailer()

I am using phpmailer() to send email from my website. But when It's sent email I see following warning message.
I can't understand why it's showing and how can I fix this error message. Can anyone tell me about it ?
Following is my code :
<?php
require_once("mail/PHPMailerAutoload.php");
$mail = new PHPMailer;
$mail->setFrom($email);
$mail->addReplyTo('toemail#gmail.com', 'First Last');
$mail->addAddress('toemail#gmail.com', 'First Last');
$mail->Subject = 'PHPMailer mail() test';
$mail->msgHTML(file_get_contents('mail/contents.html'), dirname(__FILE__));
$mail->AltBody = 'This is a plain-text message body';
$mail->addAttachment('mail/images/phpmailer_mini.png');
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Easy, you could read about SPF DNS Record.
When you send and email, services like gmail check if the sender ip is the same that the domain of the email, for example:
You send an email as "foo#gmail.com" to "bar#hotmail.com". Your
server ip is 1.1.1.1
Hotmail receives an email from "foo#gmail.com" so check if gmail.com
ip (2.2.2.2) is the same as your server (1.1.1.1). The answer is NO,
so the email is marked as spam.
To avoid that your email will marked as spam, you could use
phpmailer using a real google account and provide phpmailer the user
and password to send the email.
I tried to explain you the situation very easy on point 2. Real situation is a bit complicated but the logic is the same, check ip sender and origin. Read about SPF (and dkim) because is what are you looking for :)
http://en.wikipedia.org/wiki/Sender_Policy_Framework

How to send to BCC address when using PHPMailer to format MIME message for Gmail API?

I am using PHPMailer to build an email message. I am using PHPMailer only for MIME message formatting, not sending.
I then extract the raw message from the PHPMailer object before passing it on to the Gmail API for processing.
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
$mail->IsHTML(true);
//Disable SMTP debugging
// 0 = off (for production use)
$mail->SMTPDebug = 0;
//Set who the message is to be sent from
$mail->setFrom("fromaddress#domain.com", "From Name");
//Set an alternative reply-to address
$mail->addReplyTo("replyaddress#domain.com", "Reply Name");
//Set to address
$mail->addAddress("address#domain.com", "Some Name");
//Set CC address
$mail->addCC("ccaddress#ccdomain.com", "Some CC Name");
//Set BCC address
$mail->addBCC("bccaddress#ccdomain.com", "Some BCC Name");
//Set the subject line
$mail->Subject = "Test message";
//Set the body
$mail->Body = file_get_contents("/messagestore/some.html");
//Attach a file
$mail->addAttachment("/messagestore/some.pdf","some.pdf","base64","application/pdf");
//generate mime message
$mail->preSend();
//get the mime text
$mime = $mail->getSentMIMEMessage();
//do the google API dance
$newMailMessage = new Google_Service_Gmail_Message();
$data = base64_encode($mime);
$data = str_replace(array('+','/','='),array('-','_',''),$data); // url safe
$newMailMessage->setRaw($data);
$gmailService = new Google_Service_Gmail($google_client);
$gmailService->users_messages->send('me', $newMailMessage);
According to PHPMailer docs, CC and BCC only function for sending in the Win32 environment.
However, my MIME formatted messages transmit successfully via the Gmail API to the "TO" and "CC" addresses, but not the "BCC" address.
To summarize, When I send email using this code and I provide a 'BCC' address to the Gmail API, I do not see 'undisclosed-recipients' in the sent message header, and the message is not transmitted to the BCC address.
When I send email using the gmail web interface and I provide a 'BCC' address there, I do see 'undisclosed-recipients' in the sent message header, and the message is transmitted to the BCC address.
Does anyone know of a workaround for this issue?
PHPMailer will track the BCC recipients internally and if you were to send the message with PHPMailer it would specify the BCC recipients during the SMTP envelope.
However, when you extract the raw message from PHPMailer you lose the internal recipient list that PHPMailer was tracking. The raw message does not include the BCC information. The To: and Cc: headers will include the appropriate recipients and the GMAIL API probably uses these headers to infer the intended recipients.
To add in the BCC recipients you will need to use the GMAIL API to add these recipients before sending the message.
You didn't provide your GMAIL API code but it might follow this outline:
$message = new Message();
# construct message using raw data from PHPMailer
$message->setSubjectBody(...);
$message->setTextBody(...);
$message->setHtmlBody(...);
# *** add the BCC recipients here ***
$message->addBcc("secret.recipient#google.com");
# send the message
$message->send();
To anyone who finds this question but is not using the Gmail api to send it, only using the PhpMailer to build a raw MIME message:
if you set $phpMailer->isMail() (yes that's a setter) it will include the BCC: in the raw MIME message.
I suppose it makes no difference if the phpMailer object is set to SMTP or mail method since you'll not use it to actually send the email.
Just add
$mail->addBCC('bcc#example.com');

Cannot send mail to yahoo (any address) using PHP code and SMTP server

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);

Does Gmail not allow sender to set a return path value to receive bounce messages?

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.

Categories