PHP Mail to SMTP - php

I currently have a piece of PHP script that sends an email from a user inputted email. This was being identified by GMail as a spam email because it spoofed the email. I would like to convert my script so that it does exactly the same things but through an SMTP email.
<?php
ob_start();
include('mplookup.php');
ob_end_clean();
$email = $_POST['emailfrom'];
$human = $_POST['human'];
$text = $_POST['text'];
$address = $_POST['address'];
$city = $_POST['city'];
$footer = '<br><em><strong>Disclaimer: "Any views or opinions presented in this email are solely those of the author and do not necessarily represent those of EG4DEMUK. EG4DEMUK will not accept any liability in respect of defamatory or threatening communication. If you would like to raise a complaint about an email sent using our tool, please contact us at ".</strong></em><p>-----------------------------------------------------------</p>
The ERC is an organisation that brings together Egyptian citizens and movements abroad, irrespective of their political or ideological affiliations. We share in common a belief in the principles of the January 25th Revolution and oppose all aspects of corruption and dictatorship in Egypt. We believe in constitutional legitimacy and work for the establishment of a civil state that reflects the will of the Egyptian people and their freedom in choosing their government.</p>';
$postcode = $_POST['postcode'];
$name = $_POST['flname'];
$message = $text.$name."<br />".$address."<br />".$city."<br />".$postcode."<br />".$footer;
$to = "";
$subject = 'Sisi\'s visit to the UK: Sent using the ERC\'s Tool ';
$headers = "From: ".$email."\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "Return-Path: ".$email."\r\n";
$headers .= "BCC: \r\n";
$headers .= "CC: $email\r\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
?>
<?php
if ($_POST['submit'] && $human == '4')
{
if (mail ($to, $subject, $message, $headers)) {
echo '<p>Your message has been sent! Thank you for participating in EG4DEMUK\'s campaign.</p>';
}
else
{
echo '<p>Something went wrong, please go back and try again!</p>';
}
}
?>
I have no clue how to proceed in converting this to SMTP. Any help would be much appreciated.

You can use a pre-written PHP library for this.
https://github.com/PHPMailer/PHPMailer
The GitHub page includes a very good example script and you can simply copy your already written parameters to the corresponding variables of the library.
For example:
$to = "xxx#outlook.com"; becomes $mail->addAddress('xxx#outlook.com');
$mail->Body = $message;
etc.

Related

How to send e-mail to multiple recipients without limit

I want to use php to send email to many e-mail addresses but:
I want to make 30 seconds delay between every sent email
I don't want limit to recipients list (my list is over 500 email) when I run the code it tells my time out after 30+ sent email
this is the code I use so far hope if you can help
<?php
$EmailsDB = file('emails.txt');
$from = "myemail#website.com";
$fromName = "Website Name";
$subject = "Subject";
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header .= 'From: '.$fromName.'<'.$from.'>' . "\r\n";
$message = file_get_contents("message.html");
foreach ($EmailsDB as $to) {
if(mail($to, $subject, $message, $header)) {echo"E-mail sent successfully to $to <br />";}
else {echo"Sorry, failed while sending!";}
}
?>
Hope to find the answer here,
kind regards

Mail "received" header change

It has been 2 days now trying to find a solution for this problem.
Recently my local ISP changed their security settings and now emails generate from PHP scripts is being rejected due header "Received: (from apache#localhost)".
Searching various articles and forums I have come to know that webserver/computer cannot find its own name (so it comes out
as "localhost") when mails are being sent. I have every solution mentioned on the forums but still could not change the "localhost" part to my server name.
hostname at /etc/hosts is ok.
have added MASQUERADE_AS... to /etc/mail/sendmail.mc...
/etc/sysconfig/network is ok.
I desparately need help and wonder if someone can help me solve this problem.
function MAIL_NVLP($fromname, $fromaddress, $toname, $toaddress, $subject, $message)
{
// Copyright ? 2005 ECRIA LLC, http://www.ECRIA.com
// Please use or modify for any purpose but leave this notice unchanged.
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: php\n";
$headers .= "From: \"".$fromname."\" <".$fromaddress.">\n";
return mail($toaddress, $subject, $message, $headers);
}
$message = "This is a test from the support dept";
$fromname = "myname";
$fromaddress = "myname#domain.com";
$toname = "myname";
$toaddress = "myname#domain.com";
$subject = "testing new script one";
MAIL_NVLP($fromname, $fromaddress, $toname, $toaddress, $subject, $message);
echo $subject;

add image to PHP email script

