Send message via php - php

I have problem with sending messages via php from host email to someone email with script. Like for registration code etc...
Here is my code:
$to = "$email";
$from = "support#webiste.com";
$subject = 'Web Site';
$message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>WebSite</title></head><body style="padding: 20px;"><h1>WEBSITE</h1><p>Hello '.$name.' '.$lname.'</p><h3>Click the link below to activate your account: </h3><p><a style="text-decoration: none;" href="http://www.website.com/MB/activation.php?id='.$rewr.'&re='.$re.'">Click Here</a></p></body></html>';
$headers = "From: $from\n";
$headers = "MIME-Version: 1.0\n";
$headers = "Content-type: text/html; charset=iso-8859-1\n";
if(mail($to, $subject, $message, $headers)){
echo "Sent";}else echo "failed";
I got failed message every time

sendmail is server dependent a better solution would be to use Pear mail.
include_once 'Mail.php';
include_once 'Mail/mime.php' ;
$recipients = '<' . $this->to . '>';
$text = $this->message['text'];
$html = $this->message['html'];
$crlf = "\n";
$hdrs = array('From' => $this->from,'To' => $this->to,'Subject' => $this->subject);
$mime = new Mail_mime(array('eol' => $crlf));
if(!empty($text)){$mime->setTXTBody($text);}
if(!empty($html)){$mime->setHTMLBody($html);}
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$params["host"] = MailSMTP;
$params["port"] = MailPort;
$params["auth"] = true;
$params["username"] = $username;
$params["password"] = $password;
$mail =& Mail::factory('smtp',$params);
$value = $mail->send($recipients,$hdrs, $body);
if(PEAR::isError($value)) {
return false;
}
Above is code that I use myself. It will send multi-part mail ie HTML and plain text so they can read it on any device and it uses Pear mail and also send to 1 or multiple recipients. Pear is an extension of PHP.

Related

How to send email from custom Elementor widget form?

I develop the custom Elementor widget. This is the form with two fields - email and name. Also, I have a form handler which must send the email from this form. In my widget, I added action="sender.php". In sender.php I write the code to send an email. But it does not send any emails. I also added SMTP features for PHPMailer in function.php. But t still does not work! Please, can you help me!!
sender.php
<?php
$phone = htmlspecialchars($_POST["phone"]);
$name = htmlspecialchars($_POST["name"]);
$toEmail = 'myemail#gmail.com';
$subject = 'New email';
$body ='
<h4>Name: </h4><p>'.$name.'</p>
<h4>Phone: </h4><p>'.$phone.'</p>';
$headers = "MIME-Version:1.0"."\r\n";
$headers .= "Content-Type:text/html;charset=UTF-8" . "\r\n";
$headers .="From: ".$name."<".$name.">". "\r\n";
if(mail($toEmail, $subject, $body,$headers)){
echo 'send';
};
?>
functionp.php
add_action( 'phpmailer_init', 'set_phpmailer_details' );
function set_phpmailer_details( $phpmailer) {
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.googlemail.com';
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 465;
$phpmailer->Username = 'myemail#gmail.com';
$phpmailer->Password = '******';
$phpmailer->SMTPSecure = 'ssl';
}
Ok, It seems I solved the problem! In sender.php I added in the beginning
require('../../../../wp-load.php');
And after that my code to send the email:
$phone = htmlspecialchars($_POST["phone"]);
$name = htmlspecialchars($_POST["name"]);
$to ='myemail#gmail.com';
$subject = 'Subject';
$body ='
<b>Name: </b><span>'.$name.'</span><br>
<b>Phone: </b><span>'.phone.'</span>
$headers = "MIME-Version:1.0"."\r\n";
$headers .= "Content-Type:text/html;charset=UTF-8" . "\r\n";
$headers .="From: ".$name."<".$name.">". "\r\n";
wp_mail( $to, $subject, $body, $headers );
And installed the SMTP plugin for smtp.

Php mail() vs Yahoo: Can someone Simply Explain Steps required for YAHOO to receive mail from php mail function?

