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);
Related
When testing out our mail server we stumbled accross an error that prevents us from sending mails via PHP, though regular sending/receiving per Mail-in-a-box works without any problems.
We are running a separate Ubuntu 18.04 server that only has Mail-in-a-box with all its needed components running.
Output in the error.log text file
PHP Fatal error: Uncaught Swift_TransportException: Expected response
code 354 but got code "554", with message "554 5.5.1 Error: no valid
recipients\r\n"
PHP file
$request_email = $_POST['request-email'];
$request_name = $_POST['request-name'];
$request_text = $_POST['request-text'];
$transport = (new Swift_SmtpTransport('data.abc.xy', 587, 'tls'))
->setUsername('contact#abc.xy')
->setPassword('*******')
->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));
$mailer = (new Swift_Mailer($transport));
$message = (new Swift_Message('Name: '. $request_name))
->setFrom(['' . $request_email => '' . $request_name])
->setTo(['contact#abc.xy'])
->setBody('E-Mail: ' . $request_email . $request_text)
->setContentType("text/html");
$result = $mailer->send($message);
What we have tried is to reinstall all of Mail-in-a-box and all of the components and checking everything for spelling mistakes. The ricipient does exist on our mail server and can receive and send mails manually via the client.
I have solved this error (554) just adding to laravel (.env file) these 2 lines:
MAIL_FROM_ADDRESS=you#email.com
MAIL_FROM_NAME="you#email.com"
Finally, run this command to flush mail cache config:
php artisan config:cache
The 554 5.5.1 error is the SMTP error "no valid recipients". It can occur if you've misspelled the recipient addresses but it can also occur if you are not properly authenticating to the outgoing server.
So the problem is that abc.xy is not a registered domain so you can't send an email to this address. I think it's not something related to your code.
You can catch the Swift_TransportException error and handle it in your own codebase like :
try {
$result = $mailer->send($message);
}
catch (Swift_TransportException $e) {
echo $e->getMessage();
}
I had the same problem in Laravel and fixed it by changing "from" to "replyTo".
I believe the problem is when sender's and app server's domains differs.
// in Laravel
// ...
namespace App\Notifications;
// ...
public function __construct(User $from, string $body)
{
$this->from = $from;
$this->body = $body;
}
// ...
public function toMail($notifiable)
{
$message = new MailMessage;
$message->subject('some subject');
// "no valid recipients" error:
// $message->from($this->from->email, $this->from->name_full);
// works:
// sender globally set in .env (MAIL_FROM_NAME, MAIL_FROM_ADDRESS)
$message->replyTo($this->from->email, $this->from->name_full);
$message->line(new HtmlString(nl2br($this->body)));
return $message;
}
// Swiftmailer respectively
// ...
$message = (new Swift_Message('Name: '. $request_name))
->setFrom(['contact#abc.xy' => 'contact'])
->setReplyTo([$request_email => $request_name])
// ...
I solved this issue by ensuring that the email address set in env file
MAIL_USERNAME=example#example.com
is the same as the "from" address in my custom notification mail class' toMail function
->from('example#example.com','My app name')
It seems as though some email services find it odd that the app is sending an email using one credential (env credentials) but a different email address is being masked as the sender.
A better solution would probably be to never set a "from" address in your notification classes.
One more thing is that it might be everything OK with your code, but the receiver address simply doesn't exist. We have a lot of customers from other companies and with time people simply change their job and the email address they used is deleted
"receiver email address doesn't exist anymore"
so if you try to send an email to non existing email address you will get that same error.
In my case, I got this error because I had forgotten to set the MX record on the recipient's domain, so the app didn't know where to deliver the email.
Another reason can be the domain being blacklisted, check spamhaus
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.
Ok I have a code to send emails. I'm using Swiftmailer and the transporter is sendmail. Here is the main code:
$transport = \Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -t -i');
$message = \Swift_Message::newInstance()
->setSubject('Notificatio')
->setFrom(array(BR_EMAIL => 'BR'))
->setTo(array($user->getEmail() => $user->getFirstName().' '.$user->getLastName()))
->setBody($msg, 'text/html', 'utf-8');
$headers = $message->getHeaders();
$headers->addIdHeader('Message-ID', time().'.'.uniqid('noreply').'#'.$_SERVER['SERVER_NAME']);
$headers->addTextHeader('X-Mailer', 'PHP v'.phpversion());
if(!$mailer->send($message, $failures)) {
echo "Failures:";
print_r($failures);
}
Now here is the strange part. If the recipient is a Gmail user, the email never gets to it. The send method doesn't give any failures, but the email is lost. It's not in the spam folder either.
But when the recipient is a Gmail Business user (email from Gmail engine with custom domain) the emails are received perfectly and into inbox.
So what is the problem? Is it Gmail filtering the mails? Or do I need to add other Headers?
EDIT
Don't know how, but the problem is resolved.
I'm using PHPMailer for a while now and never really had any problems, but last week I installed one of my CMS on a website and the client insisted on having 2 e-mails receiving the contents of the contact form of his website.
Ok, no problem I thought, just adding an e-mail address using the $phpmailer->AddAddress() function. However, adding a second receiver is causing PHPMailer to send the mail twice to both receivers. I tried adding a third receiver to see if I got it three times, but this didn't change anything. So adding 2+ receivers is causing PHPMailer to send the message twice to all receivers.
There's nothing strange in my code. It's a basic PHPMailer example:
$mail = new PHPMailer();
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddAddress("info#address.com");
$mail->AddAddress("info#address.nl");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->Send();
I've ran out of options. I have absolutely no clue where it's going wrong.
Thanks in advance
--
Just some random thought: I noticed the mailer is defaulted by iso-8859-1 and my site is running utf8. Can there be a "silent" redirect by the server itself?
//EDIT, that ^^ solved my problem
//EDIT2:
Ok, it did not.. today the script worked fine (just 1 mail with 2 receivers) but a moment ago the strange behavior started again. Any tips?
// Solution:
ok, I feel quiet stupid! The answer Zulkhaery Basrul came closest to the actual problem! This is what happened: I was sending an e-mail to multiple addresses in the "to" field. For example:
To: A, B, C
Both A/B are my own adresses. In outlook I had some message rules to put e-mails with certain addressees inside a specific folder. Both A and B had this rule.
When I recieved my e-mail, both mails contained both mailaddresses in the to-field. Causing both of the mails to meet the rule requirements for both rules. Thus keeping the original in one folder and creating a copy in the other (twice).
Thanks for thinking tho :)
You can use $SingleTo property.
From PHPMailer docs:
$SingleTo
Provides the ability to have the TO field process individual emails,
instead of sending to entire 'TO addresses'
$mail = new PHPMailer();
$mail->SingleTo = true; //will send mail to each email address individually
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddAddress("info#address.com");
$mail->AddAddress("info#address.nl");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->Send();
Try to add after
$mail->send();
$mail->ClearAddresses();
Ok, if someone runs into this problem like I did:
My website runs in utf-8, PHPMailer uses iso-8859-1 by default. Somehow the script is run in both charsets causing duplicates. Changing the charset in the class.phpmailer.php file to utf-8 solved the problem!
I think these will do the job :)
$mail->SingleTo = true;
$mail->CharSet = "UTF-8";
I tried everything, UTF8 / BOM check on all files, SingleTo-property, CharSet-property.
It didn't work.
I used the "View Details" function from Gmail and saw that my duplicate mails were sent without a subject.
So, I ended up with a very, very dirty fix.
I put this DIRECTLY below the first line of the send function in the class file:
public function Send() {
if ($this->Subject == "") {
return true;
}
// ... rest of code...
}
I ran into the same problem. The one and only right answer is $mail->ClearAllRecipients()
When I used the accepted answer BCC mails were being sent to all recipients.
SingleTo Is not a good idea. It only works with "sendmail" or "mail" transports, not with SMTP. If you use SingleTo with SMTP, this parameter is just ignored without any error or warning, and you may get duplicates.
According to the authors of the library, SingleTo is planned to be deprecated in the release of PHPMailer 6.0, and removed in 7.0. The authors have explained that it's better to control sending to multiple recipients at a higher level: "PHPMailer isn't a mailing list sender". They tell that the use of the PHP mail() function needs to be discouraged because it's extremely difficult to use safely; SMTP is faster, safer, and gives more control and feedback.
But SMTP is incompatible with SingleTo -- that's why the authors of PHPMailer will remove SingleTo, not SMTP.
Please tell me what I am doing wrong. I am sending an email using the Zend_Mail class like this:
$message = <<<STR
You have a new invoice!
Sign in to your clientarea to see it.
Best regards,
Company name
STR;
$mail = new Zend_Mail();
$mail->setBodyText($message);
$mail->setFrom('billing#company.com', 'Company.com');
$mail->addTo('client#email.com', 'Client Name');
$mail->setSubject('You have a new invoice!');
$mail->send();
It is received as a spam though. There are other applications such as Webmin on my server and emails they send is not treated as SPAM.
I have solved this by adding these lines:
$mail->setReplyTo('contact#company.com', 'Company');
$mail->addHeader('MIME-Version', '1.0');
$mail->addHeader('Content-Transfer-Encoding', '8bit');
$mail->addHeader('X-Mailer:', 'PHP/'.phpversion());
The critical line seems to be adding Reply-To header. Without that it would always go to SPAM. Once I set the Reply-To header email clients stopped treating it as spam.