I've done some research on this issue but still can't find an answer that I understand. Basically I'm trying to add a Logo to the emails sent via my email PHP script. The code of my PHP script is:
<?php
$name = $_POST['Name'];
$email = $_POST['Email'];
$sub = $_POST['Subject'];
$message = $_POST['Message'];
$Subject = "$sub";
$Sendto = "Omitted for privacy reasons";
mail($Sendto,$Subject,
"$message
Email: $email
Name: $name
"
,"From: $email,$name");
?>
How do I go about putting an image held on my server in Images/logo above the email:$email part of the message?
thanks
As a side question can anyone provide me with a guide or reading on how to beautify the message as its so plain
What you might do is to send the mail not as "text/plain", but as "text/html".
You e-mail content is then basically a static webpage and you can insert images to.
Example Code for Mail with HTML Content
$to = 'to#someone.com';
$subject = 'The Subject';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: other#someone.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
// this is the important header to set the type
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// now $message can be a static html page like:
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '</body></html>';
// with image embedded
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '<img src="http://i.stack.imgur.com/Qmw3Z.jpg?s=32&g=1" alt="AVTR" />';
$message .= '</body></html>';
Important: If you insert something into the message coming from POST, do not forget to escape it properly with htmlentities($_POST['somevar']) or striptags.
Then send mail:
mail($to, $subject, $message, $headers);
Answer to side-quest: "can anyone provide me with a guide or reading on how to beautify the message as its so plain". When you send the mail as HTML you might apply CSS styles to your HTML.
Starter tutorial: http://webdesign.tutsplus.com/articles/build-an-html-email-template-from-scratch--webdesign-12770
you might also google for "html email templates", download one and modify to your needs

PHP mail form problems

I'm creating a simple mail form with checkboxes, a couple of input tabs, and a text input section. It uses PHP to retrieve the information then email it to a specific email. Right now I have it emailing to my own yahoo email just for testing. When I test the site on my hosting account jacobbuller.com/testsites/peacock/contact.php the form works perfectly and forwards the email from my generic "theski" server email. But when I upload the site to the actually live hosting account for peacockautoservice.com the contact form works - it executes and sends a ?msg=1 variable in the url - but I never receive the email in my Yahoo account...
Here's the PHP I am using to send the email.
<?php ob_start();
<?php
$required_field = array('name', 'email', 'message');
foreach($required_field as $fieldname) {
if (!isset($_POST[$fieldname])) {
$errors[] = $fieldname;
}}
if (empty($errors)) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$contact = $_POST['contact'];
$phone = $_POST['phone'];
$services = IsChecked('checkboxes', 'factory');
$services .= IsChecked('checkboxes', 'timing belt');
$services .= IsChecked('checkboxes', 'brakes');
$services .= IsChecked('checkboxes', 'computerized');
$services .= IsChecked('checkboxes', 'steering and suspension');
$services .= IsChecked('checkboxes', 'heating and air');
$services .= IsChecked('checkboxes', 'electrical');
$services .= IsChecked('checkboxes', 'other');
$body = "Customer:" . $name;
$body.= "Phone Number:" . $phone;
$body.= "Contact:" . $contact;
$body.= "Services:" . $services;
$body.= "Message:" . $message;
$to = "jcbbuller#yahoo.com";
$subject = "Peacock Auto Services Inquirey";
$from = $email;
$mailed = mail($to, $subject, $body, $from) or die("Error!");
}
if($mailed) {
redirect_to("../contact.php?msg=1");
}
?>
<?php
// IsChecked FUNCTION - Detemines what checkbox are/aren't checked on form.
function IsChecked($postname, $value){
if(!empty($_POST[$postname])) {
foreach($_POST[$postname] as $job) {
if ($job == $value) {
$project = " ". $value . " ";
return $project;
}
}
}
} //END IsChecked FUNCTION
function redirect_to( $location = NULL ) {
if ($location != NULL) {
header("Location: {$location}");
exit;
}
}
?>
<?php ob_end_flush(); ?>
Please let me know if you see something wrong with the PHP above or if you know why their GoDaddy hosting account is not executing the email. I tried using their customer service network but they said that they can't help me with my code...
Your issue is mainly at server end. Mail function is working because of your check on it, if it had failed, it would have given you notification. So, mails are going definitely. If mail server is working properly at your production server, then check for SPAM folder at yahoo mail server. I would suggest you to ask your hosting provider to enable SPF and DKIM records because most of email providers requires sender authentication (if it is not a spam) and these records are helpful in it.
I can also see that your not using any headers, so I would suggest you to use extended headers to avoid providers identifying you as a spammer. I use below mentioned headers and my emails never go in spam on anyprovider, but again it depends on IP reputation of the server as well.
$headers .= "Reply-To: Awaraleo <email#domain.org>\r\n";
$headers .= "Return-Path: Awaraleo <email#domain.org>\r\n";
$headers .= "From: Awaraleo <email#domain.org>\r\n";
$headers .= "Organization: Awaraleo\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
and then use it like
mail($to,$subject,$message,$headers);
Here email#domain.org should be a valid email address created on the domain where this form is implemented.
Another authentic way is to use SMTP Authentication in coding.