I have seen thousands of similar questions asked on this topic. And for sure am aware of the "MARKED AS DUPLICATE QUESTION" thing in SO.
However, it is still not Clear how or what one has to do in simple terms to have yahoo Inbox emails from a PHP mail() function.
In the Yahoo site, they give a sample script to send mails like
Link http://help.yahoo.com/l/us/yahoo/smallbusiness/webhosting/php/php-03.html
$email = "EMAIL TO";
$subject = "Test Message";
$msg = "This is a test message";
//$eLog="/tmp/mailError.log";
//Get the size of the error log
//ensure it exists, create it if it doesn't
//$fh= fopen($eLog, "a+");
//fclose($fh);
//$originalsize = filesize($eLog);
mail($email,$subject,$msg);
//NOTE: I commented out unneeded lines
Using this basic approach found in the Yahoo's own legitimate website fails.
The second suggestion would be (for PERL) but can be converted to PHP with some editing:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
$title='mail test';
$to='MAIL ADDRESS TO SEND TO';
$from= 'EMAIL#YOURDOMAIN.COM';
$subject='Using Sendmail';
open(MAIL, "|/usr/sbin/sendmail -t");
## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL "This is a test message from Yahoo \n";
close(MAIL);
print "<html><head><title>$title<
/title></head>\n<body>\n\n";
## START HTML content
print "<h1>$title</h1>\n";
print "<p>A message has been sent from $from to $to";
## END HTML CONTENT
print "\n\n</body></html>";
Link: http://help.yahoo.com/l/us/yahoo/smallbusiness/webhosting/php/php-17.html
After few edits to make it PHPish it looks like:
<?php
///#!/usr/bin/perl
$title='_01_mail test';
$to='user_name#yahoo.com, user_name2#gmail.com';
$from= 'info#companyname.com';
$subject='_01_Using Sendmail';
## Mail Header
$headers = "To: $to\n";
$headers .= "From: $from\n";
$headers .= "Subject: $subject\n\n";
## Mail Body
$message = "<html><head><title>$title<
/title></head><body>
<h1>$title</h1>
<p>A message has been sent from $from to $to\n\n
</p></body></html>";
if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>
The user_name2#gmail.com for GMAIL sends the Email as required but user_name#yahoo.com somehow does not go anywhere.
So, what should we do?... If YAHOO's own samples are not working, then:
a) Is PHP mail() function getting deprecated?... If so, what is the alternative?
b) Should the function still be valid, how do we come up with YAHOO Inbox friendly PHP Codes?
c) What is the best practice for PHP mail() Function?
EDIT:
Additional tests.
Just tested it in this format Suggest by PHP mail() function not sending email:
$subject = "subject";
$message = "message";
$to = "USER_NAME_HERE#yahoo.com";
$type = "plain"; // or HTML
$charset = "utf-8";
$mail = "no-reply#".str_replace("www.", "", $_SERVER["SERVER_NAME"]);
$uniqid = md5(uniqid(time()));
$headers = "From: ".$mail."\n";
$headers .= "Reply-to: ".$mail."\n";
$headers .= "Return-Path: ".$mail."\n";
$headers .= "Message-ID: <".$uniqid."#".$_SERVER["SERVER_NAME"].">\n";
$headers .= "MIME-Version: 1.0"."\n";
$headers .= "Date: ".gmdate("D, d M Y H:i:s", time())."\n";
$headers .= "X-Priority: 3"."\n";
$headers .= "X-MSMail-Priority: Normal"."\n";
$headers .= "Content-Type: multipart/mixed;boundary=\"----------".$uniqid."\""."\n\n";
$headers .= "------------".$uniqid."\n";
$headers .= "Content-type: text/".$type.";charset=".$charset.""."\n";
$headers .= "Content-transfer-encoding: 7bit";
STILL YAHOO DOES NOT INBOX THE MAIL.
EDIT2
I went to this Link: http://www.forensicswiki.org/wiki/Evolution_Header_Format
YAHOO said the header should be like this:
Subject: header test
From: Username <username#sendinghost.com>
To: Username <username#receivinghost.com>
Content-Type: text/plain
Date: Sat, 28 Jul 2007 11:52:35 +0200
Message-Id: <1185616355.19231.0.camel#localhost>
Mime-Version: 1.0
X-Mailer: Evolution 2.10.1
Content-Transfer-Encoding: 7bit
Thested with PHP mail() func. ... GMail received, again, YAHOO Rejected... I Get NO Error in the Logs
I Finally got a laaaaarge smile on my face.
Working together with #DaveRandom, He helped me come up with these codes:
NOTE: The code bellow uses PHPMailer
<?php
$senderName = 'Erick Best'; //Enter the sender name
$username = 'erickbestism#yahoo.com'; //Enter your Email
$password = 'passwordHere';// Enter the Password
$recipients = array(
'erickbestism#gmail.com' => 'Erick Best',
'erickbestism#yahoo.com' => 'Yahoo User',
);
///That's all you need to do
//No need to edit bellow
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.mail.yahoo.com";
$mail->Port = 587; // we changed this from 486
$mail->Username = $username;
$mail->Password = $password;
// Build the message
$mail->Subject = 'PHPMailer mail() test';
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
$mail->AltBody = 'This is a plain-text message body';
$mail->addAttachment('images/phpmailer_mini.gif');
// Set the from/to
$mail->setFrom($username, $senderName);
foreach ($recipients as $address => $name) {
$mail->addAddress($address, $name);
}
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
And this WORKED like VOODOO!... It sent mails to any provider. Including **YAHOO**
Hope it helps someone!
try this:
<?php
include("Mail.php");
$recipients = "mailto#example.com";
$headers["From"] = "mailfrom#example.com";
$headers["To"] = "mailto#example.com";
$headers["Subject"] = "Test message";
$body = "TEST MESSAGE!!!";
$params["host"] = "example.com";
$params["port"] = "25";
$params["auth"] = true;
$params["username"] = "user";
$params["password"] = "password";
// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory("smtp", $params);
$mail_object->send($recipients, $headers, $body);
?>
where username and password are for a yahoo account.

