How to send email without my own mail server by PHP? - php

Is it possible to use Google's mail server for testing purpose,and replace the address of mail server when my own server is ready?

You can just send your mails via smtp.gmail.com (port 465 or 587) as with any email client. Note anyway that you will need a Google email account for this. More details are here: Configuring email clients for using GMail

I suggest you to use phpmailer, this is an example working code with it:
<?php
include_once("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
// enable SMTP authentication
$mail->SMTPAuth = true;
// sets the prefix to the server
$mail->SMTPSecure = "ssl";
// sets GMAIL as the SMTP server
$mail->Host = 'smtp.gmail.com';
// set the SMTP port
$mail->Port = '465';
// GMAIL username
$mail->Username = 'your.gmail.user#gmail.com';
// GMAIL password
$mail->Password = 'your-gmail-password';
$mail->From = 'email address who send the email';
$mail->FromName = 'yourname';
$mail->AddReplyTo('email to reply', 'name to reply');
$mail->Subject = 'Test Gmail!';
if($is_your_mail_an_html){
$mail->MsgHTML($html_message);
$mail->IsHTML(true);
}else{
$mail->Body = $text_message;
$mail->IsHTML(false);
}
$mail->AddAddress('to address email', 'to name');
if(!$mail->Send()){
echo = $mail->ErrorInfo;
}else{
$mail->ClearAddresses();
$mail->ClearAttachments();
}
?>
But even without phpmailer, you can use gmail to send emails; Just set the port to 465 and enable the ssl auth.
P.s.: dont try to send nesletter throught gmail; they will block your account for 1 day if you send more than $x email per day ($x is 500 on the google documentation, but my experience say that is around 85!)

Yes google does offer that via smtp.
smtp.google.com
port: 587
You also will need your google username and password to send emails.
You need a php smtp class. PHPMailer has one.

If you run a Windows server you can just do this (if you have access to the php.ini). Otherwise follow Sarfraz suggestion.
<?php
ini_set('sendmail_from','test#test.com');
ini_set('SMTP','smtp.test.net');
mail(...);
?>

Related

PHPMailer sets the From same as the smtp username

I am using PHPMailer library to send SMTP email for booking enquiry. (Note: The same problem I was facing in PHP Pear Mail library as well).
I set the email from as below
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true;
$mail->Username = 'gmail.owner#gmail.com'; // SMTP username
$mail->Port = 465; // TCP port to connect to
$mail->isHTML(true);
$mail->Password = 'xxxx';
$mail->setFrom(mark.antony#example.com, Mark Antony);
$mail->replyTo(mark.antony#example.com, Mark Antony);
$mail->addAddress(gmail.owner#gmail.com, Gmail Owner);
$mail->Subject = $whatever subject;
$mail->Body = $whatever html;
The problem is after sending email in the received mailbox I see the From: is the same as To: (Or same as the gmail/smtp username).
I have fixed the problem on reply to by setting replyTo value.
Is there anyway I can fix this? Or is that how it suppose to be?
Google does not allow you to set arbitrary from addresses, because doing so is usually forgery. If you try to, gmail will ignore it and substitute your Username address, which is what you're seeing.
There is one exception to this: you can set up additional aliases in your gmail settings (which you need to verify), and once you've done that you can use those addresses as from addresses.
If you're using this for a contact form (as it sounds like you are), setting to and from to your own address, and the submitter as a reply-to is the correct way to go, as the PHPMailer contact form example demonstrates.

phpmailer to send email from any gmail

I used the following code snippet using phpmailer class inorder to send an email using gmail server when a form is submitted in my web application! Though it works for my personel gmail address, it does not send emails when I replace my gmail address and password with my clients gmail address and password!
How can i correct this probem?
require "phpmailer/class.phpmailer.php"; //include phpmailer class
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 465; //Gmail SMTP port
$mail->Encoding = '7bit';
// Authentication
$mail->Username = "sunethperera#gmail.com"; // Your full Gmail address
$mail->Password = "******"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['emailid'], $_POST['fullname']);
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "Pharmacy order confirmaton"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress($_POST["Email"], "Recipient Name"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
When I send the message from my client's email, I get the error message:
SMTP Error: Could not authenticate.
How can I correct this issue?
Trying using this:
$mail->SMTPSecure = "tls";
Remove the Encoding parameter
And also check you have the correct from address and the password as well.
Update your PHPMailer right now. You are running an old vulnerable version. Read the troubleshooting guide and do what it says. Do not use the submitter's address as the from address - its forgery and will make you fail SPF checks. Gmail does not let you send from arbitrary from addresses. Your auth problem is covered in the guide, but you will need to either enable less secure apps in Gmail, or implement oauth, as described in the docs and examples.
This has all been covered many times on SO - search before you post.

Send email with false email on PHP

I installed recently PHPMailer, because of not being able to send without it.
Now I have another problem: I want to send an email with an invent one, for example, "no-reply#my-domain.com". I don't seem to be able to do it. when I send with Sendmail, it just won't send, and if I use SMTP with autentication, it sends with my email.
require "../PhpMailer/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "marcelo43#gmail.com";
$mail->Password = "My Password";
$mail->setFrom('no-reply#my-domain.com','MyDomain Admin');
$mail->addAddress('to#gmail.com','To');
$mail->Subject = "Test";
$mail->msgHTML('My message');
if(!$mail->send())
echo 'Could not send email';
else
echo 'Email succesfully sent';
This just sends an email to "to#gmail.com" with "MyDomain Admin" as the name and "marcelo43#gmail.com" as its email.
What do I need to do to send with the email "no-reply#my-domain.com"?
You can't send email with a false email because Gmail blocks mail from an other domain that its own.
If you want to send an e-mail with an other domain you should use an other SMTP server. You can use an SMTP from OVH for example if you have a mail server. Otherwise, Yahoo will let you send you e-mail I think.
Gmail doesn't let you use arbitrary from addresses at all, even from gmail domains. You can however create aliases within your gmail settings that are allowed as from addresses. If you want to use your own domain via gmail, you will neeed to configure gmail as the MX for your domain. If you do that, you will be able to do what you ask.
BTW, this is mentioned in the PHPMailer docs.

How to send an email using php [duplicate]

This question already has answers here:
send email using php
(3 answers)
Closed 6 years ago.
When I execute this code, a new tab is opened. It displays my code. How do I fix this?
<?php
$mail = new PHPMailer();
//Send mail using gmail
if($send_using_gmail){
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "myemailid#gmail.com"; // GMAIL username
$mail->Password = "mypassword"; // GMAIL password
}
//Typical mail data
$mail->AddAddress('myemailid#gmail.com', 'Name');
$mail->SetFrom('myemailid#gmail.com', 'Name');
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";
try{
$mail->Send();
echo "Success!";
} catch(Exception $e){
//Something went bad
echo "Fail :(";
}
?>
I am trying to send an email using PHP here.
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
try this one it worked for me. If didnt work let me know
I know this will not answer OP's question but i want to make this in people's attention, Please excuse me...
Source :- https://sendgrid.com/blog/gmail-dmarc-update-2016/
What is Gmail changing?
June of 2016 Gmail will change its DMARC policy from p=”none” to p=”reject.” This means any message sent using gmail.com in the from address, will have to originate from Gmail’s infrastructure.
What does this mean for me?
It depends. If you have any mail streams that send messages using gmail.com in the from address, you will have to make changes before June, or risk having those messages filtered or blocked outright.
If you only send email using your own domain or another domain that you control, you have nothing to worry about. However, it’s not uncommon for some applications or websites to send messages using their users’ email addresses. For example, if a user wants to send a message to their friend using your platform, it could make sense to send the message using their personal email address. If their email address happens to be a gmail.com address, this message will no longer deliver once these changes take place. A good alternative to sending mail from your user’s email address is to use their name in the friendly from. A “friendly from” is when you use a name to appear as the from address, instead of the email address itself:
exampleuser#yahoo.com can be sent as “Example User”
This way your recipients still recognize the individual that sent the message, and you’re no longer at risk of violating Gmail’s DMARC policy.

Can I use gmail as smtp server for my website

Hello I am trying to get a website up and running. It is currently hosted on AWS, so I do not have my own smtp server running at this moment. So after reading a few articles, I have understood that we could used gmail as a smtp server.
I wanted to double check if what I read was right, I am going to use smart job board software, can I plug in the values provided by gmail and use that as an smtp server??
Yes, Google allows connections through their SMTP and allows you to send emails from your GMail account.
There are a lot of PHP mail scripts that you can use. Some of the most popular SMTP senders are: PHPMailer (with an useful tutorial) and SWIFTMailer (and their tutorial).
The data you need to connect and send emails from their servers are your GMail account, your password, their SMTP server (in this case smtp.gmail.com) and port (in this case 465) also you have to make sure that emails are being sent over SSL.
A quick example of sending an email like that with PHPMailer:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->Username = "john.doe#gmail.com"; // SMTP account username
$mail->Password = "your.password"; // SMTP account password
$mail->SetFrom('john.doe#gmail.com', 'John Doe'); // FROM
$mail->AddReplyTo('john.doe#gmail.com', 'John Doe'); // Reply TO
$mail->AddAddress('jane.doe#gmail.com', 'Jane Doe'); // recipient email
$mail->Subject = "First SMTP Message"; // email subject
$mail->Body = "Hi! \n\n This is my first e-mail sent through Google SMTP using PHPMailer.";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
I successfully use the GMail SMTP server.
We do have a corporate GMail account, though I don't think that matters. A personal GMail account should suffice.
I do not have a PHP sample however the following configuration for ASP.Net should provide adequate guidance:
<mailSettings>
<smtp from="me#gmail.com">
<network enableSsl="true" host="smtp.gmail.com" port="587" userName="me#gmail.com" password="mypassword" />
</smtp>
</mailSettings>
If someone has a suitable PHP sample, feel free to edit my answer or post your own.
Authentication is required I believe, but I don't see why not. I won't do the research for you, but there are a couple things to look into:
Their SMTP server requires TLS encryption and is hosted on a non-standard port (995). You will need to ensure that AWS supports both of these options for SMTP outbound.
There is probably a cap on emails you can send before being marked as spam. You should research this and ensure it is not beyond your requirements.
You can user PHPMailer class for this job. And you can easily configure the smtp.
An example of configuration
if (class_exists(#PHPMailer)) {
$smtp_mail = new PHPMailer();
$smtp_mail->isSMTP();
$smtp_mail->SMTPAuth = true; // enable SMTP authentication
$smtp_mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$smtp_mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$smtp_mail->Port = 465; // set the SMTP port
$smtp_mail->Username = "info#example.com"; // GMAIL username
$smtp_mail->Password = "password"; // GMAIL password
$smtp_mail->From = "info#example.com";
$smtp_mail->FromName = "Name";
$smtp_mail->AltBody = "This is the body when user views in plain text format"; //Text Body
$smtp_mail->WordWrap = 50; // set word wrap
$smtp_mail->AddReplyTo("info#example.com","Name");
$smtp_mail->isHTML(true); // send as HTML
}
Although you can technically use Gmail as SMTP server, it is not recommended for larger websites. By time you may receive issues like "421 4.7.0 Temporary System Problem" or similar, is it was not designed to be used by an application, rather a single person.
Related issue for the error message above:
Gmail SMTP error - Temporary block?

Categories