php mail function not getting the emails

ok, so I put together a very basic mail function and while testing this, I used a couple of email accounts, one my google account and the other my work account. I get all emails at the google account, but not to those pointing at my work. I'm thinking that could be because they have been caught up with the anti-spam software. Any ideas on how can I develop the mail function to avoid being caught on with spam software?
Here is a copy of my mail function
$to = 'account#gmail.com';
$subject = 'The subject';
$message = 'Hello,'."\n";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers = 'From: me#mycompnay.com' . "\r\n" .
'CC: anotherone#mycompany.com' . "\r\n";
$mail_sent = mail($to, $subject, $message, $headers);
if($mail_sent) {
header("location:newlocation.php");
}
}
A lot of times this has less to do with PHP's mail() function and much more to do with the configuration of your mail transport agent. A lot of mail servers will bounce messages they assume are from spammers (i.e. unconfigured/misconfigured senders) before they even get passed to a spam filter.
If you check your MTA's logs you'll probably find some bounce messages the likes of, "Mail from this server not allowed, see blacklist info at [insert url].
Spam filters use a lot of different methods to determine if the mail coming in is actually spam or not.
Here are a few things that I would suggest:
Descriptive subject line
Descriptive message, careful with HTML and other rich content inside the body as sometimes spam filters will pick up on it as an "advertisement".
Full complete headers with realistic information in there as much as possible.
Try experimenting with different combination's and see if you can get one through to your work. The good thing is that your google account got the e-mail so you know its not an server side issue locally.
you probably need to format your headers and content properly. boundaries are missing.
Here's one simple function with HTML formatting mail:
<?php
function html_mail($i){
$to = $i['to'];
$to_name = $i['to-name'];
$subject = $i['subject'];
$html_message = $i['message'];
$from = $i['from'];
$from_name = $i['from-name'];
$reply_to = $i['reply-to'];
$reply_to_name = $i['reply-to-name'];
if(!$to || !validate::email($to)){return false;}
$email_message = '';
$email_subject = $subject;$email_txt = $html_message;
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$email_to = ($to_name ? $to_name.'<'.$to.'>':$to);
$headers = "From: ".($from_name!='' ? $from_name.'<'.$from.'>':$from)."\n";
if($reply_to){
$headers .= "Reply-To: ".($reply_to_name ? $reply_to_name.'<'.$reply_to.'>':$reply_to)."\n";
}
$headers .= "MIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n";
$email_message .= "--{$mime_boundary}\n";
$email_message .= "Content-Type: text/html; charset=utf-8\n";
$email_message .= "Content-Transfer-Encoding: 8bit\n\n";
$email_message .= $email_txt;
$email_message .= "\n\n";
$email_message .= "--{$mime_boundary}\n";
$email_message .= "Content-Type: text/plain; charset=utf-8\n";
$email_message .= "Content-Transfer-Encoding: 8bit\n\n";
$email_message .= trim(strip_tags(str_replace(array('<br/>','<br />','<br/>'),"\r\n",$email_txt)));
$email_message .= "\n\n";
$email_message .= "--{$mime_boundary}--";
$ok = #mail($email_to, $email_subject, $email_message, $headers);
return $ok;
}
?>
when you have a properly formatted mail, you probably will be able to by pass the filters.
Spam determination is entirely determined by the software that's running the spam heuristics. You would have to look into the anti-spam software that your company uses and see why it's being caught as spam. More often than not it has to do with your mail server set up. A key factor that a lot of software uses is a valid reverse DNS entry, so you could look into that.
You have to realize that if there was an easy way around anti-spam software catching your email as spam just by modifying a few headers, then anti-spam software would be entirely useless, since the spammers would know those methods as well.
Adding a valid 'from' header would be the first thing to do, I think.
and thank you very much for all your suggestions. I found the answer on this post
How to change envelope from address using PHP mail?
It worked.
L.

Categories