Here's a scenario; I want to send email using PHP and Zend Framework. Here's an example on how this is usually done:
$smtp = new \Zend_Mail_Transport_Smtp(array(
'host'=> 'localhost'
, 'auth' => 'login'
, 'ssl' => 'TLS'
, 'username' => 'john'
, 'password' => '123'));
\Zend_Mail::setDefaultTransport($smtp);
$mail = new \Zend_Mail();
$mail->addTo('jane.smith#localhost', 'Jane Smith');
$mail->setSubject('Greetings');
$mail->setBodyText('Hi there');
$mail->setFrom('john.smith#localhost', 'John Smith');
$mail->send();
My question is, how can we make sure that the from address (in this case 'john.smith#localhost') actually exists and it belongs to the username and password provided to SMTP connection? Because if it is not, then I can send email on behalf of anyone!
[UPDATE]
I believe I found a partial answer to my own question. It should be done through IMAP / POP3 protocols. But yet these two protocols take in a username and password in order to athenticate and so you can not check for the association between the provided username and an email address. So the question is how to authenticate an email address through IMAP / POP3 in PHP and Zend Framework?
SMTP does allow to send emails as anyone. This is why most security-oriented people promote the use of digital signatures.
Validation can be done by the SMTP itself. Gmail's SMTP will refuse to send emails with an address not attached to the account for example.
Related
I've searched high and low and I can't seem to find a working example of a swiftmailer script that uses a hotmail smtp, I have tried using the google one's in examples and upating the port, user and password but the page just hangs until it times out.
My code:
require_once 'lib/swift_required.php';
// Create the SMTP configuration
$transport = Swift_SmtpTransport::newInstance("smtp.live.com", 465, "TLS");
$transport->setUsername("xxxx#hotmail.com");
$transport->setPassword("XXXXXX");
// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
"XXXX#company.com" => "xxx",
));
$message->setCc(array("another#fake.com" => "Aurelio De Rosa"));
$message->setBcc(array("boss#bank.com" => "Bank Boss"));
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You're our best client ever.");
$message->setFrom("XXXXX", "XX XXXXXXX");
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message, $failedRecipients);
// Show failed recipients
print_r($failedRecipients);
I expect you need to set your encryption param correctly. For port 465 use ssl; for 587, tls. hotmail isn't any different to anywhere else - your code would not work anywhere.
I was trying to set from in php mailer (gmail smtp). But in inbox it shows from mygmailaccount#gmail.com. here is my code :
$this->_mail->Username = "mygmailaccount#gmail.com";
//Password to use for SMTP authentication
$this->_mail->Password = 'mypassword';
$this->_mail->From="support#mydomain.com";
$this->_mail->FromName="Support Team";
//Set who the message is to be sent from
$this->_mail->setFrom('support#mydomain.com', 'Support Team',false);
But when I receive email in inbox its show from : mygmailaccount#gmail.com
and I want to show from : support#mydomain.com
Can anyone help me to find out what I am missing.
This is a gmail restriction, though it's not all bad news! In your gmail preferences you can configure aliases for your account, and you can send out using them as your from address. So you can set the from address, but only to a choice from that preset list, not any arbitrary address. This question has been asked on here before.
I used to use "Php mailer" to authenticate using a user's email and password to send from their address in my app, but it is such an inconvenience.
So I want to do something like "Mailchimp" does. Mailchimp allows a user to set their email and then Mailchimp just uses user's email as the "Sender".
Please anyone help me with this email issue.
You can't. MailChimp requires you to verify yourself as the owner of the e-mail address and domain through DNS settings. See more details
What you need to do is first verify the user's email address is correct. Here is a good walk-through that shows how to do that:
http://code.tutsplus.com/tutorials/how-to-implement-email-verification-for-new-members--net-3824
You'll also want a disclaimer as proof that they have given you permission to send emails on their behalf.
Disclaimer: by enrolling for this service you are granting this
app permission to send emails on your behalf.
Next you'll want to create a functional email account that belongs to your app only. For example:
clientmailer#myapp.com
Then, last but not least- continue to use PHPMailer, but use it differently. Authenticate and send emails from your app's functional account, but set the AddReplyTo address as your customer's address and name. For example:
select email, firstname from client;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tsl';
$mail->SMTPDebug = 1;
$mail->Host = yourhost#myapp.com;
$mail->Port = EMAIL_PORT;
$mail->Username = clientmailer#myapp.com;
$mail->Password = yourpass;
$mail->SetFrom('clientmailer#myapp.com', 'Functional Mailer');
$mail->AddReplyTo('(email from select)', '(firstname from select)');
I am using this extension.
http://www.yiiframework.com/extension/smtp-mail
I would work properly. But From Email id in mail not set that i have defined in "SetFrom()" function but it takes Gmail username (myemail#gmail.com).
Below is my code for sending mail in my Controller.
$mail = Yii::app()->Smtpmail;
$mail->SetFrom("otherid",$from_name); // This id not coming in my response mail
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->AddAddress($to, "");
config/main.php
'Smtpmail'=>array(
'class' => 'application.extensions.smtpmail.PHPMailer',
'Host' => "smtp.gmail.com",
'Username' => 'myemail#gmail.com',
'Password' => 'password',
'Mailer' => 'smtp',
'Port' => 465,
'SMTPAuth' => true,
'SMTPSecure' => 'ssl'
),
Gmail doesn't seem to allow sending the email via a different email-id if its not registered to the primary gmail account.
When logged in to the primary gmail account -> go to Account tab (top-right).
Search for Add another email address under Send mail as.
Add the new email address there. Confirm the link sent to the additional email account.
On confirmation, your account can send emails from either of the address.
Try the new email address now in $mail->SetFrom("new_gmail_id#gmail.com", $from_name); and it will work fine.
I have tried that and it works. Let us know if you have done all this already.
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);