I'm trying to set up a web site on a (shared) web hosting account. And I'm facing an issue with the mail() method. The site has a contact form where a user can submit their feedback:
Name: _____________
Email: ____________
Message: __________
So I was using the mail() method as such to send it as an email to my own account:
if(mail("mysitecustomerfeedback#hotmail.com",
"Customer message",
$message,
"From: $name <$email>\r\n".
"Reply-To: $name <$email>\r\n".
"X-Mailer: PHP/".phpversion()) === true)
{
$messageWasSent = true;
}
So when I try it, the email is dispatched, but there are several issues with it.
For instance, for my test I provided my actual email, say, johndoe#hotmail.com but when the email is received in my mysitecustomerfeedback#hotmail.com Hotmail account, the from email field is filled out with the default email on the shared hosting server, or something like web-user234#server875.web-hosting.com and not johndoe#hotmail.com as I would expect it to be.
Also the email was automatically placed into Junk by Hotmail, even though it contained no attachments, images, or anything. It was a plain text message.
Here's a screenshot:
So I understand that this shared hosting company doesn't want me to send spam using mail() method, but how else can I send these emails off my web site? Is there a replacement method for mail()?
You can use PHPMailer.
Example:
$mail = new PHPMailer;
$mail->isSMTP();
// You can use the SMTP host/auth details
// supplied by your personal email provider.
$mail->SMTPAuth = true;
$mail->Host = 'smtp.example.com';
$mail->Username = 'myUsername';
$mail->Password = 'myPassword';
$mail->setFrom('my#example.com');
$mail->addAddress('to#example.com');
$mail->Subject = 'A subject.';
$mail->Body = 'A message.';
$mail->send();
Since you authenticate with hotmail/gmail or whichever email server you use, the recipient will receive the email just as if you sent it using your favourite email client (this is a PHP email client).
Related
I have been digging around to look for suggestions on how to ensure an email address that has been inputted in a web form is valid or not. It seems like the best solution is to send an automated email to the email address the user has submitted, if they receive it then great if not its obviously not valid.
what i want to do is have the user fill in the contact form, when they submit the form it checks all validation and then sends an automated email to the users email address, then depending on whether the email was successfully received send the original query to my email.
I can get the automated email sending ok but is there a way to get php to return a email received confirmation so that i can process the rest of my script?
here is my code
<?php
// if user has submitted the form and there are no errors
if(($_SERVER["REQUEST_METHOD"] == "POST") && !$nameErr && !$emailErr && !$phoneErr && !$messageErr && !$botErr && !$pointsErr)
{
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Set the hostname of the mail server
$mail->Host = 'mailout.one.com';
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = 'USERNAME';
//Password to use for SMTP authentication
$mail->Password = 'PASSWORD';
//Set who the message is to be sent from
$mail->setFrom($from, $name);
//Set an alternative reply-to address
$mail->addReplyTo($email, $name);
//Set who the message is to be sent to
$mail->addAddress($from, 'PERSON');
// To send automated reply mail
$autoemail = new PHPMailer();
$autoemail->From = $from;
$autoemail->FromName = "PERSON";
$autoemail->AddAddress($email, $name);
$autoemail->Subject = "Autorepsonse: We received your submission";
$autoemail->Body = "We received your submission. We will contact you soon ...";
$autoemail->Send();
if(!$autoemail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
//CHECK AUTOMATED EMAIL HAS BEEN RECEIVED BY THE USER THEN SEND THEIR ENQUIRY EMAIL
//Set the subject line
#$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
#$mail->Body = $message."<p><strong>Club Enquiry Relates To: </strong>".$club."</p><p><strong>Client Details</strong></p>Name: ".$name."<br/>Email: ".$email."<br />Tel No: ".$phone."<p><strong>Extras</strong></p>How Did you find our website? ".$hearsay."<br/ >Spam Score: ".$points." out of ".$maxPoints."<br />IP Address: ".$_SERVER['REMOTE_ADDR'];
//Replace the plain text body with one created manually
#$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
#$mail->addAttachment('images/phpmailer_mini.png');
echo"<h2>Thank You!!!</h2>";
echo "<p>Your message has been sent successfully, we aim to respond within a couple of days. Please check your Junk/Span folder incase it ends up there.</p>";
}
But I'm unsure how to tell if the auto response email was delivered or not?
any help is greatly appreciated
An automatic and instant delivery confirmation is not possible, so if you're thinking about waiting for that confirmation for your script to perform the second part of the signup process, you need to rethink your strategy.
Also, keep in mind that a valid email doesn't necessarily mean that the email belongs to the person signing up. I could in theory sign up to your site using an email of someone I know and be OK just because the email exists.
If you need email validation, you should consider sending an automated email with a unique disposable link which the user clicks to complete the process or containing a unique code which the user must enter in a confirmation step.
My suggestion would be to include a link in the email pointing to your site (i.e., yoursite.com/verification/random_unique_string)
The random_unique_string may be anything as long as it's not reversible by a malicious user. For inatance, it could be a hash of the recipient email, date sent, random salt, etc which is unique and when clicked, leads to the finalization of the sign up process
I'm trying to use PHPMailer on my website to send mail as part of a contact form. I would like to use contact#example.com instead of standard_email#example.com to send mail, for filtering and security. My mail is handled by GSuite (formerly called Google Apps) and is setup like below:
User - standard_email#example.com
Alias - contact#example.com --> standard_email#example.com
I have sending working perfectly when using standard_email#example.com in the PHPMailer configuration, but when I try to send using the alias it does not work. Is this not possible with GSuite aliases?
Contact Controller
define("SMTP_HOST", "smtp.gmail.com");
define("MAIL_ADDRESS", "alias#example.com");
define("SMTP_USERNAME", "alias#example.com");
...
//Configure SMTP authentication
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = SMTP_HOST;
$mail->SMTPAuth = true;
$mail->Username = SMTP_USERNAME;
$mail->Password = SMTP_PASSWORD;
$mail->SMTPSecure = "tls";
$mail->Port = SMTP_PORT;
//Set email headers
$mail->setFrom(MAIL_ADDRESS, "Contact Us Submission");
$mail->addAddress(MAIL_ADDRESS, "Contact Us Submission");
$mail->addReplyTo($form_email, $form_name);
$mail->isHTML(true);
//Set email content
$mail->Subject = htmlspecialchars($form_subject);
$mail->Body = htmlspecialchars($form_comments);
$mail->AltBody = htmlspecialchars($form_comments);
//Attempt to send the message and display the results to the user
if ($mail->send() == false) {
return new MailResponse(1, "Message could not be sent", $contactSubmission);
} else {
return new MailResponse(0, "Message sent successfully", $contactSubmission);
}
I've also tried using standard_email#example.com as the SMTP_USERNAME and alias#example.com as the MAIL_ADDRESS, but that didn't work either.
Results
There are no reported errors, and the page displays the "success" mail message; however, no mail is actually sent/received when I visit my user. Since GSuite apparently routes all of the alias mail to my standard address, I should be seeing it.
Let me know if I'm missing something, and thanks!
The solution from #Synchro works and for convenience here is a short step by step solution:
On your computer, open Gmail.
In the top right, click Settings Settings and then See all settings.
Click the Accounts and import or Accounts tab.
In the "Send mail as" section, click Add another email address.
Enter your name and the address you want to send from.
Click Next Step and then Send verification.
Click Add Account
Learn more: https://support.google.com/mail/answer/22370?hl=en
Everything should be working now :)
is anybody know any solution for below problem
in my project, i am send email to client using smtp and php mailer and gmail script. so when i am send mail gmail send mail to particular client. for that i am passing gmail login and user name which is valid. all mail are sending properly. but sometime it may happen that some client not receive mail and at that time i am not able to get or trace any error. so i there any way, when i am sending mail to client , and when client get it and read it at that time i got any kind of confirmation.
Please if anybody have any idea , help me out.
<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* #package phpmailer
* #version $Id$
*/
require ('../class.phpmailer.php');
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = "Please return read receipt to me.";
$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "SMTP SERVER IP/DOMAIN"; // SMTP server
$mail->Username = "EMAIL USER ACCOUNT"; // SMTP server username
$mail->Password = "EMAIL USER PASSWORD"; // SMTP server password
$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("someone#something.com","SOMEONE");
$mail->From = "someone#something.com";
$mail->FromName = "SOMEONE";
$to = "other#something.com";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message[Test Read Receipt]";
$mail->ConfirmReadingTo = "someone#something.com"; //this is the command to request for read receipt. The read receipt email will send to the email address.
$mail->AltBody = "Please return read receipt to me."; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
Some modification need to be done in above script.
Configure SMTP mail server.
Set the correct FROM & FROM Name (someone#something.com, SOMEONE)
Set the correct TO address
from
also
Delivery reports and read receipts in PHP mail
You can always add reply to mail address which will take care if there is any error so you will get email back, and for if it's read, include a picture (blank picture of 1 pixel will do) and add code in that picture like and you can see how many hits that image did recieve or recieved any at all.
You can check that things in two different ways like, by using the header "Disposition-Notification-To" to your mail function, it will not going to work in all case because most people choose not to send read receipts. If you could, from your server, influence whether or not this happened, a spammer could easily identify active and inactive email addresses quite easily.
You can set header like
$email_header .= "Disposition-Notification-To: $from";
$email_header .= "X-Confirm-Reading-To: $from";
now the another method to check is by placing an image and you can add a script to onload of that image, that script will send notification to your system that xxx user have read the mail so, in that way you can track delivery status of the mail.
In fact, I can't think of any way to confirm it's been delivered without also checking it gets read, either by including a image that is requested from your server and logged as having been accessed, or a link that the user must visit to see the full content of the message.
Neither are guaranteed (not all email clients will display images or use HTML) and not all 'delivered' messages will be read.
Though for more info https://en.wikipedia.org/wiki/Email_tracking
I am using the following PHP CODE to send BULK MAIL .But Mails seems to Land in SPAM.I am using "phpmailer" class to send the mail.
require 'mailer/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "info#gmail.com";
$mail->Password = "Bexwa44Puciz"; // GMAIL password
$mail->AddReplyTo('info#gmail.com', 'Info');
$Appname = 'info.com';
$_subject="Newsletter From: ".$Appname;
$ema=",";
$to_bcc=explode(",",$ema);
$mail->AddCustomHeader($headers);
foreach($to_bcc as $tb){
$mail->AddBCC($tb, $dname);
}
$_body ="News content";//$hid;
$mail->FromName = "info.com";
$mail->From="inf#gmail.com";
$mail->Subject = $_subject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($_body);
if($mail->Send()){
echo "Done";
}else {
echo "Failed";
}
I experienced same. My website sends requests for data confirmation to users a few times each day while I do my daily data maintenance. I sent a test message to my Gmail address and found that if you read your mail through Gmail webmail interface it will sometimes tell you Why the message was spammed. Very useful. It gave the reason "A lot of messages from hp19.hostpapa.com were spam". I am on a budget shared server and I assume a hundred other spammers have bought accounts on the same machine as mine and are using it for evil. My site is non-profit so buying a dedicated box to avoid spam is not an option. So...
My solution was to change my CMS to not use PHP mail() at all. Now my CMS simply displays the message and a mailto: link with Subject parameter set. Now my process is to hit CTRL+C, Click the link, CTRL+V, and hit send. Messages are sent from my computer's IP Address (not on any blacklist) using my mail client, Thunderbird.
This takes me just a couple of seconds longer than it did when my CMS used PHP mail() to send the message for me. However I have found I am receiving a lot more replies so I am happy that the vast majority of messages are not getting spam-binned.
I appreciate this manual solution is not appropriate for automated bulk messaging but for small non-profit sites on shared server who trigger each message with a click, I thought it was worth sharing.
There are a number of reasons you could be going into someones spam box. Your email server could be blacklisted due to either you, or another user on your server. You can check it at http://mxtoolbox.com/blacklists.aspx
Also check your SPF records in your DNS
I am currently working on a project where I am accessing an e-mail account using PHP's imap_open(). I know that I can send an e-mail with PHP using the mail() function.
However, I was wondering if, after I send an e-mail, I could place that e-mail in the email account's sent folder using any of PHP's imap functions.
Any help would be greatly appreciated.
This is more to email account setting to save the email sent to sent account folder.
You may try with external mail that already configured to save email sent to sent folder e.g Gmail http://www.vulgarisoip.com/files/phpgmailer.zip
example :
require_once('/phpgmailer/class.phpgmailer.php');
$mail = new PHPGMailer();
$mail->Username = 'user#domain.com';
$mail->Password = 'password';
$mail->From = 'user#domain.com';
$mail->FromName = 'User Name';
$mail->Subject = 'Subject';
$mail->AddAddress('myfriend#domain.com');
$mail->Body = 'Hey buddy, here\'s an email!';
$mail->Send();