php pear mail cant send the html formatted mail

Hi here my mail is working fine, but i dont know how to send a html formatted mail, because i am using pear mail!here i am using the code to send mail using php , first calling the mail.php and mime.php. $to mention which mail id you want to send a mail, to send a mail you need specify email id and password of email which is the sender mail.
<?php
if(isset($_POST['submit']))
{
require_once "Mail.php";//calling mail function
require_once 'Mail/mimePart.php';
$from = "you#localhost.com";
$to = "you#gmail.com";//your mail id
$CompanyName = $_POST['CompanyName'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$paddress = $_POST['paddress'];
$subject = 'MOSONS Group';
$message = 'PersonName: ' .$CompanyName. "\n";
$message .= 'Mobile: ' .$mobile. "\n";
$message .= 'Email: ' .$email. "\n";
$message .= 'Message: ' .$paddress. "\n";
$host="ssl://smtp.gmail.com";
$port="465";
$username="you#gmail.com";// your email id
$password="yourpassword"; //password
$mime= "MIME-Version: 1.0\r\n";
$type= "Content-type: text/html\r\n";
$headers = array ('From' => $from,'To' => $to ,'Subject'=>
$subject,'Mime'=> $mime,'Contenttype'=> $type);
$smtp =# Mail::factory('smtp',array ('host'=>$host,'port'=>$port,'auth'=>true,'username'=>$username,'password'=>$password));
$mail = #$smtp-> send($to,$headers,$message);
if(#PEAR::isError($mail)){
echo ("<p>" .$mail->getmessage(). "</p>");
}
else
{
echo '<script type="text/javascript">alert("Thank You for Contacting us, our team will get in touch with you soon.!"); </script>';
}
}
?>
You are setting your headers incorrectly. e.g you're doing stuff like
'Contenttype'=> 'Content-type: text/html';
It should be just
'Content-Type' => 'text/html'
PEAR::Mail is a horribly crappy package. You should switch to something better, like PHPMailer or Swiftmailer.

send email in php with attachment

i have use this code in php mail send with attachment. I am using the Apache http server with wamp.
<?php $to = 'santosh_091020#rediffmail.com';
// Declaring the necessary variables for mail sending :
$subject = 'Testing sendmail.exe';
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$mob = $_REQUEST['mob'] ;
$msg = $_REQUEST['msg'] ;
$message = ' santosh enquire \n\nName: '.$name.' \n Email:'. $email.' \n Mobile:'. $mob.' \n Message: '.$msg.' \n ';
// Declaration of the attributes for attachment.
$upload_name=$_FILES["upload"]["name"];
$upload_type=$_FILES["upload"]["type"];
$upload_size=$_FILES["upload"]["size"];
$upload_temp=$_FILES["upload"]["tmp_name"];
$fp = fopen($upload_temp, "rb");
$file = fread($fp, $upload_size);
fclose($fp);
$file = chunk_split(base64_encode($file));
$num = md5(time());
// Defining the contents of the header.
$headers = "From: ".$email. "\r\n" . "CC: santosh#creativecrows.com" ;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
//$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";
$headers .= "Content-Type:".$upload_type." ";
$headers .= "name=\"".$upload_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$upload_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
//$data = chunk_split(base64_encode($data));
// sending the mail.
if(mail($to, $subject, $message, $headers))
{
echo "Email sent";
send();
}
else
{
echo "Email sending failed";
}
//send mail in client
function send()
{
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$to= trim($email);
$subject = 'thanx';
$message = '<html><head><title>HTML email</title></head>
<body style="background-color:#000099;"><p style="color:#000099;">This email contains HTML Tags!</p><table><tr><th>Firstname</th><th>Lastname</th></tr><tr><td>John</td><td>Doe</td></tr></table></body></html>';$headers1 = 'MIME-Version: 1.0' . "\r\n";
$headers1.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers1.= "From: ".$email;
if(mail($to, $subject, $message,$headers1))
{
echo "Email sent";
}
else
{
echo "Email sending failed";
echo $to;
echo $subject;
echo $message;
}
}
?>
but i got the following error;
Delivery to the following recipient failed permanently:
1.0#localhost
Technical details of permanent failure:
DNS Error: Domain name not found.
please help me. Thnx in advance.
Check this URL,
seems your header info is not absolute
http://webcheatsheet.com/php/send_email_text_html_attachment.php
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup server
$mail->Port = '';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = '';
$mail->FromName = '';
$mail->AddAddress($row['client_email'], ''); // Add a recipient
$mail->AddReplyTo('', 'Information');
$mail->AddCC('cc#example.com');
$mail->AddBCC('bcc#example.com');
$mail->WordWrap = 50;
// Set word wrap to 50 characters
$mail->AddAttachment('kurtacompany/techreporting/upload/"'.$row['file1'].'"'); // Add attachments
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = '';
$mail->Body = '<p>type whatever you want</p>';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';

Add HTML formatting in phpmailer

I am using PHP mailer to send an online form directly to email. I want to edit the form like this:
$message = '<p>The following request was sent from: '</p>;
$message .= '<p>Name: '.$name.'</p><br />';
$message .= '<p>Phone: '.$phone.'</p><br />';
$message .= '<p>Email: '.$email.'</p><br />';
However, in the email I receive back, I get the <p>, <br />, etc. Not the formatting I want, but the actual HTML. It has always worked for me, then I realized I was using the stock PHP mail function. not sure if PHP mailer has different parameters OR if I just missing small here?
Where would I set the headers for text/html?
$mail = new PHPMailer();
$mail -> IsSMTP();
$mail -> Host = "---";
$mail -> Port = 25;
$mail -> SMTPAuth = false;
$mail -> Username = EMAIL_USER;
$mail -> Password = EMAIL_PASS;
$mail -> FromName = $from_name;
$mail -> From = $from;
$mail -> Subject = $subject;
$mail -> Body = $message;
$mail -> AddAddress("---");
$result = $mail -> Send();
In PHP mailer, you need to set below
$mail->IsHTML(true);
Note: $mail means your PHPMailer object.
Reference Link: PHPMailer
By default, the PHP mail() function is text/plain.
In your mail() headers, change the content-type to text/html and try.
Example:
<?php
$body = "<html>\n";
$body .= "<body style=\"font-family:Verdana, Verdana, Geneva, sans-serif; font-size:12px; color:#666666;\">\n";
$body = $message;
$body .= "</body>\n";
$body .= "</html>\n";
$headers = "From: My site<noreply#example.com>\r\n";
$headers .= "Reply-To: info#example.com\r\n";
$headers .= "Return-Path: info#example.com\r\n";
$headers .= "X-Mailer: Drupal\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
return mail($recipient, $subject, $message, $headers);
?>
PHP Mailer:
You need to set IsHTML(true):
$mail->IsHTML(true);

Categories