Swiftmailer won't send mail, but mail() will - email

PHP's mail() function sends mail fine, but Swiftmailer's Swift_MailTransport doesn't work!
This works:
mail('user#example.com', 'test '.date('H:i:s'), '');
But this does not:
$transport = Swift_MailTransport::newInstance('');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('test '.date('H:i:s'))
->setFrom('user#example.com')
->setTo('user#example.com')
->setBody('Testing one two three');
$result = $mailer->send($message);
(The user#example.com is replaced by a valid email address in my test code.)
The mail logs for both events look very similar in both cases, and it appears that mail is being sent in the latter.
Could there be something about the message constructed by Swiftmailer that is causing it to be blocked by a spam filter?
(By the way, I have tried using the SMTP transport, with no luck; I figured that since mail() works correctly, it would be trivial to switch to Swiftmail's Mail transport...)

Which mail server are you using(like your web server or gmail,yahoo..)
this is for gmail SMTP,
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername($login_id)
->setPassword($password)
;
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('test '.date('H:i:s'))
->setFrom('user#example.com')
->setTo('user#example.com')
->setBody('Testing one two three');
$result = $mailer->send($message);
if mail() function works, then SwiftMailer should also work.
Hope it worked for you, and helped you.

Related

Email is not Delivering using Sendgrid SMTP

I am using CI library to send an email using Sendgrid smtp.
but it is not giving any response, neither email is sending.
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.sendgrid.net';
$config['smtp_user'] = 'api key name';
$config['smtp_pass'] = 'api key';
$config['smtp_port'] = '587';
$config['smtp_keepalive'] = 'TRUE';
$this->email->initialize($config);
$this->email->from('test#test.com', FROM_NAME);
$this->email->to($email);
extract($arr_var);
$sub=addslashes($row['subject']);
eval("\$subject= \"$sub\";");
$body=addslashes($row['message']);
eval("\$message= \"$body\";");
if ($subject!='') {
$this->email->subject(stripslashes($subject));
} else {
$this->email->subject($row['subject'].' - '.$this->config->item('app_title'));
}
$this->email->message($message);
$bool=$this->email->send();
Using above code i am sending en email CI v3 but it I am not receiving any email?
Twilio SendGrid developer evangelist here.
Two things stand out to me here.
First, where you set the smtp username, it should be the string "apikey" (see the instructions for sending with SMTP here).
Second, the from address you are sending from is "test#test.com". SendGrid requires you to verify the identity you send from. You either need go through Single Sender Verification or Domain Authentication and then use an email address that you have verified to send the emails from.
Once you have sorted those two bits out, your emails should send successfully.

Authenticating email through SMTP or DKIM

I'm using Perl with the
Mime::Lite module
to send email to my subscribed mailing list using
DomainKeys Identified Mail (DKIM)
and
Sender Policy Framework (SPF)
record:
use MIME::Lite;
use Net::SMTP;
my $host = 'mail.domain.com';
my $user = 'user#domain.com';
my $pass = 'password1234';
MIME::Lite->send('smtp', $host, AuthUser => $user, AuthPass => $pass);
my $msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject_enc,
Type => 'text/plain; charset=UTF-8',
Encoding => 'quoted-printable',
Data => 'Hello everyone!'
);
$msg->send;
This works just fine, but the DKIM signature fails in Gmail (and presumably others).
However, when I send email without SMTP authentication:
use MIME::Lite;
my $msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject_enc,
Type => 'text/plain; charset=UTF-8',
Encoding => 'quoted-printable',
Data => 'Hello everyone!'
);
$msg->send;
The DKIM passes fine.
So my question is: if I have successfully implemented both DKIM and SPF records on all my outgoing email, is it even necessary to use SMTP authentication to verify the sender of the email, or is piping to Sendmail with the appropriate headers sufficient to ensure best chance of delivery?
Local authentication doesn't add much to the credibility of your messages, but also there's no harm in it. It will show up in your Received headers either way, and there's a distant possibility you might be marked down by spam filters for it (they'd probably be more interested in whether any hops were unencrypted, which is independent of auth).
That you used auth shouldn't make any difference to DKIM - but what will make a difference is where (and if) your DKIM signing takes place - for example if your local mail server signs but your remote one doesn't. Does the Mail::Lite package do DKIM signing itself? Or are you relying on your servers to do it? Your headers in received messages will show you what has been signed where, and they may also give you some clues about what exactly is wrong with your DKIM signature - it would be useful if you added gmail's headers to your question.
A separate concern is performance - submitting via a sendmail binary is relatively inefficient - all sendmail binaries do is open a synchronous SMTP connection to localhost anyway, so you may as well do it directly - this is what postfix recommends.

