I'm working on a form on a Zend Framework 2 website, which sends an e-mail using Mail\Transport\Sendmail().
I don't know if it's a server (I'm working in local, Win10 and XAMPP) or framework problem, but the e-mails I receive have duplicate subject.
My code is quite long but very simple at its core, and I use setSubject($subject) to add the subject to the e-mail. If $subject = "TEST", the e-mail I will receive will have the subject "TEST, TEST".
What is wrong and how could I fix this?
if ($form->isValid()) {
$transport = new Mail\Transport\Sendmail();
$mail = new Mail\Message();
$mail->setFrom("my#email.com", "Me");
$mail->addTo($senderEmail, $senderName);
$mail->setSubject($subject);
$mail->setBody($body);
$transport->send($mail);
}
All works fine, but on the received e-mail subject will be duplicated, with a comma in the middle: "$subject, $subject".
I had the same problem, and I changed the transport agent to Zend\Mail\Transport\Smtp instead of using sendmail.
use Zend\Mail\Transport\Smtp as SmtpTransport;
...
$transport = new SmtpTransport();
Related
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);
I want to send mails from different accounts in my Laravel application.
Therefore I created a new swiftmailer transport
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.example.org', 25))
->setUsername('your username')
->setPassword('your password')
;
// Create the Mailer using your created Transport
$swift_mailer = new Swift_Mailer($transport);
When I send the message directly from the Swift_Mailer object
// Create a message
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['john#doe.com' => 'John Doe'])
->setTo(['receiver#domain.org', 'other#domain.org' => 'A name'])
->setBody('Here is the message itself')
;
// Send the message
$result = $swift_mailer->send($message);
I get a mail that is actually send from my mail server:
However, when I send a mail from a Laravel Mailer (as explained here) using the same Swift_Mailer object from above:
$view = app()->get('view');
$events = app()->get('events');
$mailer = new \Illuminate\Mail\Mailer($view, $swift_mailer, $events);
$mailer->alwaysFrom('john#doe.com', 'John Doe');
$mailer->alwaysReplyTo'john#doe.com', 'John Doe');
$mailer->to('my_email#test.com')->send(new \App\Mail\Test);
then it appears in Gmail, that the mail wasn't really send from my mail server:
When I click on details it tells me that
The sender domain is different from the domain in the "From:" address.
I actually own both domains. In my .env file the FROM address is ****.com and I wanted to create a new mail from a ****.net address.
Why does Gmail think I have send a mail via my ****.com address if use the Laravel Mailer class?
Note: This is not a duplicate of multiple mail configurations - this is in fact where I started looking, but the 5 year old answer was not quite working and I gave this answer, but I am stuck with the above described problem.
I solved it as follows:
I compared first the header of both mails, I realized that in the mail using the Mailer object I had a respond address with the ***.com domain.
spf=pass (google.com: domain of respond#****.com designates **** as
permitted sender) smtp.mailfrom=respond#****.com Return-Path: <respond#***.com>
Then I checked the Mailer class (with hindsight, I wonder why I didn't look here first..) and I found the following snippet that I added some month ago:
$this->withSwiftMessage(function ($message) {
$message->getHeaders()
->addTextHeader('Return-Path', 'respond#****.net');
});
So I just had to remove it, now everything works.
In summary, I was convinded the problem was in the Mailer class, but it was simply inside the build method of the Mailable that I was using.
I am new in wordpress. I am sending mail via wp-mail function locally I have install east wp-mail plugin for smtp.
include("wp-mail.php");
$to = 'test#gmail.com';
$subject = 'Apple Computer';
$message = 'Steve, I think this computer thing might really take off.';
wp_mail( $to, $subject, $message );
But I am getting error liike :
Slow down cowboy, no need to check for new mails so often!
Please guide me....
Do i need to change in any file or install any plugin ???
wp_mail works similar to PHP's function mail. You can read more about it here. PHP mail function needs access to sendmail binary, as stated in docs, you shouldn't have this configured in localhost, that's why it fails to send emails.
In order to send emails when testing your site in localhost you should configure SMTP to send emails.
There's a pretty good plugin called WP Mail SMTP, you can install it here.
It overrides the wp_mail function and enables you to send emails using the Gmail SMTP server, for instance. You can use any SMTP server you want.
If you not want to use any plugin to setup your smtp you can do it easy with code . WordPress have their own hook to setup smtp.
Here is the code. Simply by adding your Host, Username and Password. You can send email from your local machine.
add_action( 'phpmailer_init', 'my_phpmailer_example' );
function my_phpmailer_example( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.example.com';
$phpmailer->SMTPAuth = true; // Force it to use Username and Password to authenticate
$phpmailer->Port = 25;
$phpmailer->Username = 'yourusername';
$phpmailer->Password = 'yourpassword';
// Additional settings…
//$phpmailer->SMTPSecure = "tls"; // Choose SSL or TLS, if necessary for your server
//$phpmailer->From = "you#yourdomail.com";
//$phpmailer->FromName = "Your Name";
}
You can learn more about it on WordPress Codex
I am teaching myself php and how to use Swiftmailer. I have followed the doc and added the code below to my php file, but it returns array( ). I can't figure out what could be wrong and how to check. I don't seem to have access to the remote server's ini file from the hosting provider. Would appreciate any help in determining what may be wrong and how to correct. thanks!
require_once 'Swift/lib/swift_required.php';
//Use simple mail transport
$transport = Swift_MailTransport::newInstance();
// Create the message
$message = Swift_Message::newInstance();
$message->setTo("xxx#yyy.com");
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You're our best client ever.");
//$message->setFrom($email);
$message->setFrom("xxx#zzz.net");
$message->setReturnPath('xxx#zzz.net');
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message, $failedRecipients);
// Show failed recipients
print_r($failedRecipients);
I'm trying to set a default sender to all messages in Swift Mailer 4.3.0, but couldn't find a proper solution. I want to avoid using ->setFrom() in every message, since it'll be a future headache. I could use a constant for that, but I was looking for a more elegant solution.
$message = Swift_Message::newInstance()
->setSubject('subject')
->setFrom(array('sender#domain.com' => 'Sender'))
->setTo(array('recipient#domain.com'))
->setBody('Message');
Thanks!
You can't.
As far as I know, you can't omit this parameter.
A From: address is required and is set with the setFrom() method of the message. From: addresses specify who actually wrote the email, and usually who sent it.
This is maybe the best solution:
->setFrom(MyClass::FROM_EMAIL);
Once thing you can try, is to create an instance once in your application, and clone it when you want to send a new mail without re-defining the from part:
// somewhere early in your app
$message = Swift_Message::newInstance()
->setFrom(array('sender#domain.com' => 'Sender'));
And then, somewhere else:
$newMessage = clone $message;
$newMessage
->setSubject('subject')
->setTo(array('recipient#domain.com'))
->setBody('Message')
->send();
If you use a SwiftMailer ≥ 4 and are familiar with Composer, you can use a Defaults Plugin for SwiftMailer to achieve this.
First install the package:
composer require finesse/swiftmailer-defaults-plugin
Second set up a Swift_Mailer instance:
$mailer = new Swift_Mailer(/* your transport here */);
$mailer->registerPlugin(new Finesse\SwiftMailerDefaultsPlugin\SwiftMailerDefaultsPlugin([
'from' => ['sender#domain.com' => 'Sender']
]));
And then send emails without specifying the from address:
$mailer->send((new Swift_Message())
->setSubject('subject')
->setTo(array('recipient#domain.com'))
->setBody('Message'));