通过 SMTP 发送邮件?

问题描述 投票:0回答:3

我的网站上有一个表格,人们可以在其中参加活动。其背后的代码是这样工作的:

  1. 所有信息都保存在数据库中。这部分工作正常。

  2. 代码的第二部分向我和用户发送一封电子邮件,其中包含他输入的信息(与数据库中保存的信息相同)

问题在于这些电子邮件是通过托管帐户上的默认电子邮件发送的,未经身份验证。我必须修改脚本以强制使用我的托管帐户下的有效电子邮件进行 SMTP 身份验证才能修复错误。现在,脚本发送了电子邮件,但它最终进入了所有 ISP 的垃圾邮件过滤器,因此用户永远不会收到电子邮件。

我不知道该怎么做,也不知道如何创建代码,因此脚本使用 SMTP 身份验证。以下是我目前拥有的代码。有人可以帮助我吗?

<?
// SEND OUT EMAIL PART
// COPY SEND TO MY SELF
$to = "[email protected]"; 
$from = $_REQUEST['email'] ; 
$name = $_REQUEST['name'] ; 
$headers = "From: $from"; 
$subject = "Thanks!"; 

$fields = array(); 
$fields{"name"} = "Name"; 
$fields{"address"} = "Address"; 
$fields{"phone"} = "Phone"; 
$fields{"email"} = "E-mail addesse"; 

$body = "INFO:\n\n"; foreach($fields as $a => $b){  $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 


// SEND TO THE USER
$headers2 = "From: [email protected]"; 
$subject2 = "THANKS!"; 

$fields2 = array(); 
$from = $_REQUEST['email'] ; 
$name = $_REQUEST['name'] ; 
$headers = "From: $from"; 
$subject = "Thanks!"; 

$body2 = "

TEXT TO EMAIL RECEIVER

\n\n"; foreach ($fields2 as $a => $b){  $body2 .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 

// ERROR MESSAGES
if($from == '') {print "MISSING EMAIL ADDRESS.";} 
else { 
if($name == '') {print "MISSING NAME";} 
else { 
$send = mail($to, $subject, $body, $headers); 
$send2 = mail($from, $subject2, $body2, $headers2); 
if($send) 
{header( "Location: http://mysite/send.php" );} 
else 
{print "MISSING EMAIL ADDRESS ALL FILDS MUST BE FILLED!"; } 
}
}
?>
php email smtp send
3个回答
0
投票

最好使用专用脚本来发送电子邮件。例如 PHPMailer,它为您提供各种配置选项,包括 SMTP:

// Send Mail
$mail = new PHPMailer(); // initiate
$mail->IsSMTP(); // use SMTP

// SMTP Configuration
$mail->SMTPAuth = true; // set SMTP
$mail->Host = "myhost"; // SMTP server
$mail->Username = "[email protected]";
$mail->Password = "password";            
$mail->Port = 465; // change port is required

0
投票

这是一种方法。您需要包含 PHPMailer

#somewhere in code before you call function
include($path_to_PHPMailer."class.smtp.php");

function sendEmail($email,$name,$title,$content,$who=false,$att=array('')){
    //VARIABLE $email is email of sender 
    //VARIABLE $name is name of sender
    //VARIABLE $title is title of email
    //VARIABLE $content is content of email
    $mail = new PHPMailer();
    $mail->IsSMTP(); // telling the class to use SMTP
    // DOUBLE $mail->Host       = "your email_server"; // SMTP server 
    $mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
    // 1 = errors and messages
    // 2 = messages only
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = "your email_server"; // sets the SMTP server
    $mail->Port       = server port;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "your username"; // SMTP account username
    $mail->Password   = "your password";        // SMTP account password

    if($who){
        $mail->SetFrom($email, $name);
        $mail->AddReplyTo($email,$name);
        $address = "set your email";//EMAIL OF RECIPENT
    } else{
        $mail->SetFrom("set your email", "your name (or service name)");
        $mail->AddReplyTo("set your email","your name (or service name)");
        $address = $email;
    }

    $content=str_replace("\n", "<br>", $content);
    $mail->Subject    = $title;
    $mail->MsgHTML($content);
    $mail->AddAddress($address, $name);
    $mail->CharSet='utf-8';

    if($att[0]!=''){
        foreach ($att as $at){
            $mail->AddAttachment($at);
        }
    }

    if(!$mail->Send()) {
        return false; //$mail->ErrorInfo;
    } else {
        return true;
    }
}

0
投票
    #!/bin/bash

    # Get today's date (Day of the month and Month)
     today_day=$(date +%d)
     today_month=$(date +%m)

     # Set the recipient email and the sender email
     recipient="[email protected]"
     from_address="[email protected]"

     # Set the subject
     subject="Happy Birthday!"

     # Create an HTML file for the email content
     html_file="/tmp/birthday_wish.html"

     # Write the HTML content into the file
        cat <<EOF > $html_file
        <!DOCTYPE html>
     <html>
    <head>
        <title>Birthday Wishes</title>
    </head>
    <body>
    <h1 style="color:blue;">Happy Birthday!</h1>
    <p>Wishing you a fantastic birthday filled with joy and happiness.🎉</p>
    </body>
    </html>
    EOF

    # Check if today's date is 22 and the month is October (10)
    if [ "$today_day" -eq 22 ] && [ "$today_month" -eq 10 ]; then
    # Send the email with the HTML file as the content and custom "From" address
                cat $html_file | mailx -a "Content-Type: text/html" -r "$from_address" -s "$subject" "$recipient"
    echo "Birthday wish email sent to $recipient from $from_address."
    else
    echo "Today is not October 22nd, no email sent."
    fi
© www.soinside.com 2019 - 2024. All rights reserved.