Send email via MATLAB

I need to send an email via MATLAB and I've read the instructions for sendmail and lots of answers around here. I've tried 3 email providers and I can't really use any of them:
Gmail: I can only send email when I deactivate my anivirus
Hotmail and Yahoo: Error using sendmail (line 171) Exception reading response; Connection reset
Hotmail and Yahoo (antivirus off): Error using sendmail (line 171) Exception reading response; Unrecognized SSL message, plaintext connection?
Here's the code
mail = 'user#service.com';
password = 'passwordgoeshere';
setpref('Internet','SMTP_Server','smtp.server.com');
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Username',mail);
setpref('Internet','SMTP_Password',password);
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory');
props.setProperty('mail.smtp.socketFactory.port',port);
sendmail(mail,'Test from MATLAB','Hello! This is a test from MATLAB!')
I've used the following variables:
Gmail: smtp.gmail.com port=465
Hotmail: smtp.live.com port=465 and port=587
Yahoo: smtp.mail.yahoo.com port=587
Since deactivating the antivirus is not a good option, can anyone help me solving this?
Thank you
An alternative way on linux is to run a command line that send the email.
unix('echo "message" | mail -s "subject" example#gmail.com');
A similar method should be available for windows.
For Gmail
Change your settings to allow less secure apps to access your account. Go to the "Less secure apps" section in My Account.
Next to "Access for less secure apps," select Turn on. (Note to Google Apps users: This setting is hidden if your administrator has locked less secure app account access.)
In Matlab:
mail = 'user#otherdomain.com';
password = 'myPassword';
% Set up the preferences
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','SMTP_Username',mail);
setpref('Internet','SMTP_Password',password);
% The following is necessary only if you are using GMail as
% your SMTP server.
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory');
props.setProperty('mail.smtp.socketFactory.port','465');
subject = 'Test subject';
message = 'Test message';
sendmail(mail,subject,message)
Simply declare
mail = 'user';
Drop the extension #service.com for the variable mail.

using Mail() in php for multiple Contacts

I need to be able to send emails to contacts
stored in a database(its for newsletter not spam :P). I'm able to do this using mail() and a loop, but
I've read that this isn't a very good idea as there could be a few
hundred contacts.
What is the best way to go about this? Any advice or pointers in the
right direction will be greatly appreciated!
Thanks.
mail() will be very slow with several hundred contacts. I recommend swiftmailer at http://www.swiftmailer.org. Here's an example of sending many mails from thier site:
require_once 'lib/swift_required.php';
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
;
//Send the message
$numSent = $mailer->batchSend($message);
And you can use your SMTP connection/account to send, or sendmail.

Using CakePHP's Email component

I try to send a simple Email via CakePHP's Email Component. I'm using following code from the cookbook documentation:
$this->Email->from = 'Irgendjemand <irgendjemand#example.com>';
$this->Email->to = 'Irgendjemand Anderes <irgendjemand.anderes#example.com>';
$this->Email->subject = 'Test';
$this->Email->send('Dies ist der Nachrichtenrumpf!');
The send()-method does only return a boolean value with the value false - but no error or warning occurs.
Does somebody have a solution for that?
Have you tried changing the delivery options? There are three options: mail, smtp and debug.
$this->Email->delivery = 'debug';
$this->Email->send('test message');
debug($this->Session->read('Message.email'));
You can debug with EMail. Set the delivery to debug and the email message will be set to Session.message:
if (Configure::read('debug') > 1) {
$this->Email->delivery = 'debug';
}
$ret = $this->Email->send();
if (Configure::read('debug') > 1) {
pr($this->Session->read('Message.email'));
}
Which OS are you on? If Windows, this note may be of interest:
Note: The Windows implementation of mail() differs in many ways from the Unix implementation.
...
As such, the to parameter should not be an address in the form of
"Something <someone#example.com>". The mail command may not parse this properly while talking with the MTA.
Secondly, it may just be the case that no mail server will accept outgoing mail from your local machine due to spam protection. I have often seen that the same mail() function will not work locally, but works fine once uploaded to a trustworthy server. You could try to use an authenticated mail relay in that case (SMTP).